2017-04-05 3 views
4

나는 keras와 python에있어서 매우 새로운 기능을 가지고 있습니다. 시퀀스 길이가 다른 시계열 데이터 세트가 있습니다 (예 : 첫 번째 시퀀스는 484000x128, 두 번째 시퀀스는 563110x128 등) 시퀀스를 3D 배열에 넣었습니다.다른 시퀀스가있는 keras에서 lstm 입력 모양 이해하기

제가 혼란스러워서 제 질문은 입력 모양을 정의하는 방법입니다. DL4J를 사용하고 있었지만 개념은 네트워크 구성을 정의 할 때 다른 점입니다. 당신이 일정한 크기의 시간 단계를 필요로하기 위하여려고하고있다 있음을 의미한다

Input shapes

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

:

여기
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의 입력 형태에 문서입니다 : 여기

내 첫 시험 코드 각 배치마다.

이 다음 시도 할 수 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을 사용해야합니다. 이는 배치 크기가 하나의 tilmestep (시퀀스)임을 의미합니까? ValueError : 입력 값 0은 레이어 lstm_1과 호환되지 않습니다 : expected ndim = 3, found ndim = 4 –

+1

이 주석은 매우 일반적입니다 (예 : input_shape = (1, timestep, dims)). 문제가 있으며 어떤 종류의 응답이 있어야합니다. 그렇지 않은 경우 대답을 업데이트해야합니다. –

+0

@ NathanMcCoy 다시 돌아 오지 못해 죄송합니다. 나는 이걸 보려고 현재는 시간을 잰 적이 없지만이 글을 읽으십시오. https://machinelearningmastery.com/use-different-batch-sizes-training-predicting-python-keras/ 나는 최대한 빨리 함께하려고 노력합니다. – putonspectacles

관련 문제