Natural Language Processing · Advanced level

Advanced level · Module 5 of 10 · 14 guided hours

Attention Mechanisms in NLP

Attention as a solution to the encoder-decoder bottleneck: additive and dot-product attention, visualising alignments, and self-attention as the bridge to transformers.

Learning objectives

  • Explain the bottleneck problem and how attention solves it
  • Add attention to a seq2seq model and visualise attention weights
  • Compute scaled dot-product self-attention

Topics

  • the fixed-vector bottleneck
  • additive (Bahdanau) attention
  • dot-product attention
  • attention weights and alignment
  • self-attention
  • queries, keys and values

Lessons

  1. Lesson 1 Why Attention: the Bottleneck Problem

    Learners see why squeezing a whole input into one vector fails on long inputs and how attention lets the decoder look back at every encoder state.

    bottleneckencoder outputsalignment scoressoftmax weightscontext vector per step

  2. Lesson 2 Adding Attention to Seq2Seq

    Learners add dot-product attention to the date-normalisation decoder, retrain and visualise the attention matrix as a heatmap.

    dot-product attentionadditive attentiontorch.bmmattention heatmapinterpretation limits

  3. Lesson 3 Self-Attention, Queries, Keys and Values

    Learners compute scaled dot-product self-attention by hand and in PyTorch, the core operation of transformers.

    self-attentionquery, key, valuesoftmax(QKᵀ/√d)Vmulti-head attentionnn.MultiheadAttention

Practical activity

Add attention to the seq2seq model, demonstrate the improvement on long inputs, present attention heatmaps and implement self-attention from scratch.

Expected outcomes

  • Deep understanding of attention
  • Readiness for transformer architectures

Assessment

Notebook review (60%) and a hand-calculation test on self-attention (40%).

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.