Thomas Piacentini
← Workshop
← All concepts

Understand attention

Concept 1 / 10

Change concept
01 · Predict a token

Predict a token

A Transformer turns tokens already seen into probabilities for the next token.

Your turn

2
3 tokens × 4 components+ positionH1×××123H2×××123Causal: past and presentLN + MLPGELUResiduals + normalization4 components70.6 %13.4 %11.6 %4.4 %
Representation and attentionMLP blockProbabilities

Predict four candidates70.563 % / 13.433 % / 11.644 % / 4.36 %

Four normalized logits produce these probabilities. These fixed weights have not learned a language.

Build
Final result · Chance of the first candidate token70.563 %

Understand the calculation, step by step

An autoregressive model predicts the next token from preceding tokens, then repeats. Inputs pass through attention and position-wise networks before projection into vocabulary logits. The original demonstrates GPT-2; our fixed tiny examples expose dimensions without reproducing its knowledge or training a large model.

A complete causal block

3 tokens × 4 components ; position → LN → 2 causal heads Q=K=V → residual → LN → GELU MLP → residual → LN → logits → softmax

Check my understanding

For Third token first component = 3, what is the value of « first candidate token probability » ?

Answer as a proportion (0 to 1), or a percentage with the % sign.

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

02 · Tokenization and embeddings

Tokenization and embeddings

A token receives a list of numbers the model can transform and compare.

Your turn

2
Token(2 ; 1)Position(0.1 ; 0.2)+(2.1 ; 1.2)
InputTransformationResult

Add componentwise(2.1 ; 1.2)

The final representation still has two components; token ID is not multiplied by a vector.

Build
Final result · first input component2.1

Understand the calculation, step by step

A tokenizer maps text to subword identifiers; a token need not be a word or character. An embedding table returns row Eᵢ for identifier i, not the identifier multiplied by a vector. Similar spelling need not imply nearby learned vectors. Vocabulary and tokenizer must match model weights.

Sum embeddings

token=(x,1), position=(0.1,0.2) ; input=token+position

Check my understanding

For Token first component = 3, what is the value of « first input component » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

03 · Position and final representation

Position and final representation

The same words in a different order change meaning: the model needs their positions.

Your turn

2
Token(2 ; 1)Position(0.1 ; 0.2)+(2.1 ; 1.2)
InputTransformationResult

Add componentwise(2.1 ; 1.2)

The final representation still has two components; token ID is not multiplied by a vector.

Build
Final result · first input component2.1

Understand the calculation, step by step

Without position information, attention alone does not know input order. Position representations here add componentwise to token vectors. GPT-2 uses learned positions; the original Transformer used another method, sinusoids. Position indices are not new tokens appended to the sentence.

Sum embeddings

token=(x,1), position=(0.1,0.2) ; input=token+position

Check my understanding

For Token first component = 3, what is the value of « first input component » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

04 · Q, K, V projections and heads

Q, K, V projections and heads

Multiple heads compare the same tokens using different representations.

Your turn

2
xyuv4 − 1 = 3
InputTransformationResult

Add3

A positive result means an acute angle, negative an obtuse angle; zero means perpendicular.

Build
Final result · Alignment · dot product3

Understand the calculation, step by step

For T×d input X, Q=XWQ, K=XWK and V=XWV. Each head uses dₕ components; QKᵀ is T×T and compares positions. Distinct heads learn distinct projections, not guaranteed linguistic categories. Check dimensions before values; division by √dₕ moderates score scale.

Dot product

u = (x, 1), v = (2, −1) ; u·v = 2x − 1

Check my understanding

For x component of u = 3, what is the value of « dot product » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

05 · Causal mask and value mixing

Causal mask and value mixing

Attention mixes available information by giving some values more weight.

Your turn

25 %
V₁2V₂625 %75 %0.54.5+0.5 + 4.5 = 5
Value 2Value 6Mixture

Add contributions5

Output is 2p+6(1−p), so remains between 2 and 6.

Build
Final result · Result of mixing5

Understand the calculation, step by step

Future positions receive −∞ before softmax, making their weights zero. Each softmax row sums to one; multiplication by V mixes visible values. Head outputs are concatenated and projected by WO. A large weight describes the computation without proving a causal explanation of model behaviour.

Attention mixing

V₁=2, V₂=6 ; output = 2p+6(1−p)

Check my understanding

For First token weight = 0.75, what is the value of « weighted output » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

06 · MLP and nonlinearity

MLP and nonlinearity

A nonlinear transformation lets a network do more than rescale its input.

Your turn

1
x = 1GELU2 × 0.841 = 1.682
InputTransformationResult

Multiply by 21.682

Final output is 2GELU(x). The curve makes nonlinearity visible.

Build
Final result · 2GELU output1.682

Understand the calculation, step by step

The MLP uses the same weights at each position: GELU(XW₁+b₁)W₂+b₂. It mixes components within a token; attention mixes positions. Without nonlinearity, two affine layers collapse into one. Our exercise uses the stated tanh approximation to GELU rather than the exact Gaussian-integral function.

Miniature position-wise network

GELU approx(x)=0.5x(1+tanh(√(2/π)(x+0.044715x³))) ; output=2GELU(x)

Check my understanding

For Input activation = 2, what is the value of « 2GELU output » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

07 · Per-token normalization

Per-token normalization

Normalization puts token components on a comparable scale before the next transformation.

Your turn

3
One token123μ = 2, v = 0.667-1.22501.225(h − μ) / √(v + 10⁻⁵)
InputTransformationResult

Dashed: the mean of the activations.

Rescale(-1.225 ; 0 ; 1.225)

Centred components are divided by √(variance+10⁻⁵), with γ=1 and β=0.

Build
Final result · activation mean2

Understand the calculation, step by step

LayerNorm subtracts the mean across one token components and divides by √(variance+ε), then applies learned γ and β. It does not normalize the whole batch. In GPT-2 pre-normalized blocks it precedes each sublayer. This exercise fixes γ=1 and β=0 and displays normalized components and mean.

Normalize a token

h=(1,2,x) ; μ=(3+x)/3 ; variance=Σ(h−μ)²/3 ; ε=10⁻⁵

Check my understanding

For Third activation = 5, what is the value of « activation mean » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

08 · Residual paths

Residual paths

A residual connection adds a transformation to information already present.

Your turn

2
x2IdentityF(x)=0.5x1+3
InputTransformationResult

Join both paths3

Addition combines x and F(x) without changing dimension.

Build
Final result · residual output3

Understand the calculation, step by step

A residual connection computes X+F(X) with matching dimensions. Its gradient includes an identity path as well as the path through F, aiding deep optimization without guaranteeing trouble-free training. One addition follows attention and another follows the MLP. Check that each branch preserves token dimension d.

Residual connection

branch F(x)=0.5x ; output=x+F(x)

Check my understanding

For Input x = 3, what is the value of « residual output » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

09 · Dropout: training and inference

Dropout: training and inference

During training, removing activations discourages relying on the same paths every time.

Your turn

20 %
Activation220 %80 %Dropped0Kept2.5Expectation = inference = 2
InputTransformationResult

Compensate if kept2.5

Kept value is 2/(1−p), so expectation stays 2. Dropout is disabled at inference.

Build
Final result · Retained value during training2.5

Understand the calculation, step by step

Dropout randomly removes activations during training. In inverted dropout, surviving values are divided by 1−p to preserve expectation; inference disables it. Variation in generated text at inference generally comes from token sampling rather than active dropout.

Inverted dropout

input=2 ; retained training activation=2/(1−p) ; inference=2

Check my understanding

For Drop probability p = 0.5, what is the value of « retained activation » ?

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

10 · Logits, temperature and selection

Logits, temperature and selection

The model proposes probabilities; sampling settings determine how a token is selected.

Your turn

1
logits : 2 ; 1 ; 0 ; −1173.1 %226.9 %3×4×τ = 1 → softmaxtop-k = 3 → top-p = 0.8
InputTransformationResult

Filter and renormalize73.106 % / 26.894 % / 0 % / 0 %

Keep top-k=3 then top-p=.8 on renormalized mass; removed candidates become zero.

Build
Final result · Chance of the first candidate token73.106 %

Understand the calculation, step by step

Final projection gives logits and softmax(logits/τ) gives probabilities. Top-k retains k candidates; top-p retains a set reaching cumulative mass p, then renormalizes. Low temperature concentrates probability without ensuring truth. Output-logit temperature differs from altering internal attention scores. Compare original controls and tutorial with our lab.

Temperature, top-k and top-p

logits=(2,1,0,−1) ; divide by τ ; top-k=3 then top-p=0.8 (renormalized mass) ; final first-token probability

Check my understanding

For Temperature τ = 2, what is the value of « final first-token probability » ?

Answer as a proportion (0 to 1), or a percentage with the % sign.

Keep a thought in my notebook
Read the source and go further

The extended record combines confirmed reading, interaction, correct answer and a note of at least 40 characters. You can continue at any time.

Take this further

atomsh / lab