Language Modeling
What Is a Language Model?
Predicting P(next token | context) over a vocabulary
Given context "the cat sat", the model predicts P(next token). The correct next word "on" has the highest probability (74.9%). A language model assigns probabilities to every possible continuation.
Step 1 of 6: What Is a Language Model?
Predicting P(next token | context) over a vocabulary
Given context "the cat sat", the model predicts P(next token). The correct next word "on" has the highest probability (74.9%). A language model assigns probabilities to every possible continuation.
Step 2 of 6: Autoregressive Generation
Each predicted token feeds back as input for the next
Generation starts with a seed token "the". The model takes this single token as context and predicts the next most likely token "big".
Step 3 of 6: Training with Cross-Entropy
The loss that teaches models to assign high probability to correct tokens
Moderate confidence (70%) — loss = 0.357. Cross-entropy penalizes the model for not being confident enough about the correct token "sat". Training pushes this probability higher.
Step 4 of 6: Temperature & Sampling
Control the sharpness of the probability distribution
T = 1.00 — moderate temperature. The top token "the" is favored but alternatives have nonzero probability. This balances quality and diversity. T=1.0 is the model's "natural" distribution.
Step 5 of 6: Top-k Sampling
Filter low-probability tokens to prevent nonsense
Top-3: only the 3 most probable tokens are kept (86.9% of total mass). Crossed-out tokens are impossible to sample. This prevents unlikely tokens from appearing while allowing some diversity.
Step 6 of 6: Perplexity
Measuring how 'surprised' the model is by the data
Perplexity 2.7 — decent model. Notice how function words ("the", "on") still get high probability, but content words ("cat", "mat") are less certain. PPL = e^(average −log p) — it's the geometric mean of 1/p across all tokens.