0
$\begingroup$

While training a sequential model using Keras, Im getting this error

The model summary is shown below

Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               20480512  
_________________________________________________________________
activation_1 (Activation)    (None, 512)               0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
activation_2 (Activation)    (None, 512)               0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 1026      
_________________________________________________________________
activation_3 (Activation)    (None, 2)                 0         
=================================================================

I used the below steps to train the model, for binary classification,

model = Sequential()

model.add(Dense(512, input_shape=(vocab_size,)))
model.add(Activation('relu'))
model.add(Dropout(0.3))

model.add(Dense(512))
model.add(Activation('relu'))

model.add(Dropout(0.3))
model.add(Dense(num_labels))
model.add(Activation('softmax'))

num_labels = 2 for the above code

The error is shown below.

ValueError: Error when checking target: expected activation_3 to have
shape (2,) but got array with shape (1,)
$\endgroup$
  • $\begingroup$ When do you get the error? On .compile()? Or .fit()? If it's .fit() can you show us the .shape for the x and y variables you pass to it? $\endgroup$ – Dan Scally Jul 30 at 10:56
  • $\begingroup$ I get the error on .fit(). X_train shape is (1, 40000) y_train shape is (1, 1) $\endgroup$ – krishna rao gadde Jul 30 at 11:00
  • $\begingroup$ Thanks. I added an answer explaining the problem generally. Your x_train shape is also crazy though; you have 1 sample of 40,000 features!? $\endgroup$ – Dan Scally Jul 30 at 11:07
0
$\begingroup$

The problem is the shape of y_train. You're defining an output layer of 2 neurons, and only passing labels with a shape of (n_samples, 1). You need to convert this to shape (n_samples, num_labels) and can use keras.utils.to_categorical to do so easily.

The number of neurons in your output layer has to match the second dimension of the label array, so you transform it from one dimensional like this:

[0,1,0,0,0,1]

To two dimensional like this:

[[1,0],
[0,1],
[1,0],
[1,0],
[1,0],
[0,1]]

When you make your predictions, softmax activation will give you a 'probability' for each class like so:

[[0.97,0.03],
[0.85,0.15],
...
]]
$\endgroup$
  • $\begingroup$ I'm using this model for binary text classification, I have two classes, so i have made a model.. I did some changes and got these shapes x_train (2, 40000), y_train(2, 1) .Still getting the value error on y_train. I think it requires y_train(2,2) $\endgroup$ – krishna rao gadde Jul 30 at 11:24
  • $\begingroup$ @krishnaraogadde yes, as I say the second dimension of your y_train has to match num_labels (because you're using num_labels to define the number of neurons in your output layer). So yes, y_train needs to be (2, 2). Use keras.utils.to_categorical to convert it. $\endgroup$ – Dan Scally Jul 30 at 11:43
  • $\begingroup$ Yes. It worked. Thank you @DanScally !! $\endgroup$ – krishna rao gadde Jul 30 at 11:52
  • $\begingroup$ @krishnaraogadde You are welcome! If you're happy with the answer please consider marking it as accepted :) $\endgroup$ – Dan Scally Jul 30 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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