2017-05-16 133 views
2

我是Keras的新手,我想創建我的網絡,需要在紙牌遊戲上學習。它需要93個二進制輸入,一個隱藏層有40個神經元和一個輸出神經元,計算得分(從0到25)。Keras輸入形狀錯誤

model = Sequential() 
model.add(Dense(input_dim=93, units=40, activation="sigmoid")) 
model.add(Dense(units=2, activation="linear")) 
sgd = optimizers.SGD(lr=0.01, clipvalue=0.5) 
model.compile(loss="mse", optimizer=sgd, learning_rate=0.01) 

我想先來計算(做正向傳播)的93個輸入

這是 「s.toInputs()」

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1]

model.predict(np.array(s.toInputs()) 

,但我得到錯誤

ValueError: Error when checking : expected dense_1_input to have shape (None, 93) but got array with shape (93, 1)

我該怎麼辦ss正確的參數?

回答

1

其實s.toInputs()應該是這樣的

[[0,0,0, etc...],[0,1,0, etc...]]

基本上,你必須有一個與以下形狀的陣列:(n_batchesn_attributes

您有93個屬性,所以如果您使用張量流,這應該做的伎倆

np.array(s.toInputs()).reshape(-1, 93) 

工作的完整示例

model = Sequential() 
model.add(Dense(input_dim=93, units=40, activation="sigmoid")) 
model.add(Dense(units=2, activation="linear")) 
sgd = optimizers.SGD(lr=0.01, clipvalue=0.5) 
model.compile(loss="mse", optimizer=sgd, learning_rate=0.01) 

# random data 
n_batches = 10 
data = np.random.randint(0,2,93*n_batches) 
data = data.reshape(-1,93) 

model.predict(data) 
+0

我仍然得到相同的錯誤(我打印數組,它就像你說的),也許我的模型是錯誤的?我的數組只包含網絡的93個輸入,什麼是n_samples和n_attributes? – Ggs

+0

你使用theano還是tensorflow? –

+0

我使用theano – Ggs

1

錯誤消息告訴您,您的數據需要有形狀(None, 93)None在這裏意味着,這個尺寸可以有任意值。這是你的樣本數)

但輸入數據有形狀(93,1)。請注意,尺寸是相反的。 可以使用轉來獲得正確的形狀數據:

model.predict(np.array(s.toInputs()).T) 
+0

我試過了,但仍然收到相同的錯誤。 – Ggs

-1

整形東西效果很好。 將數組重新整形(batch_size,your_input_dimensions)

+0

代碼片段可以改進這個答案。 –