Log inStart free
LESSON 9.04 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Self-attention vs cross-attention

See where the queries, keys and values come from in two kinds of attention.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 9.04 · ATTENTION BAY6 ENGLISH · 4 FRENCH TOKENS
01
Stage 1 of 6 · Two sentences
The encoder holds 6 English tokens. The decoder has written 4 French tokens so far.
Explain it simply

This is a translation model at work. On top, the encoder holds the English sentence: the black cat drinks warm milk. Below, the decoder writes French, one token at a time. Each token is 4 numbers, and a stronger shade is a bigger size. A minus sign marks a number well below zero.

Two blocks of rows. The top block has 6 English tokens: the, black, cat, drinks, warm, milk. The bottom block has 4 French tokens: le, chat, noir, boit. Each row has 4 cells.

Weights are set by hand, not trained.
tokens, queries, keys, valuesweight matrices Wattention weights, outputsmasked
−−theblackcatdrinkswarmmilklechatnoirboitencoder · Englishdecoder · FrenchX_en: 6 × 4 · X_fr: 4 × 46 English tokens · 4 French tokens so far−−theblackcatdrinkswarmmilklechatnoirboitencoder · Englishdecoder · FrenchX_en: 6 × 4 · X_fr: 4 × 46 English tokens · 4 French tokens so far
Toy: 6 English and up to 7 French tokens, width 4
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Xen:6×4,Xfr:n×4\sy{xe}{data}{X_{\text{en}}}: 6 \times 4, \quad \sy{xf}{data}{X_{\text{fr}}}: n \times 4
Xen:6×4,Xfr:4×4X_{\text{en}}: 6 \times 4, \quad X_{\text{fr}}: 4 \times 4

Each sentence is a stack of token rows, 4 numbers wide.

Common mix-ups

MYTHCross-attention needs a causal mask, as decoder self-attention does.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01Switch from decoder self-attention to cross-attention. How many columns does the weight grid get?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.