0
我試圖編譯順序()與Keras模塊的數據集,但我得到一個值誤差:ValueError異常
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)
34是輸入向量的長度,36是第一個緻密層中的神經元數(因此是該層輸出的長度),而不是隱藏層的數量。 –
是的,一個錯字!我正在編輯,謝謝 –
@ML_TN謝謝!我最終發現兩件事情出錯了。錯誤出現在那裏,因爲沒有隻有一個神經元的輸出層(這使得程序無誤地運行)。其次,正如你所說,輸入數據與第一層(我使用錯誤文件中的數據)之間存在不匹配。感謝您的幫助@ML_TN! – Larry