私は、シーケンス内の次の要素を予測するKerasを使用してLSTMでいくつかのバニラパターン認識を行うことをしようとしています。Kerasで可変長入力LSTMを作成するにはどうすればよいですか?
私のデータは次のようになります。トレーニングシーケンスのラベルはリストの最後の要素である
:X_train['Sequence'][n][-1]
。
私Sequence
列はシーケンスの要素の可変数を持つことができるので、私はRNNを使用するのに最適なモデルであると信じます。以下はKerasでLSTMを構築するための私の試みです:
# Build the model
# A few arbitrary constants...
max_features = 20000
out_size = 128
# The max length should be the length of the longest sequence (minus one to account for the label)
max_length = X_train['Sequence'].apply(len).max() - 1
# Normal LSTM model construction with sigmoid activation
model = Sequential()
model.add(Embedding(max_features, out_size, input_length=max_length, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
そして、ここでは、私は私のモデルを訓練しようと方法は次のとおりです。
# Train the model
for seq in X_train['Sequence']:
print("Length of training is {0}".format(len(seq[:-1])))
print("Training set is {0}".format(seq[:-1]))
model.fit(np.array([seq[:-1]]), [seq[-1]])
は私の出力は次のとおりです。ただし
Length of training is 13
Training set is [1, 3, 13, 87, 1053, 28576, 2141733, 508147108, 402135275365, 1073376057490373, 9700385489355970183, 298434346895322960005291, 31479360095907908092817694945]
、私は次のエラーが表示されます。
Exception: Error when checking model input: expected embedding_input_1 to have shape (None, 347) but got array with shape (1, 13)
I私の訓練のステップが正しく設定されていると信じて、私のモデル構築が間違っていなければならない。 347はmax_length
です。
どのように私が正しくKerasに可変長の入力LSTMを構築することができますか?私はデータをパッドしたくないと思う。関連性があるかどうかはわかりませんが、私はTheanoバックエンドを使用しています。
私はinput_shapeを指定し、任意の長さのためにどれを使用していないことで、この作業を取得するために管理。 – Benjamin
おそらく私自身の質問があなたの役に立つかもしれません:http://stackoverflow.com/questions/38265922/recurrent-convolutional-blstm-neural-network-arbitrary-sequence-lengths – Benjamin