2016-09-02 8 views
2

keras에 상태 저장 LSTM을 생성하려고합니다. 나는 다음과 같은 명령어를 주었다 :Keras 상태 저장 LSTM 오류

model.add(LSTM(300,input_dim=4,activation='tanh',stateful=True,batch_input_shape=(19,13,4),return_sequences=True)) 

배치 사이즈 = 19. 그러나 실행에 내가 내 스크립트에서 아무 곳이나 배치 크기 (32)를 지정하지 않은 오류를

Exception: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 8816 samples. Batch size: 32. 

를 제공하고 19 8816

답변

2

동적으로 크기를 데이터와 배치 :

크기 데이터 및 교육 샘플 분할 : 배치 크기에 훈련 샘플의

data_size = int(len(supervised_values)) 
train_size_initial = int(data_size * train_split) 
x_samples = supervised_values[-data_size:, :] 

크기 번호 :

if train_size_initial < batch_size_div: 
    batch_size = 1 
else: 
    batch_size = int(train_size_initial/batch_size_div) 
train_size = int(int(train_size_initial/batch_size) * batch_size) # provide even division of training/batches 
val_size = int(int((data_size - train_size)/batch_size) * batch_size) # provide even division of val/batches 
print('Data Size: {} Train Size: {} Batch Size: {}'.format(data_size, train_size, batch_size)) 

열차 및 유효성 검사 세트로 데이터 분할

train, val = x_samples[0:train_size, 0:-1], x_samples[train_size:train_size + val_size, 0:-1] 
6

model.fit() (예를 들어 model.train_on_batch 반대) 일괄 수행으로 나눌 수있다. 따라서 batch_size 매개 변수의 기본값은 32입니다.

입력 배치 크기로 변경하면 예상대로 작동합니다.

예 :

batch_size = 19 

model = Sequential() 
model.add(LSTM(300,input_dim=4,activation='tanh',stateful=True,batch_input_shape=(19,13,4),return_sequences=True)) 

model.fit(x, y, batch_size=batch_size) 
+0

시도했습니다. 그러나 다시 동일한 오류가 발생합니다 –

+0

그런 다음 오류를 복제 한 최소한의 작동 예제로 질문을 업데이트해야합니다. – nemo