RNN, GRU and LSTM Sequence Models Explained

MetaCyberGuru Academy

AdvancedEstimated learning effort: 80 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Embeddings and Neural NLP

Recurrent networks process tokens step by step and provide the historical foundation for modern sequence modelling, especially when state and order matter.

A simple RNN updates one hidden state through the sequence. LSTM and GRU cells add gates that help information and gradients persist, although they do not remove every long-range limitation.

Packed batches, padding masks, sequence length and hidden-state shape are frequent implementation errors. Compare with a sparse baseline before accepting neural complexity. Record sequence lengths, masks and baseline results so later architectures are compared on the same workload.

Trace Sequence Information Through Recurrent States

  • Explain hidden states, gates, padding and sequence lengths through their tensor shapes.
  • Pass a padded sequence through a recurrent model and inspect every important tensor shape.
  • Compare recurrent models with a sparse baseline using held-out quality, latency and length-based errors.
  • Inspect tensor shapes, padding masks and gradient behaviour before interpreting the sequence model output.

From Padded Tokens to a Sequence Prediction

Packed batches, padding masks, sequence length and hidden-state shape are frequent implementation errors. Compare with a sparse baseline before accepting neural complexity.

The short batch reveals every tensor dimension and hidden state. Inspect those shapes before training on variable-length data.

Follow Information Through the Sequence

Training loss alone is not enough. Use held-out metrics, inspect length-based errors and record inference latency and memory for the deployment device.

Architecture Choices That Affect Long Context

Falling training loss does not establish useful sequence modelling. Compare held-out errors, latency and long-input behaviour with the sparse baseline.

For RNN, GRU and LSTM Sequence Models Explained, change one setting at a time while the data split, comparison baseline and metric remain fixed.

What to record before scaling RNN, GRU and LSTM Sequence Models Explained

Before a longer RNN LSTM NLP Python run, write the data source, split rule, dependency versions and acceptance criteria.

Keep a fixed RNN, GRU and LSTM Sequence Models Explained failure set and a short limitations note so later changes can be compared rather than guessed.

Why recurrent encoder-decoder models led to attention

In a sequence-to-sequence system, an encoder reads the source sequence and a decoder generates the target one token at a time. Early neural machine translation compressed the source into one final recurrent state. Long sentences exposed that bottleneck. Repeated recurrent multiplication can also shrink gradients toward zero or grow them until training becomes unstable. LSTM and GRU gates help preserve useful state, while gradient clipping can limit explosions, but neither makes the bottleneck disappear.

Attention lets the decoder build a new context from all encoder states at each output step. Additive attention scores a query and key through a small learned network with a nonlinearity. Multiplicative attention uses a dot product, often scaled, and is cheaper to batch. The score formula differs, but both normalise scores into weights and take a weighted sum of value vectors.

Inspect one scaled dot-product context

import math

encoder_states = [[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
decoder_query = [1.0, 0.5]
scores = [sum(a*b for a, b in zip(state, decoder_query)) / math.sqrt(2)
          for state in encoder_states]
peak = max(scores)
exp_scores = [math.exp(score - peak) for score in scores]
total = sum(exp_scores)
weights = [value / total for value in exp_scores]
context = [sum(weight * state[d] for weight, state in zip(weights, encoder_states))
           for d in range(2)]
print('weights', [round(value, 3) for value in weights])
print('context', [round(value, 3) for value in context])

Expected attention calculation

weights [0.32, 0.224, 0.456]
context [0.776, 0.68]

The third encoder state receives the largest weight for this query, so it contributes most to the context. These vectors and the query are invented only to make the arithmetic visible. In a trained translation system they are learned, attention weights are not guaranteed explanations, and output quality still needs task-specific evaluation.

The bridge to transformers

Recurrent encoder-decoder models compute states in sequence. Transformers retain attention but remove recurrence from the main path, allowing positions to be processed in parallel and related through scaled dot products. The next module develops that change carefully. Keep this recurrent baseline in mind because it explains what self-attention fixes and what evaluation questions remain.

Build the RNN, GRU and LSTM Sequence Models Explained example

Run the provided padded sequences and trace their shapes first. Add a corpus only after the mask and length logic are correct.

Keep the data and split fixed while you change the RNN LSTM NLP Python decision.

Install:

python -m pip install torch
import torch
from torch import nn
torch.manual_seed(3)
embedding=nn.Embedding(num_embeddings=30,embedding_dim=8,padding_idx=0)
lstm=nn.LSTM(input_size=8,hidden_size=12,batch_first=True)
classifier=nn.Linear(12,2)
tokens=torch.tensor([[2,5,7,0],[3,4,9,8]])
lengths=torch.tensor([3,4])
x=embedding(tokens)
packed=nn.utils.rnn.pack_padded_sequence(x,lengths.cpu(),batch_first=True,enforce_sorted=False)
_,(hidden,_)=lstm(packed)
logits=classifier(hidden[-1])
print('logits shape',tuple(logits.shape))
print('predictions',logits.argmax(dim=1).tolist())

Expected Sequence Shapes and Predictions

logits shape (2, 2)
predictions contains two class indices. Random weights mean the classes have no learned meaning yet.

Match the printed tensor shapes and one sequence output to the model definition before interpreting predictions.

If the RNN, GRU and LSTM Sequence Models Explained result differs, print intermediate values and confirm the documented dependency versions first.

Diagnose failures in RNN, GRU and LSTM Sequence Models Explained

Check token indices, padding masks, packed lengths and hidden-state dimensions before changing optimiser settings.

Checks for the RNN, GRU and LSTM Sequence Models Explained example
SymptomLikely causeUseful check
All examples use padding as signalPadding is not masked or packedSet padding_idx and pass true lengths
Loss is NaNLearning rate, exploding gradients or invalid inputsInspect batches and clip gradients only after identifying the cause
Short texts work, long texts failState compression or truncation loses evidencePlot errors by length and review truncation policy

Train a small LSTM honestly

Use a modest labelled sequence dataset and the same split as a TF-IDF baseline.

  1. Create a vocabulary with PAD and UNK
  2. Batch variable lengths safely
  3. Track training and held-out loss
  4. Compare F1, latency and errors with logistic regression
  5. Save configuration and seed

Definition of done: The report explains whether sequence order improves meaningful errors enough to justify cost.

Stretch task: Replace LSTM with GRU under the same budget and compare, without retuning only the preferred model.

Check your RNN, GRU and LSTM Sequence Models Explained reasoning

Answer the recurrent-model questions before reading the explanations. They focus on gates, padding, gradient limits and fair baselines.

1. Why use packed sequences?
Check the answer

Answer: To keep padded positions from affecting recurrent computation.

2. What does the sample prediction mean before training?
Check the answer

Answer: Nothing useful, because the weights are random.

3. What is the right baseline?
Check the answer

Answer: A simpler model evaluated on the same split and task metric.

Primary references for RNN, GRU and LSTM Sequence Models Explained

Re-run the shape and latency checks after framework or recurrent-layer updates. Preserve the device, sequence lengths and baseline comparison.

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.