Initializing LSTM hidden state Tensorflow/Keras
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
In Keras, an LSTM has two state tensors: the hidden state h and the cell state c. If you do nothing, Keras initializes both to zeros. That default is correct for many models, but you can also provide an explicit initial state when you need more control.
The Default Behavior
For a regular tf.keras.layers.LSTM, the initial hidden and cell states are zero tensors with the right batch size and unit count. This is what happens in ordinary sequence models:
You do not have to initialize anything manually unless your task specifically depends on nonzero or carried-over state.
That is why many introductory Keras examples never mention hidden-state initialization at all: the default zero state is already built into the layer behavior.
Passing an Explicit Initial State
If you want to control the starting state, pass initial_state when calling the layer. The LSTM expects:
- one tensor for hidden state
h - one tensor for cell state
c
Example:
The batch dimension and unit dimension of the initial state must match the LSTM configuration and the current input batch.
Stateful LSTMs Are Different
If you create the layer with stateful=True, Keras carries state across batches instead of resetting it automatically every call:
This is useful for streaming or chunked sequence processing, but it adds constraints:
- batch size must stay fixed
- you must reset state deliberately when sequence boundaries matter
You can reset with:
That is often more important than custom initialization itself.
Using Encoder State in Sequence Models
One common advanced use case is encoder-decoder modeling. The encoder produces a final hidden and cell state, and the decoder starts from those states:
This is a real reason to initialize LSTM state manually, and it shows why the API accepts both h and c.
It also illustrates that manual initialization is usually about information flow between model components, not about randomizing the start of training.
When Manual Initialization Helps
Manual state initialization is useful when:
- carrying context from one model component to another
- continuing sequence processing across chunks
- injecting learned or computed initial conditions
It is not required just to "help convergence" in ordinary models. For many tasks, zero initialization is the standard and correct choice.
Common Pitfalls
The biggest mistake is passing only one state tensor. Keras LSTM expects both hidden state and cell state.
Another mistake is forgetting batch-size alignment. If the input batch has shape (32, ...), the initial states must also start with batch size 32.
A third issue is using stateful=True without resetting state between logically separate sequences, which leaks information across examples.
Summary
- Keras LSTMs default to zero-initialized hidden and cell states.
- Pass
initial_state=[h0, c0]when you need explicit control. - Stateful LSTMs persist state across batches and require deliberate resets.
- Manual initialization is especially useful for encoder-decoder or streaming setups.
- In many ordinary models, the default zero state is already the right initialization.
Related reading
- Initializing tensorflow Variable with an array larger than 2GB
- Input 0 is incompatible with layer flatten_2 expected min_ndim3, found ndim2
- Input 0 of layer lstm_5 is incompatible with the layer expected ndim3, found ndim2
- Input Permutations in Feed-Forward Neural Networks
- Input 0 of layer conv1d is incompatible with the layer expected min_ndim3, found ndim2. Full shape received None, 30
- Input images with dynamic dimensions in Tensorflow-lite
- Input to LSTM network tensorflow
- Input to reshape is a tensor with 37632 values, but the requested shape has 150528
.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.