Yanick Schraner
Yanick Schraner

Reputation: 315

Keras: expected activation_3 to have shape (None, 3) but got array with shape (5708, 1)

I wan't to train a simple multilayer perceptron with Keras. My input (x_train) is a np.array where every data point is represented by an 300 dimensional vector. My output should be a class 0,1 or 2. Shapes: x_train: (5708, 300) y_train: (5708,) shape: (300,)

shape = x_train[0].shape
model = Sequential()
model.add(Dense(32, input_shape=shape))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=5)

After calling model.fit I get this error:

ValueError: Error when checking target: expected activation_3 to have shape (None, 3) but got array with shape (5708, 1)

What is wrong and what which layer is activation_3?

Upvotes: 2

Views: 3376

Answers (3)

user9606893
user9606893

Reputation: 1

The shape of y_train have to be (5708,num_classes). And you must change your structure of y_train with keras.utils.to_categorical(labels, num_classes=10)

Upvotes: 0

Ioannis Nasios
Ioannis Nasios

Reputation: 8527

Convert your y_train from shape (5708,1) to shape (5708,3)

from keras.utils import np_utils
y_train=np_utils.to_categorical(y_train)

Upvotes: 1

chiragjn
chiragjn

Reputation: 774

The error occurs while comparing the output of your network (shape 5708 x 3) with your provided y_train (shape 5708 x 1)

Your network output has shape Batch Size x Num Classes i.e. 5708 x 3 (a probability distribution on the three output classes) and thus ground truth labels should be one hot encoded to be able to use categorical cross entropy

So for any input sample vector of 300 dims, the true labels should be either one of [1, 0, 0], [0, 1, 0] or [0, 0, 1]

Upvotes: 4

Related Questions