2016-07-07 120 views
1

我想在Keras的詞級上訓練語言模型。Keras LSTM/GRU語言模型的輸入形狀

我有我的X和Y,都與形狀(90582L,517L)

當我試圖適應這種模式:

print('Build model...') 
model = Sequential() 
model.add(GRU(512, return_sequences=True, input_shape=(90582, 517))) 
model.add(Dropout(0.2)) 
model.add(GRU(512, return_sequences=True)) 
model.add(Dropout(0.2)) 
model.add(TimeDistributedDense(1)) 
model.add(Activation('softmax')) 
model.compile(loss='categorical_crossentropy', optimizer='rmsprop') 
model.fit(x_pad, y_pad, batch_size=128, nb_epoch=2) 

我得到的錯誤:

Exception: Error when checking model input: 
expected gru_input_7 to have 3 dimensions, but got array with shape (90582L, 517L) 

我需要一些關於輸入形狀應該是什麼的指導?我對各種組合進行了嘗試和錯誤,但似乎我誤解了一些根本性的東西。

在Keras文本生成示例中,X矩陣有3個維度。我不知道第三維應該是什麼。

回答

3

這取決於你想要做什麼。我想你的形狀數據(90582,517)是一組90582個樣本,每個樣本517個字。如果是這樣,你必須將你的單詞轉化爲單詞向量(= embeddings)以使它們變得有意義。然後你將得到GRU可以處理的形狀(90582,517,embedding_dim)。

Keras Embedding layer可以爲你做到這一點。將其添加爲您的神經網絡的第一層之前的第一個GRU層。

vocabulary_size = XXXXX  # give your vocabulary size here (largest word ID in the input) 
embedding_dim = XXXX  # give your embedding dimension here (e.g. 100) 

print('Build model...') 
model = Sequential() 
model.add(Embedding(vocabulary_size, embedding_dim, input_shape=(90582, 517))) 
model.add(GRU(512, return_sequences=True)) 
model.add(Dropout(0.2)) 
model.add(GRU(512, return_sequences=True)) 
model.add(Dropout(0.2)) 
model.add(TimeDistributedDense(1)) 
model.add(Activation('softmax')) 
model.compile(loss='categorical_crossentropy', optimizer='rmsprop') 
model.fit(x_pad, y_pad, batch_size=128, nb_epoch=2)