2017-10-10 130 views
0

我正在嘗試使用LSTM來訓練一個簡單的多對一RNN分類器。我的時間步長爲100個數據點,具有7個特徵,總共有192382個樣本。這是我的型號:多對一的LSTM,Keras上的softmax尺寸錯誤

model = Sequential() 
model.add(LSTM(50,input_shape = (100,7),name = 'LSTM',return_sequences=False)) 
model.add(Dropout(0.2)) 
model.add(Dense(3, activation='softmax',name = 'softmax_layer')) 
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'],name='softmax') 
model.fit(datax,datay,epochs=25,batch_size=128) 
model.summary() 

該模型編譯罰款沒有錯誤,但我不能適應模型。這是它返回的錯誤:

ValueError: Error when checking target: expected softmax_layer to have shape (None, 3) but got array with shape (192282, 100) 

有沒有人有一個想法,爲什麼softmax圖層返回一個(192282,100)矩陣? LSTM層中的return_sequence = False是否應該只給每個時間步輸出一個輸出?

+0

假設你正在分類至3班,你傳遞DATAY錯誤的值 – ilan

回答

0

實際上,softmax_layer回報率(無,3),因爲最後一層的尺寸爲3

也許你要修復它?所以,爲了解決它,你需要輸出層(softmax_layer)的大小應該等於你的標籤數組的大小(datay.shape [1])。換句話說,它必須等於類的數量。

快速的解決方法是:

model = Sequential() 
model.add(LSTM(50,input_shape = (100,7),name = 'LSTM',return_sequences=False)) 
model.add(Dropout(0.2)) 
model.add(Dense(datay.shape[1], activation='softmax',name = 'softmax_layer')) 
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'],name='softmax') 
model.fit(datax,datay,epochs=25,batch_size=128) 
model.summary()