Search code examples
kerasclassificationperceptron

compilation step in keras sequential model throwing the error "ValueError: Input 0 of layer sequential_9 is incompatible with the layer:


I'm trying to develop as classifier for two classes. I've implemented the model as follows:

model = keras.models.Sequential() #using tensorflow's version of keras
model.add(keras.layers.InputLayer(input_shape = X_train_scaled[:,1].shape))
model.add(keras.layers.Dense(250,activation="relu"))
model.add(keras.layers.Dense(50,activation="relu"))
model.add(keras.layers.Dense(2,activation="softmax"))
model.summary()

enter image description here

# Compile the model
model.compile(loss = 'sparse_categorical_crossentropy',
             optimizer = "sgd",
             metrics = ["accuracy"])

The size of the inputs are

X_train_scaled[:,1].shape, y_train.shape
((552,), (552,))

The entire error message is:

ValueError: Input 0 of layer sequential_9 is incompatible with the layer:
expected axis -1 of input shape to have value 552 but received input with shape (None, 1)

What am I doing wrong here?


Solution

  • The error message says that you defined a model which expects as input a shape of (batch_size, 552) and you are trying to feed it an array with a shape of (batch_size, 1).

    The issue is most likely with

    input_shape = X_train_scaled[:,1].shape)
    

    This should most likely be:

    input_shape = X_train_scaled.shape[1:]
    

    i.e. you want to define the shape of your model to the the shape of the features (without the number of examples). The model is then fed in mini-batches... e.g. if you do call model.fit(X_train_scaled, ...) keras will create mini-batches (of 32 examples by default) and update the model weights for each mini batch.

    Also, please be aware that you defined the model to return a shape of (batch_size, 2). So y_train must have a shape of (X_train.shape[0], 2).