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,)
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,).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With