# Mini batch size and reset states

3 is a big file, but I would like to reset the state after mini_batch_size of 50.

n_epoch=10000
n_batch=50

# create and fit the LSTM network
model = Sequential()
model.summary()

#fitting model
for i in range(n_epoch):
history=model.fit(trainX, trainY, epochs=1, batch_size=n_batch,verbose=2, shuffle=False)
model.reset_states()


I am getting the error:

value error:  In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 63648 samples<


How can I train a LSTM in mini_batch size of 50 (a number which is not divisible by trainX)?

You can simply make your training data a shape that is divisible by 50. In [1]: sample_size = 63648

In [2]: to_remove = sample_size % 50

In [3]: end_index = sample_size - to_remove

In [4]: model.fit(trainX[:end_index], trainY[:end_index], ...


What are the dimensions of trainX and trainY? You are not specifying a validation dataset, is that intentional?

You say you want to reset the state after a mini-batch, but you are resetting the state after each epoch (one loop through your entire dataset).

One more comment: the n_epochs = 10000 seems excessive.

• trainX=(63648,3,1), trainY=(6245,1) .I removed validation_split. Actually it was giving error when i set n_batch=len(trainX). I did the above changes..set n_epoch=1300 as i want to cover the full train data with batch size of 50. model is runing. how can i set state after each mini_batch? – Hazel Jun 30 '18 at 16:26
• like as you said if i am doing using for loop and setting epoch=1, will it see entire dataset in each epoch or it will see only mini batch in each epoch? – Hazel Jun 30 '18 at 16:28
• It will reset the state after each single epoch. I only mention is because you had started by saying you want to reset after each mini-batch. But that would basically be the same as just setting stateful=False. – n1k31t4 Jun 30 '18 at 16:50
• ok .so that means if i use mini_batch size and set stateful = false.it will automatically reset the states after each mini batch. next error i am getting ,,...if i set validation split =0.1%. model is not runing. Error: cannot feed input of(40,3,1) into an lstm of(50,3,1). what should i do now? How to edit if want validation split as well? – Hazel Jun 30 '18 at 17:07
• hello.. by using the method you proposed my dataset is chopped.. now i have 63000 in train X. so i lost 648.. i dont want it like this. it is a time series data and i want all the data in series. if i will chop then i will lose the information. so i think this is not correct method for my problem. Thank you for the solution. can you please suggest something else? – Hazel Jun 30 '18 at 21:08