簡體   English   中英

神經網絡尺寸失配

[英]Neural network dimension mis-match

我在Keras中為MNIST數字數據集設置了一個神經網絡,如下所示:

input_size = features_train.shape[1]
hidden_size = 200
output_size = 9
lambda_reg = 0.2
learning_rate = 0.01
num_epochs = 50
batch_size = 30

model = Sequential()
model.add(Dense(input_size, hidden_size, W_regularizer=l2(lambda_reg), init='normal'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))

model.add(Dense(hidden_size, output_size, W_regularizer=l2(lambda_reg), init='normal'))
model.add(Activation('softmax'))

sgd = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)

history = History()

model.fit(features_train, labels_train, batch_size=batch_size, nb_epoch=num_epochs, show_accuracy=True, verbose=2, validation_split=0.2, callbacks=[history])
score = model.evaluate(features_train, labels_train, show_accuracy=True, verbose=1)
predictions = model.predict(features_train)
print('Test score:', score[0])
print('Test accuracy:', score[1])

features_train的形狀為(1000,784),labels_train的形狀為(1000,1),兩者均為numpy數組。 我想要784個輸入節點,200個隱藏節點和9個輸出來對數字進行分類

我不斷收到輸入尺寸不匹配錯誤:

Input dimension mis-match. (input[0].shape[1] = 9, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{Sub}[(0, 0)](AdvancedSubtensor1.0, AdvancedSubtensor1.0)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(30L, 9L), (30L, 1L)]
Inputs strides: [(36L, 4L), (4L, 4L)]
Inputs values: ['not shown', 'not shown']

我正在嘗試確定尺寸可能不正確的地方,但沒有看到。 誰能看到這個問題?

很久以來,我一直在訓練2類分類模型,以至於我習慣於處理僅僅是單個值的標簽。 對於這個問題(對多個結果進行分類),我只需要將標簽更改為向量本身即可。

這解決了我的問題:

from keras.utils.np_utils import to_categorical

labels_train = to_categorical(labels_train)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM