使用Keras顺序编译时的ValueError()

时间:2017-05-06 20:23:20

标签: python python-3.x machine-learning neural-network keras

我正在尝试使用Keras模块使用Sequential()编译数据集,但是我收到了值错误:

ValueError: Error when checking model input: expected dense_input_1 to have shape (None, 33) but got array with shape (32, 36)

我多次查看我的代码,但找不到任何可能的错误。

我有一个包含32个项目的数据集,所有这些项目都被转换为浮点数。

这是我神经网络的代码:

# Build neural network
# Sequential
model = Sequential()

# Neural network
model.add(Dense(36, input_dim=34, init='uniform', activation='sigmoid' ))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(33, init='uniform', activation='sigmoid'))

# Compile model
model.compile(loss='mean_squared_logarithmic_error', optimizer='SGD', metrics=['accuracy'])

# Fit model
history = model.fit(X, Y, nb_epoch=20, validation_split=0.2, batch_size=3)

以下是我收到的完整错误消息:

Traceback (most recent call last):
  File "/Users/cliang/Desktop/Laurence/Python/Programs/Python/Collaborative_Projects/Cancer_screening/neural_network_alls_1.py", line 111, in <module>
    history = model.fit(X, Y, nb_epoch=20, validation_split=0.2, batch_size=3)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/models.py", line 672, in fit
    initial_epoch=initial_epoch)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1116, in fit
    batch_size=batch_size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1029, in _standardize_user_data
    exception_prefix='model input')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 124, in standardize_input_data
    str(array.shape))
ValueError: Error when checking model input: expected dense_input_1 to have shape (None, 34) but got array with shape (32, 36)

1 个答案:

答案 0 :(得分:1)

如错误所示,您的输入数据形状与第一层之间存在未匹配。您可以清楚地定义输入维度是(要素数量)是34 input_dim=34,尽管您传递的数据包含36个要素。

我认为您在隐藏图层36的神经元数量与输入数据之间存在混淆34.要么从数据中删除两列,要么更改input_dim=36