keras-team/keras · error · ValueError
If an RNN is stateful, the batch size of the input sequences
Error message
If an RNN is stateful, the batch size of the input sequences must be the same as the batch size of the initial state.
- Expected batch size: {self._expected_batch_size}
- Received batch size: {actual_batch_size} What it means
Error "If an RNN is stateful, the batch size of the input sequences must be the same as the batch size of the initial state. - Expected batch size: {self._expected_batch_size} - Received batch size: {actual_batch_size}" thrown in keras-team/keras.
Source
Thrown at keras/src/layers/rnn/rnn.py:396
"the time dimension by passing a `shape` "
"or `batch_shape` argument to your `Input()`."
)
if initial_state is None:
if self.stateful:
initial_state = self.states
else:
initial_state = self.get_initial_state(
batch_size=ops.shape(sequences)[0]
)
if self.stateful:
actual_batch_size = sequences.shape[0]
if (
self._expected_batch_size is not None
and actual_batch_size is not None
and actual_batch_size != self._expected_batch_size
):
raise ValueError(
f"If an RNN is stateful, the batch size of the "
f"input sequences must be the same as the batch "
f"size of the initial state. \n"
f"- Expected batch size: {self._expected_batch_size}\n"
f"- Received batch size: {actual_batch_size}"
)
# RNN expect the states in a list, even if single state.
if not tree.is_nested(initial_state):
initial_state = [initial_state]
initial_state = list(initial_state)
# Cast states to compute dtype.
# Note that states may be deeply nested
# (e.g. in the stacked cells case).
initial_state = tree.map_structure(
lambda x: backend.convert_to_tensor(
x, dtype=self.cell.compute_dtypeView on GitHub (pinned to 7a34a03db6)
When it happens
Trigger: Thrown at keras/src/layers/rnn/rnn.py:396 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/9e679c5083abb843.
Report an issue: GitHub.