Lesson 11 of 23 · LLM preliminaries
Every position predicts what comes next.
Your win: construct autoregressive inputs and targets, read vocabulary logits, and calculate one next-token cross-entropy loss.
One sequence supplies many training examples
For tokens \([t_0,t_1,t_2,t_3,t_4,t_5]\), remove the last token to form the input and remove the first token to form the targets:
At input position 2, the visible prefix ends in “sat” and the target is “on.” A causal mask prevents that position from reading the target token before predicting it.
Logits score every vocabulary item
For batch size \(B\), sequence length \(T\), and vocabulary size \(V\), the language-model head returns
The slice \(L[b,t,:]\) contains \(V\) unnormalized scores for one next-token decision. Softmax converts that score vector to probabilities.
Numbers substituted: one prediction over three tokens
Let the logits be \((2,1,0)\), with target index \(1\). Then
Increasing the target logit raises its probability and lowers the loss.
Code checkpoint · shift, flatten, compare
Show next-token labels and cross-entropy
import torch
import torch.nn.functional as F
tokens = torch.tensor([[4, 8, 15, 16, 23, 42]])
inputs = tokens[:, :-1] # [B=1, T=5] = [4, 8, 15, 16, 23]
targets = tokens[:, 1:] # [B=1, T=5] = [8, 15, 16, 23, 42]
logits = model(inputs) # [B=1, T=5, V]
loss = F.cross_entropy(
logits.reshape(-1, logits.size(-1)),
targets.reshape(-1),
)Trace it: Why do inputs and targets have the same \(B\times T\) shape even though they contain different tokens?
Retrieval check
For tokens \([5,9,2,7]\), which targets train next-token prediction?
Practice before moving on
- Form inputs and targets from \([10,20,30,40,50]\).
- If \(B=4,T=128,V=32{,}000\), state the logits shape.
- What does \(L[2,7,:]\) represent?
- Compute \(\operatorname{softmax}(0,0)\) and the loss when either class is correct.
- Why must a causal model hide future target tokens during training?
- Explain why one length-100 sequence provides 99 aligned next-token targets.
Check solutions
- Inputs \([10,20,30,40]\); targets \([20,30,40,50]\).
- \(4\times128\times32{,}000\).
- All vocabulary scores for the token following position 7 in batch item 2.
- Probabilities \((0.5,0.5)\); loss \(-\log0.5\approx0.693\).
- Otherwise it could copy the answer instead of learning from the prefix.
- Every token after the first becomes the target for the prefix ending immediately before it.
Source: Bengio et al., A Neural Probabilistic Language Model; PyTorch CrossEntropyLoss.