I am new to Keras and CNN. I am working on an assignment to build a CNN for predicting face emotions. I built the model as per the assignment but while compiling the model I get “ValueError: Shapes (None, 1) and (None, 7) are incompatible”. Can someone help me how to resolve this?

Pasting my code below for reference:

”’

model = Sequential() model.add(Conv2D(filters = 64, kernel_size = 5,input_shape = (48,48,1))) model.add(Conv2D(filters=64, kernel_size=5,strides=(1, 1), padding='valid')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2,2),strides=1, padding='valid')) model.add(Activation('relu')) model.add(Conv2D(filters = 128, kernel_size = 5)) model.add(Conv2D(filters = 128, kernel_size=5)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Activation('relu')) model.add(Conv2D(filters = 256, kernel_size = 5)) model.add(Conv2D(filters = 256, kernel_size=5)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(128)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.25)) model.add(Dense(7,activation='softmax'))

”’ ‘Then tried to compile’ ”’

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit(input_array, output_array, batch_size = 64, epochs= 20, validation_split=0.10,)

”’ ‘This gives the error’ ‘ValueError: Shapes (None, 1) and (None, 7) are incompatible’ ‘I am using google colab for this’

## Answer

You are most likely using your labels sparsely encoded, like [`0,1,2,3,4,5,6]`

instead of a one-hot-encoded form.

Your solution is to choose from one of the below:

- Use the one-hot-encoded form, i.e. transform each label to an array of length == number_of_classes. That is, for 0 you would have
`[1,0,0,0,0,0,0]`

for 1 you would have`[0,1,0,0,0,0,0]`

etc. - Use
`sparse_categorical_crossentropy`

. If you use this loss functions, the OHE step is done behind the scenes and you no longer need to process your input training + validation labels.