ValueError: Shapes (None, 1) and (None, 11) are incompatible

I have the following model:

Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_9 (Dense)              (None, 256)               10496     
_________________________________________________________________
activation_9 (Activation)    (None, 256)               0         
_________________________________________________________________
dropout_6 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_10 (Dense)             (None, 256)               65792     
_________________________________________________________________
activation_10 (Activation)   (None, 256)               0         
_________________________________________________________________
dropout_7 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_11 (Dense)             (None, 11)                2827      
_________________________________________________________________
activation_11 (Activation)   (None, 11)                0         
=================================================================
Total params: 79,115
Trainable params: 79,115
Non-trainable params: 0

With the following inputs/labels:

x_train: (100, 40)
y_train: (100,)

I take in audio files, convert to a 40-long MFCC feature vector. I have 100 examples. That’s where I get the (100, 40). The labels (100 of them, one for each example) are all strings, and there are 11 classifications.

I followed a tutorial and used this to build a model:

def build_model(feat_len, out_len):
    model = Sequential()
    model.add(keras.Input(shape=(feat_len,)))
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(out_len))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
    return model

For the input, I am using feat_len = 40 (the number of MFCC features). For the output dense layer, I’m using out_len = len(labels) = 11.

However, when trying to run

score = model.evaluate(x_train, y_train, verbose=0)

it errors out with ValueError: Shapes (None, 1) and (None, 11) are incompatible.

I believe this to be an error in the shapes of my x_train and y_train, yet I’m not quite sure. I can see that the model (in the summary) is expecting an output of (None, 11), so why is it outputting (None, 1) instead?

Answer

The problem here is just that you need to use sparse_categorical_crossentropy as the loss function. That will take care of “expanding” the input labels automatically through one-hot encoding.