2017-04-05 33 views
4

私はkerasとpythonにはとても新しいです。 シーケンスの長さが異なる時系列データセットがあります(たとえば、1番目のシーケンスは484000x128、2番目のシーケンスは563110x128など) シーケンスを3D配列に入れました。異なるシーケンスのkerasでlstmの入力形状を理解する

私は混乱しているので、私の質問はどのように入力形状を定義するかです。私はDL4Jを使用していましたが、コンセプトはネットワーク構成の定義が異なります。ここで

は私の最初のトライアルコードです:ここで

import numpy as np 
from keras.models import Sequential 
from keras.layers import Embedding,LSTM,Dense,Dropout 


## Loading dummy data 
sequences = np.array([[[1,2,3],[1,2,3]], [[4,5,6],[4,5,6],[4,5,6]]]) 
y = np.array([[[0],[0]], [[1],[1],[1]]]) 
x_test=np.array([[2,3,2],[4,6,7],[1,2,1]]) 
y_test=np.array([0,1,1]) 

n_epochs=40 

# model configration 
model = Sequential() 
model.add(LSTM(100, input_shape=(3,1), activation='tanh', recurrent_activation='hard_sigmoid')) # 100 num of LSTM units 
model.add(LSTM(100, activation='tanh', recurrent_activation='hard_sigmoid')) 
model.add(Dense(1, activation='softmax')) 
model.compile(loss='binary_crossentropy', 
       optimizer='adam', 
       metrics=['accuracy']) 

print(model.summary()) 

## training with batches of size 1 (each batch is a sequence) 
for epoch in range(n_epochs): 
    for seq, label in zip(sequences, y): 
     model.train(np.array([seq]), [label]) # train a batch at a time.. 
     scores=model.evaluate(x_test, y_test) # evaluate batch at a time.. 

答えて

3

はLSTMsの入力シェイプ上のドキュメントです:

Input shapes

3D tensor with shape (batch_size, timesteps, input_dim), (Optional) 2D tensors with shape (batch_size, output_dim).

もしあなたが一定の大きさとタイムステップを必要としていることを意味します各バッチについて。これを行うための

標準的な方法はkeras's padding utility

のようなものを使用してシーケンスを水増しされて、あなたは試すことができます:

# let say timestep you choose: is 700000 and dimension of the vectors are 128 

timestep = 700000 
dims = 128 

model.add(LSTM(100, input_shape=(timestep, dim), 
     activation='tanh', recurrent_activation='hard_sigmoid')) 

を私はbatch_size引数を除去するための答えを編集しました。この設定では、バッチサイズは指定されていません。モデルをフィッティングするときに設定することができます(model.fit())。

+3

お返事ありがとうございます。私の場合、バッチサイズ= 1を使用する必要があります。これは、バッチサイズが1つのティルメスステップ(シーケンス)であることを意味しますか? ValueError:入力0はレイヤーlstm_1と互換性がありません:期待されるndim = 3、見つかったndim = 4 –

+1

このコメントは非常に一般的です問題があり、何らかの応答が必要です。答えが更新されない場合は、 –

+0

@ NathanMcCoyこれに戻らないことを申し訳ありません。私は現在これを見て時間を持っていないが、これを読んでみてくださいこれはhttps://machinelearningmastery.com/use-different-batch-sizes-training-predicting-python-keras/私は一緒にできるだけ一緒にしようとする – putonspectacles

関連する問題