Understand attention
01 · Predict a token
Predict a token
A Transformer turns tokens already seen into probabilities for the next token.
Your turn
Predict four candidates
Four normalized logits produce these probabilities. These fixed weights have not learned a language.
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 → softmaxCheck my understanding
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
Add componentwise
The final representation still has two components; token ID is not multiplied by a vector.
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+positionCheck my understanding
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
Add componentwise
The final representation still has two components; token ID is not multiplied by a vector.
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+positionCheck my understanding
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
Add
A positive result means an acute angle, negative an obtuse angle; zero means perpendicular.
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 − 1Check my understanding
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
Add contributions
Output is 2p+6(1−p), so remains between 2 and 6.
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
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
Multiply by 2
Final output is 2GELU(x). The curve makes nonlinearity visible.
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
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
Dashed: the mean of the activations.
Rescale
Centred components are divided by √(variance+10⁻⁵), with γ=1 and β=0.
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
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
Join both paths
Addition combines x and F(x) without changing dimension.
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
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
Compensate if kept
Kept value is 2/(1−p), so expectation stays 2. Dropout is disabled at inference.
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=2Check my understanding
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
Filter and renormalize
Keep top-k=3 then top-p=.8 on renormalized mass; removed candidates become zero.
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 probabilityCheck my understanding
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