Skip to content
mentoor

Topic 2 of 5 · 5 min read

From recurrent networks to transformers

Goal: Compare an LSTM and a transformer on parallel training, long-range memory and cost for a sequence task.

A recurrent network reads a sequence one step at a time and carries a single hidden state forward, while a transformer lets every position look at every other position at once. That difference decides training speed, memory and cost.

Check yourself · 1 of 3

Why can a transformer train faster than an LSTM on the same GPU?

Choose one answer

Why it matters

Text, audio, logs and sensor streams are all sequences. Until about 2017, RNNs and their gated variant, the LSTM, were the default. Modern language models are transformers, and knowing the trade-off tells you when an older or newer design is still the right pick.

How each one works

RNN and LSTM. At step t the network combines input t with the hidden state from step t-1. An LSTM adds gates that decide what to keep, forget and output, which eases the vanishing-gradient problem of plain RNNs. Two limits remain. Step t cannot start until step t-1 finishes, so training cannot be parallel across time. And the whole past must fit into one fixed-size state, so details from far back get overwritten.

Transformer. Self-attention scores every pair of positions and mixes information by those scores. The whole sequence is processed in one pass, so training parallelises well on GPUs, and a token can read a distant token directly. The price is that attention compares all pairs: compute and memory grow with the square of the sequence length. Doubling the context roughly quadruples the attention cost.

State-space models such as Mamba keep a recurrent-style state but make it input-dependent and train it in parallel. Cost grows linearly with length, and generation needs constant memory per step. They are a serious option for very long sequences, and hybrids with attention exist, but transformers still have the widest tooling and pretrained model choice.

Before and after

Weak reasoning: "Transformers beat LSTMs, so rewrite the 50 Hz vibration sensor model that runs on a microcontroller."

Better: the sensor stream is unbounded, inference must use fixed memory, and the signal has short-range structure. A small LSTM or GRU keeps constant memory per step. A transformer would need a growing window. Keep the recurrent model, and revisit only if long-range dependencies show up in errors.

What goes wrong

  • Picking a transformer for a very long input and running out of memory because attention is quadratic.
  • Expecting an LSTM to recall a fact from thousands of steps back.
  • Comparing training speed alone: an LSTM is cheap per generated step, a transformer is cheap to train in parallel.

Examples

Same task, three designs

Task: classify a 100,000-step sensor trace. An LSTM runs in constant memory but struggles to link events far apart. A full transformer needs about 10^10 pairwise scores per head per layer, which is impractical. A state-space or chunked-attention model handles the length at linear cost.

Why training speed differs

With 512 steps, an LSTM does 512 dependent computations per layer, each waiting for the last. A transformer computes all 512 positions in one batched matrix operation, which GPUs execute efficiently.

Key points

  • An LSTM must process steps in order, so training cannot be parallelised across the length of a sequence.
  • A recurrent state has fixed size, so detail from far back is compressed or lost.
  • Self-attention connects any two positions directly and trains in parallel, but its cost grows with the square of the length.
  • State-space models give linear cost in sequence length and constant memory per generated step.
  • The best choice depends on sequence length, latency, memory budget and available pretrained models, not on which design is newer.

Common mistakes

  • Replacing a working small recurrent model on an edge device with a transformer because transformers are popular.
  • Assuming attention is cheap: for long inputs it is the dominant cost, not a free upgrade.
  • Believing an LSTM remembers everything it has seen, when its state has a fixed size.
  • Treating state-space models as a drop-in for any pretrained transformer, when far fewer pretrained checkpoints exist.

Check yourself · 2 of 3

A sequence model's input grows from 4,000 to 8,000 tokens. Roughly how does self-attention cost change?

Choose one answer

Check yourself · 3 of 3

A battery-powered sensor needs a model that reads an endless stream with fixed memory. Which is the better starting point?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic