Bitpath

Lesson 2 · Unit 02

Heads and masks

Several attentions at once, and no peeking ahead

13 exercisesabout 3 min

What you practise

  • Several heads look for different things, shapes through split and concatenate, the causal mask that hides the future and why training needs it

Try a question from this lesson

Pick one

One softmax gives a token one set of weights. But "it" may need its noun for meaning and its verb for grammar at the same time. What is the simplest fix?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Split, attend, join

Multi-head attention does not make the model wider. It splits each vector of size d_model into equal slices, one per head.

Every head runs ordinary attention inside its slice. The head outputs are then concatenated, giving one vector of size d_model per token again.

x          [tokens, 12]
4 heads    4 x [tokens, 3]
attend     4 x [tokens, 3]
concat     [tokens, 12]

Concept

The causal mask

A lower-triangular table marks what each token may look at: row i has ones up to column i and zeros after.

Where the mask is 0, the score is replaced by minus infinity before the softmax. exp of minus infinity is 0, so those tokens get exactly zero weight.

Python
import torch
mask = torch.tril(torch.ones(3, 3))
for row in mask.int().tolist():
    print(row)

10 more exercises in the app

  • 2× type the answer
  • 3× multiple choice
  • 2× fill the blanks
  • 1× true or false
  • 1× order the lines
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy