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.add(LSTM(3,batch_input_shape =(n_batch,trainX.shape[1], trainX.shape[2]),stateful=True))
model.add(Dense(1))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="adam")
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&lt;


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.