Time-series data
stateful LSTM
machine learning
neural networks
data preprocessing

Proper way to feed time-series data to stateful LSTM?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A stateful LSTM keeps its hidden state across batches instead of resetting after every batch automatically. That can help when a long sequence is split across multiple training windows, but it also means your input order, batch size, and state-reset strategy have to be consistent or the model will learn from the wrong continuity.

What Makes a Stateful LSTM Different

In a stateless LSTM, each batch is treated as independent. Hidden state is cleared between batches, so the model only remembers information inside the current sequence window.

In a stateful LSTM, sample i in one batch is assumed to continue as sample i in the next batch. That is the rule that drives almost every data-preparation decision.

Because of that rule:

  • batch size must stay fixed during training
  • sequence order must be preserved
  • shuffling must be disabled
  • states must be reset at real sequence boundaries

If those conditions are not true, the model carries state from unrelated sequences and training becomes misleading.

Shape the Data as Consecutive Windows

Keras expects LSTM input in the form samples, timesteps, features. With a stateful model, those samples cannot be arbitrary shuffled windows. They need to preserve the continuity you want the network to remember.

A simple univariate series can be converted into fixed windows like this:

python
1import numpy as np
2
3
4def make_windows(series: np.ndarray, window: int):
5    xs = []
6    ys = []
7    for start in range(len(series) - window):
8        xs.append(series[start:start + window])
9        ys.append(series[start + window])
10    x = np.array(xs, dtype=np.float32)
11    y = np.array(ys, dtype=np.float32)
12    return x[..., np.newaxis], y
13
14
15series = np.arange(20, dtype=np.float32)
16X, y = make_windows(series, window=4)
17print(X.shape)  # (16, 4, 1)
18print(y.shape)  # (16,)

This creates overlapping windows. For a stateful model, the crucial step is batching those windows in an order that preserves continuity instead of shuffling them randomly.

Build the Model with a Fixed Batch Size

In Keras, stateful LSTMs need a fixed batch size at build time. One straightforward way is to create an Input layer with batch_shape.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4
5series = np.sin(np.linspace(0, 20, 200)).astype(np.float32)
6X, y = [], []
7window = 10
8for start in range(len(series) - window):
9    X.append(series[start:start + window])
10    y.append(series[start + window])
11X = np.array(X)[..., np.newaxis]
12y = np.array(y)
13
14batch_size = 5
15usable = (len(X) // batch_size) * batch_size
16X = X[:usable]
17y = y[:usable]
18
19model = keras.Sequential([
20    keras.layers.Input(batch_shape=(batch_size, window, 1)),
21    keras.layers.LSTM(16, stateful=True),
22    keras.layers.Dense(1)
23])
24model.compile(optimizer="adam", loss="mse")
25
26for epoch in range(3):
27    model.fit(X, y, epochs=1, batch_size=batch_size, shuffle=False, verbose=1)
28    model.reset_states()

Two details matter here:

  • the dataset length is trimmed so it divides evenly by batch_size
  • 'shuffle=False preserves sequence order'

If you let the last batch be smaller or you shuffle the windows, the hidden state alignment is broken.

When to Reset State

State should be reset when continuity ends, not just whenever you feel like clearing memory.

For one long time series, resetting at the end of each epoch is common because you are about to start the same series again from the beginning.

For multiple independent sequences, reset state between sequences. If one stock price series ends and a completely different sensor stream starts, carrying the old hidden state into the new stream is usually wrong.

A useful question is: should sample i in the next batch logically continue sample i in the previous batch? If not, reset the state.

When Stateful LSTM Is the Wrong Tool

Many problems do not need stateful training at all. If each training example already contains enough context inside its own window, a stateless LSTM is simpler and easier to train.

Stateful LSTMs are best when your data is truly sequential across batch boundaries and you want the model to retain that continuity. If your training set is just many independent windows from many unrelated sequences, stateful behavior often adds complexity without benefit.

Common Pitfalls

Shuffling training windows is the most common mistake. A stateful LSTM assumes batch order has meaning.

Using a dataset size that is not divisible by batch size also causes trouble because the last batch no longer matches the fixed state layout.

Forgetting to reset states between independent sequences leaks information from one sequence into another.

Finally, do not choose stateful mode just because it sounds more powerful. If the sequence continuity does not cross batch boundaries, a stateless model is usually easier to debug and just as effective.

Summary

  • a stateful LSTM carries hidden state from one batch to the next
  • batch size must be fixed and sequence order must be preserved
  • use shuffle=False and trim the dataset so batches are evenly sized
  • reset state only at real sequence boundaries, such as between epochs or between independent series
  • if your examples are already self-contained windows, a stateless LSTM is often the better design

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.