gpt4 book ai didi

python - 使用 Keras Sequential() 编译时出现 ValueError

转载 作者:行者123 更新时间:2023-11-30 09:51:47 25 4
gpt4 key购买 nike

我尝试使用 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 个项目的数据集,所有这些项目都转换为 float 。

这是我的神经网络的代码:

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

最佳答案

正如错误所述,您的输入数据形状与第一层之间不匹配。尽管您传递的数据具有 36 个特征,但您明确定义输入维度(特征数量)为 34 input_dim=34

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

关于python - 使用 Keras Sequential() 编译时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43825039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com