# Getting Value Error while training a model for binary classification

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()



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,)

• 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? – Dan Scally Jul 30 at 10:56
• I get the error on .fit(). X_train shape is (1, 40000) y_train shape is (1, 1) – krishna rao gadde Jul 30 at 11:00
• 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!? – Dan Scally Jul 30 at 11:07

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],
...
]]

• 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) – krishna rao gadde Jul 30 at 11:24
• @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. – Dan Scally Jul 30 at 11:43
• Yes. It worked. Thank you @DanScally !! – krishna rao gadde Jul 30 at 11:52
• @krishnaraogadde You are welcome! If you're happy with the answer please consider marking it as accepted :) – Dan Scally Jul 30 at 12:09