2017-07-06 206 views
1
model = Sequential() 

    model.add(Embedding(630, 210)) 
    model.add(LSTM(1024, dropout = 0.2, return_sequences = True)) 
    model.add(LSTM(1024, dropout = 0.2, return_sequences = True)) 
    model.add(Dense(210, activation = 'softmax')) 

    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 

    filepath = 'ner_2-{epoch:02d}-{loss:.5f}.hdf5' 
    checkpoint = ModelCheckpoint(filepath, monitor = 'loss', verbose = 1, save_best_only = True, mode = 'min') 
    callback_list = [checkpoint] 

    model.fit(X, y , epochs = 20, batch_size = 1024, callbacks = callback_list)  

X:輸入矢量是形狀(204564,630,1)keras LSTM模型的輸入和輸出的尺寸不匹配

Y的:目標矢量是形狀的(204564,210,1)

即,每630個投入210個輸出已經被預測但代碼拋出上編譯以下錯誤

ValueError        Traceback (most recent call last) 
<ipython-input-57-05a6affb6217> in <module>() 
    50 callback_list = [checkpoint] 
    51 
---> 52 model.fit(X, y , epochs = 20, batch_size = 1024, callbacks = callback_list) 
    53 print('successful') 



ValueError: Error when checking model input: expected embedding_8_input to have 2 dimensions, but got array with shape (204564, 630, 1) 

請別人解釋爲什麼這個錯誤發生,以及如何解決這個

+3

你有一個尺寸太多了,爲什麼它(204564,630,1)而不僅僅是(204564,630)? –

+0

實際上 - 對於630個序列元素中的每一個,您都有210個預測中的一個。您對整個序列有210個預測。你能詳細說明你的'y'代表什麼嗎? –

+0

它似乎也錯誤地定義了「嵌入」層。你的詞彙量和你想要的嵌入暗淡是什麼? –

回答

1

該消息表示:

你的第一層期待與2個維度的輸入:(BATCHSIZE,SomeOtherDimension)。但是你的輸入有三個維度(BatchSize = 204564,SomeOtherDimension = 630,1)。

嗯......從您輸入刪除1,或重塑它的模型內部:

解決方案1 ​​ - 從輸入中刪除它:

X = X.reshape((204564,630)) 

解決方案2 - 添加一個重塑層:

model = Sequential() 
model.add(Reshape((630,),input_shape=(630,1))) 
model.add(Embedding.....)