Recurrent Networks and Sequence Modelling

RNNs, LSTMs and GRUs process sequences step by step; learning them makes the motivation for attention clear.

Intermediate · about 6 hours · difficulty 3/5

Learn first: Text Preprocessing and Representations, Debugging and Running Training Experiments

Before transformers, sequence data (text, audio, sensor streams) was handled by recurrent neural networks. They read one element at a time and carry a hidden state forward, like a running summary. Transformers have replaced them in most NLP work, but RNN ideas still appear in time series, streaming and on-device models, and understanding their limits explains why attention was such a breakthrough.

Key ideas

  • Vanilla RNN: the hidden state is updated from the previous state and the current input with shared weights.
  • Backpropagation through time unrolls the network across steps; long sequences cause vanishing or exploding gradients.
  • LSTM and GRU add gates that decide what to remember and forget, which preserves information over longer spans.
  • Bidirectional RNNs read the sequence both ways when the whole input is available.
  • Sequence-to-sequence models pair an encoder and a decoder for translation and summarization; the encoder squeezing everything into one vector is the bottleneck attention was invented to fix.

Practical notes

Pad and pack variable-length batches, clip gradients, and use teacher forcing when training decoders. For many time series problems a 1D CNN or gradient boosting on lag features is a tough baseline to beat.

Where to spend time

Build one LSTM model end to end so the concepts are concrete, then move on. Do not spend weeks here; the transformer topic is where modern NLP lives.

Good enough to move on when you can train an LSTM on a sequence task and explain why plain RNNs struggle with long-range dependencies.

Key concepts

  • Hidden state and recurrence
  • Backpropagation through time
  • Vanishing and exploding gradients
  • LSTM and GRU gates
  • Bidirectional RNNs
  • Encoder-decoder seq2seq
  • Padding and packing sequences

Mini project

Train a character-level LSTM that generates names or short text in a style of your choice, and plot how sample quality changes with sequence length and hidden size.

Check yourself

What problem do the gates in an LSTM solve, and why does a fixed-size encoder vector limit seq2seq translation quality?

Free resources