Search code examples
kerasneural-networksequential

ValueError: Shapes (None, 6) and (None, 5) are incompatible


i am trying to train my Sequential model but something goes wrong:

aspect_categories_model = Sequential()
aspect_categories_model.add(Dense(512, input_shape=(6000,), activation='relu'))
aspect_categories_model.add(Dense(5, activation='softmax'))
aspect_categories_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

when try to predict value with: aspect_categories_model.fit(aspect_tokenized, dummy_category, epochs=5, verbose=1). It got me a value error:

ValueError: Shapes (None, 6) and (None, 5) are incompatible

the code for dummies is:

from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical

label_encoder = LabelEncoder()
integer_category = label_encoder.fit_transform(dataset.aspect_category)
dummy_category = to_categorical(integer_category)

the labels are 5.


Solution

  • The shape of dummy_category is (batch_size, 6) and the output shape of the model is (batch_size, 5).

    Try changing the number of neurons in the final layer.

    aspect_categories_model.add(Dense(6, activation='softmax'))
    

    If you have only 5 categories for prediction then you have made some mistake in computing your dummy_category variable.