简体   繁体   English

ValueError: Shapes (None, 1) 和 (None, 64) 是不兼容的 Keras

[英]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.我有 32 个特征作为输入维度,这是一个分类问题。 this is the result of the summary :这是总结的结果: 在此处输入图片说明

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.测试和训练形状都是 32。我得到 ValueError: Shapes (None, 1) and (None, 64) are incompatible error when I want to fit the model 但我不知道为什么。 Much thanks.非常感谢。

The loss function is expecting a tensor of shape (None, 1) but you give it (None, 64) .损失函数需要一个形状为(None, 1)的张量,但你给它(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:您需要在最后添加一个带有单个神经元的 Dense 层,这将获得最终的计算结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM