【问题标题】:ValueError when compiling with Keras Sequential()使用 Keras Sequential() 编译时出现 ValueError
【发布时间】:2017-05-06 20:23:19
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

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


    【解决方案1】:

    正如错误所说,您的输入数据形状和第一层之间存在不匹配。您清楚地定义您的输入维度是(特征数)是 34 input_dim=34,尽管您正在传递具有 36 个特征的数据。

    我认为您混淆了隐藏层 36 的神经元数量和输入数据 34 的数量。要么从数据中删除两列,要么更改 input_dim=36

    【讨论】:

    • 34 是输入向量的长度,36 是第一个密集层中神经元的数量(因此是该层输出的长度),而不是隐藏层的数量。
    • @ML_TN 谢谢!我最终发现了两个问题。出现错误是因为没有一个只有一个神经元的输出层(这使得程序运行没有错误)。其次,正如您所说,输入数据和第一层之间存在不匹配(我使用了错误文件中的数据)。感谢您的帮助@ML_TN!
    猜你喜欢
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多