I am trying to build a model using kerras but for some reason I receive a ValueError: Error when checking target: expected activation_5 to have 4 dimensions, but got array with shape (50000, 10) error. I have recently changed the model parameters, so I think it has something to do with that, but I cannot understand what exactly. Before I changed the model everything worked

Here is the code

from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.constraints import maxnorm
from keras.utils import np_utils
from keras.datasets import cifar10
from tensorflow.python.keras.callbacks import TensorBoard
from time import time
seed = 21
tensorboard = TensorBoard(log_dir="logs/{}".format(time()))

#loading and preprocessing training and testing data
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
train_data = train_data.astype('float32')
test_data = test_data.astype('float32')
train_data = train_data / 255.0
test_data = test_data / 255.0
train_labels = np_utils.to_categorical(train_labels)
test_labels = np_utils.to_categorical(test_labels)
class_num = test_labels.shape[1]

#building model
model = Sequential()
model.add(Conv2D(32, (5,5), input_shape=(32,32,3), padding ='same'))
model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='same'))
model.add(Conv2D(32, (5,5), padding ='same'))
model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='same'))
model.add(Conv2D(64, (5,5), padding ='same'))
model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='same'))

epochs = 50
optimizer = 'adam'
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

model.fit(train_data, train_labels, validation_data=(test_data, test_labels), epochs=epochs, batch_size=64, callbacks=[tensorboard])
scores = model.evaluate(test_data, test_labels, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
  • 1
    $\begingroup$ Your last layer has 10 "classes". How many classes do your data have? I guess 4? In this case you need to change 10 to 4 $\endgroup$
    – Peter
    Mar 10, 2020 at 13:29
  • $\begingroup$ Tried that, didn't work. I do also think that the cifar10 set has 10 classes. $\endgroup$
    – SoMax
    Mar 10, 2020 at 17:20

1 Answer 1


You are missing a flatten() layer between your last MaxPooling2D and Dense layer.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.