Search code examples
tensorflowmachine-learningkerasmlp

Keras layer shape incompatibility for a small MLP


I have a simple MLP built in Keras. The shapes of my inputs are:

X_train.shape - (6, 5)
Y_train.shape - 6
  

Create the model

model = Sequential()
model.add(Dense(32, input_shape=(X_train.shape[0],), activation='relu'))
model.add(Dense(Y_train.shape[0], activation='softmax'))
# Compile and fit
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, Y_train, epochs=10, batch_size=1, verbose=1, validation_split=0.2)
# Get output vector from softmax
output = model.layers[-1].output

This gives me the error:

ValueError: Error when checking input: expected dense_1_input to have shape (6,) but got array with shape (5,). 

I have two questions:

  1. Why do I get the above error and how can I solve it?
  2. Is output = model.layers[-1].output the way to return the softmax vector for a given input vector? I haven't ever done this in Keras.

Solution

  • in the input layer use input_shape=(X_train.shape[1],) while your last layer has to be a dimension equal to the number of classes to predict

    the way to return the softmax vector is model.predict(X)

    here a complete example

    n_sample = 5
    n_class = 2
    X = np.random.uniform(0,1, (n_sample,6))
    y = np.random.randint(0,n_class, n_sample)
    
    model = Sequential()
    model.add(Dense(32, input_shape=(X.shape[1],), activation='relu'))
    model.add(Dense(n_class, activation='softmax'))
    
    # Compile and fit
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    model.fit(X, y, epochs=10, batch_size=1, verbose=1)
    
    # Get output vector from softmax
    model.predict(X)