Mohammad Heydari
Mohammad Heydari

Reputation: 4270

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

Well, I'm trying to utilize maxpooling as a first layer by keras framework. I Work on MNIST famous dataset towards digits recognition and have a problem with the input and output dimensions.

Here's my model summary:

    Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_6 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_7 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_7 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_8 (Dense)              (None, 10)                1290      
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0

I got this error at the final step:

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

I'm trying to do the classification task.

These are the main part of my program:

from keras.utils import to_categorical
num_class = 10

y_train = to_categorical(y_train, num_class)
y_test = to_categorical(y_test, num_class)
#
#Model created
from keras.models import Sequential
#Layers added
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),
     activation = 'relu', #A “relu” activation stands for “Rectified Linear Units”, which takes the max of a value or zero
     input_shape=(img_rows, img_cols, 1)))
#
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#
model.add(Dropout(0.25))
#
model.add(Flatten())
#
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_class, activation='softmax'))
#“softmax” activation is used 
#when we’d like to classify the data into a number of pre-decided classes
model.compile(loss = 'sparse_categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
#
batch_size = 128
epochs = 10

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("test_model.h5")

Upvotes: 0

Views: 489

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

You are using the wrong loss, sparse_categorical_crossentropy requires integer labels, not one-hot encoded labels, so you can just use categorical_crossentropy as loss, which requires one-hot encoded labels.

Upvotes: 1

Related Questions