2016-07-07 20 views
1

私はKerasの単語レベルで言語モデルを訓練しようとしています。Keras LSTM/GRU言語モデルの入力形状

私は、このモデルに適合しようとすると、私は両方の形状(90582L、517L)

で、私のXとYを持っている:

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) 

私は入力形状がどんなものであるべきかについていくつかの指針が必要ですか?私はあらゆる種類の組み合わせで試行錯誤しましたが、何か基本的なことを誤解しているようです。

ケラステキスト生成の例では、X行列の次元は3つです。私は第3の次元がどんなものであろうと考えていません。

答えて

3

これは実行しようとしている内容によって異なります。私はあなたの形状データ(90582,517)はそれぞれ517ワードの90582サンプルのセットだと思います。そうであれば、単語を意味のあるものにするために単語を単語ベクトル(=埋め込み)に変換する必要があります。次に、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) 
関連する問題