confusedstudent
confusedstudent

Reputation: 395

ValueError: Shapes (None, 1) and (None, 64) are incompatible Keras

I'm trying to build a sequential model . I have 32 features as the input dimension and it's a classification problem. this is the result of the summary : enter image description here

and this is my model:

#Create an ANN using Keras and Tensorflow backend
from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential
from keras.layers import Dense, Dropout,Activation
from keras.optimizers import Adam,SGD
nb_epoch = 200
batch_size = 64

input_d = X_train.shape[1]


model = Sequential()
model.add(Dense(512, activation='relu', input_dim=input_d))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu', input_dim=input_d))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Activation('softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
rms = 'rmsprop'
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy']) 

the test and train shape are both 32. I get the ValueError: Shapes (None, 1) and (None, 64) are incompatible error whnever I want to fit the model but I have no idea why. Much thanks.

Upvotes: 1

Views: 1354

Answers (1)

NotAName
NotAName

Reputation: 4322

The loss function is expecting a tensor of shape (None, 1) but you give it (None, 64). You need to add a Dense layer at the end with a single neuron which will get the final results of the calculation:

model = Sequential()
model.add(Dense(512, activation='relu', input_dim=input_d))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu', input_dim=input_d))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1, activation='softmax'))

Upvotes: 3

Related Questions