Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras stateful LSTM error

Tags:

keras

lstm

I want to create stateful LSTM in keras. I gave it a command like this:

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

where batch size=19. But on running it gives error

 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.

I did not specify batch size 32 anywhere in my script and 19 is divisible by 8816

like image 870
shaifali Gupta Avatar asked Nov 28 '22 13:11

shaifali Gupta


1 Answers

model.fit() does the batching (as opposed to model.train_on_batch for example). Consequently it has a batch_size parameter which defaults to 32.

Change this to your input batch size and it should work as expected.

Example:

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)
like image 103
nemo Avatar answered Jan 04 '23 22:01

nemo