I have built this network for text classification:

model = Sequential()
model.add(Embedding(vocabulary_dim, 150, input_length=max_length)) 
model.add(LSTM(150, return_sequences=False))
model.add(Dense(1, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

When I fit the model:

model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=5, batch_size=128)

I get this error:

ValueError: Error when checking model target: expected dense_9 to have shape (None, 1) but got array with shape (12481, 3)

I suppose because y_train and y_val are one-hot encoded, with shape:

array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 1.,  0.,  0.],

but I'm not sure about this.

  • 1
    $\begingroup$ Change this model.add(Dense(3, activation='softmax')) to model.add(Dense(1, activation='softmax')) $\endgroup$ Sep 23, 2018 at 18:53

1 Answer 1


With a single output unit model.add(Dense(**1**, activation='softmax')), Keras is expecting a binary outcome, and so the 'softmax' activation function and the 'categorical_crossentropy' loss are not appropriate.

If your output is binary, use: model.add(Dense(1, activation='sigmoid')) and model.compile(loss='binary_crossentropy'.

If your outcome is not binary, then the number of output units needs to be adjusted to match the number of output categories.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.