# Issue with output dimensions in keras

I'm currently trying to build and train a model for CIFAR data using keras. My labels should be one-hot encoded.

data.y_train.shape


is (45000, 10). My model is defined like this:

model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))


Yet, when I try to train the model, I get this error:

ValueError: Error when checking target: expected dense_10 to have shape (1,) but got array with shape (10,)


Does someone know, why the dimensions do not fit?

• Could you please include the model.compile(...) and model.fit(...) instructions as well? That would help.
– Tinu
Oct 30 '19 at 10:27
• The Error occurs in feeding a wrong shaped input in one of the Dense Layers. Please provide a model.summary()-output, because there is the layer-id specified (i.e. "dense_10") Oct 30 '19 at 11:58
• Please share data pre-pration part of the code specially train_test_split and shape of data before and after the split? Most probably, your target is is wrong shape. Therefore, use reshape() function to shape it if you have already used one-hot encoding! Happy coding. Oct 30 '19 at 12:21

Answer: The error states that the target needs to be in single row i.e. (1,) where as you are giving (10,)

ValueError: Error when checking target: expected dense_10 to have shape (1,) but got array with shape (10,)


SOLUTION 1: Use "keras.utils.np_utils.to_categorical" to convert your model data labels to categorical one hot vectors.

from keras.utils import to_categorical
label = array(label_column)
# one hot encode
encoded_label = to_categorical(label)


Then you can have 10 labels in target which your last layer expects.

model.add(keras.layers.Dense(10, activation='softmax'))


SOLUTION 2: If above does not work, then change the last layer of your CNN network to have one output as given below.

 model.add(Dense(1, activation='sigmoid'))

• Solution 1: The Label are already one hot encoded -> data.y_train.shape is (40000, 10) / Solution 2: This would create a binary classification, but there are 10 classes. Oct 30 '19 at 11:38
• Ok, the, you need to use reshape() function to reshape your target here. It is likely you are feeding in wrong shape i.e. your CNN model expects (10,) while you have not reshaped the data before feeding it to the model. Also, for better understanding, can you share the code from data prepration part? Oct 30 '19 at 11:55
• Yes. We need more information :) Asked the OP to do so. Oct 30 '19 at 12:00
• Looking at the keras documentation, I don't think to_categorical would work since it does exactly the opposite of what the error says, i.e. it will convert the matrix from (1,) to (10,) while it needs to go the other way. Mar 28 '20 at 22:45
• It works, please check the documentation carefully. keras.io/utils Here is an example code:'''# Consider an array of 5 labels out of a set of 3 classes {0, 1, 2}: > labels array([0, 2, 1, 2, 0]) # to_categorical converts this into a matrix with as many # columns as there are classes. The number of rows # stays the same. > to_categorical(labels) array([[ 1., 0., 0.], [ 0., 0., 1.], [ 0., 1., 0.], [ 0., 0., 1.], [ 1., 0., 0.]], dtype=float32)''' Mar 30 '20 at 18:17