ValueError: logits and labels must have the same shape ((None, 5) vs (None, 1))
If your labels are sparse integers, where each sample belongs to one of five classes {0, 1, 2, 3, 4}
then you should be using a softmax
activation function on your output layer and SparseCategoricalCrossentropy()
:
cnn_model.add(Dense(units = 5, activation='softmax'))
cnn_model.compile(optimizer='adam', loss = tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
If you are working on a binary classification problem where each sample can either belong to class 0 or 1, then you need to change the last layer to have exactly one output node:
cnn_model.add(Dense(units = 1, activation='sigmoid'))
If you are working on a multiple label classification problem where each label can belong to multiple labels, such as [1, 1, 0, 1, 0]
, then you need to one-hot encode your labels so that each label consists of 5 binary numbers representing the presence or absence of each class for each sample. Your model itself can remain untouched.