Data ScienceDeep learning and computer vision

RNNs and LSTMs through a sequence prediction task

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
In this article (5 sections)

A recurrent neural network updates a hidden state as it reads a sequence. Repeated multiplication through many steps can make early information and gradients shrink. LSTM-style gates create a path that can retain or forget state more selectively. A small memory task makes this mechanism visible before a large application hides it.

A fixed-cell diagnostic

The local lab creates 400 length-30 sequences. The first value encodes a binary label; later values contain small noise. It compares two authored scalar recurrences without training them.

  • Vanilla state multiplies prior memory by 0.5 at each step.
  • Gated state retains prior memory with a fixed factor of 0.98.
python
from deep_learning_cases import sequence_case

result = sequence_case()
assert result["sequence_length"] == 30
assert result["gated_initial_signal_multiplier"] > result["vanilla_initial_signal_multiplier"]
print(result["vanilla_fixed_cell_accuracy"], result["gated_fixed_cell_accuracy"])

After 29 transitions, the vanilla initial-signal multiplier is about 1.86e-9; the gated multiplier is 0.557. On this seeded task, fixed-cell accuracy is 48.8% for vanilla recurrence and 100% for the gated recurrence.

These results illustrate memory under chosen parameters. The cells are not trained, and the table is not an architecture benchmark. A learned RNN could choose different dynamics; an LSTM can also fail through poor data, optimization or gate settings.

Define the sequence task

State the input at each step, prediction point, target and permitted context. Split by entity or time so related sequence fragments do not cross partitions. Mask padding, and decide how truncation affects early or late information.

Retain simple baselines: last observation, pooled features, logistic regression over summary statistics, or a one-dimensional convolution. A recurrent model should improve a relevant metric enough to justify sequential latency and complexity.

Inspect memory and errors

Evaluate by sequence length and by the distance between informative evidence and prediction. Track gradient norms and gate activations in a trained model. Create controlled probes that move the relevant signal earlier. If performance falls only for long gaps, the aggregate score may hide a memory limitation.

LSTMs do not “understand context” by virtue of their name. Their outputs remain conditional on the training population and objective. The Data Science course connects recurrent equations with masking, evaluation and deployment constraints.

Exercise

Train a small RNN and LSTM on the same first-signal task at lengths 10, 30 and 100. Repeat seeds, compare with a summary-feature baseline, and report accuracy against signal distance and parameter count.

Continue learning

This article is part of the Deep learning and computer vision sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

References: PyTorch RNN API and PyTorch LSTM API.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.

Explore Data Science
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.