Is `RNN` initial state reset for subsequent mini-batches?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Usually, yes: the hidden state is reset between mini-batches unless you explicitly carry it forward. That default is important because most training pipelines treat each batch as an independent collection of sequences, not as one continuous stream.
The Default Mental Model
Inside a single sequence, an RNN updates hidden state from one time step to the next. Across different mini-batches, frameworks generally do not assume continuity unless you ask for it.
So there are two separate questions:
- Does the state persist across time steps within one forward pass? Yes.
- Does the state automatically persist across later mini-batches? Usually no.
That distinction explains a lot of confusion.
Keras: Stateless Versus Stateful
In Keras, the default behavior is stateless. Each batch starts with a fresh initial state unless you pass one manually.
Here the RNN processes each sample across 5 time steps, but it does not carry hidden state from one batch of 4 samples into the next batch.
If you set stateful=True, Keras keeps state across batches for the same sample index position:
With stateful=True, batch ordering matters. Sample position 0 in batch 2 receives the carried state from sample position 0 in batch 1.
PyTorch: Explicit State Passing
PyTorch is explicit about this. The RNN returns the final hidden state, and if you want continuity you pass that state into the next call.
If you omit h1 in the second call, PyTorch starts from zeros by default.
That makes the rule very clear: persistence between mini-batches happens only when your code chooses it.
When Carrying State Is Useful
State carryover is useful when a long sequence is chopped into chunks for efficiency. Language modeling and streaming time-series tasks often work that way.
Example: if one logical sequence is 10,000 time steps long, you may train on chunks of 100 time steps. In that setup, passing the final state of chunk 1 into chunk 2 preserves continuity without forcing one giant forward pass.
But once you do that, you usually also need truncated backpropagation through time. In PyTorch, that often means detaching the hidden state:
Without detaching, the computation graph grows across chunks and memory use becomes a problem.
When Resetting Is The Right Choice
If each training example is independent, resetting state between batches is correct. Carrying state across unrelated samples leaks information and makes the training signal invalid.
That is why the default reset behavior is sensible for classification, many forecasting tasks with separate windows, and datasets where batches are shuffled.
Common Pitfalls
The biggest mistake is enabling stateful behavior while still shuffling data randomly. If the next batch is unrelated to the previous one, carried state is harmful, not helpful.
Another common error is forgetting that stateful Keras models assume a fixed batch size and consistent sample ordering. If either changes, the mapping of states to samples breaks.
In PyTorch, developers sometimes pass hidden state across chunks but forget to detach it. That causes unnecessary graph growth and can lead to memory issues.
Finally, do not confuse resetting between batches with resetting between epochs. Those are separate choices in your training loop.
Summary
- By default, RNN state is usually reset between mini-batches.
- Keras keeps state across batches only if you use
stateful=Trueor pass state manually. - PyTorch keeps state only when you pass the returned hidden state into the next call.
- Carry state forward only when batches represent consecutive chunks of the same logical sequence.
- If batches are independent, resetting state is the correct behavior.
Related reading
- Is sparse tensor multiplication implemented in TensorFlow?
- Is Tensorflow 1.12 compatible with CUDA 10.1?
- Is tensorflow lazy?
- Is TensorFlow only limited to neural networks?
- Is scikit-learn suitable for big data tasks?
- Is Session.runfetches guaranteed to execute its fetches arguments in-order?
- Is TensorFlow suitable for Recommendation Systems
- Is the bias node necessary in very large neural networks?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.