Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected dense_3 to have shape (None, 1) but got array with shape (17268, 2)

Tags:

python

keras

I am new to Keras and I am trying to make a model for classification, this is my model:

model = Sequential()
model.add(Dense(86, activation='sigmoid', input_dim=21))
model.add(Dense(50, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))

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

but it keeps giving me this error:

ValueError: Error when checking target: expected dense_3 to have shape (None, 1) but got array with shape (17268, 2)

Now I know that I need to encode my labels using one hot encoding and flatten them, so I've done that too.

oht_y_train = np_utils.to_categorical(y_train, num_classes=3)
oht_y_train = np.ndarray.flatten(oht_y_train)

But I still get the same error.

NOTE: Before I flattened the labels I got the same error, just the shape was (5765, 3)

I have also printed the shape of the labels array, it gives me (17268,)

like image 727
L.Ignov Avatar asked Jul 09 '17 15:07

L.Ignov


1 Answers

Your labels should not be one-hot encoded if your final layer has an output dimension of 1 (for binary classification). If you have several classes, you should use one-hot encoding and a categorical_crossentropy loss function, but your final output layer should have dimension 3, i.e. Dense(3), where 3 is the number of classes. You should not be flattening the labels after they are encoded.

model = Sequential()
model.add(Dense(86, activation='sigmoid', input_dim=21))
model.add(Dense(50, activation='sigmoid'))
model.add(Dense(3, activation='sigmoid'))

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

model.fit(X_data, Y-one-hot-encoded) # here your labels have shape (data_size, 3).

If you only need to perform binary categorization, then it is better to use a binary_crossentropy loss and have output dimension 1, using Dense(1) and a sigmoid or softmax activation to normalize outputs between 0 and 1.

 model = Sequential()
 model.add(Dense(86, activation='sigmoid', input_dim=21))
 model.add(Dense(50, activation='sigmoid'))
 model.add(Dense(1, activation='sigmoid'))

 model.compile(loss='binary_crossentropy', optimizer='nadam', metrics=['accuracy'])

 model.fit(X_data, Y_labels) # here your labels have shape (data_size,).
like image 81
JAustin Avatar answered Oct 19 '22 22:10

JAustin