4.2.6 · D1Tokenization & Language Modeling

Foundations — Causal language modeling objective

2,087 words9 min readBack to topic

Before you can read the parent note without stumbling, you need to own every symbol it throws at you. This page builds them one at a time, from nothing, each with a picture. If you have never seen a subscript, a , or a before — start here at line one.


1. Tokens and the sequence

Before we can predict "the next word", we must decide what a "word" even is to a machine. A model cannot eat letters or spaces directly. Instead, text is chopped into small pieces called tokens — sometimes whole words, sometimes word-fragments, sometimes punctuation.

We line these tiles up in order and give the whole line a name: the bold letter .

Figure — Causal language modeling objective

Why do we need position numbers at all? Because "I love ML" and "ML love I" contain the same tiles but mean different things. The order is the information. The subscript is how we pin each tile to its slot.

See Transformer Decoder for the machine that actually processes this sequence, and Autoregressive Models for the family this whole idea belongs to.


2. Probability , and conditional probability

The parent note is drowning in the letter . Here is exactly what it means.

For language: asks "how likely is the second word to be 'love'?" — before we've looked at anything else.

But language depends on context. "love" is likely after "I", unlikely after "quantum". To express "likely given we already know something", we use the vertical bar , read as "given".

Figure — Causal language modeling objective

Why does the topic need conditionals? Because the entire trick of language modeling is: don't guess words in a vacuum — guess each word conditioned on the words before it. That bar is the heart of the whole objective.


3. The context — "everything before now"

The parent writes constantly. Decode it piece by piece:

  • = a token,
  • the little below = "at a position less than ".
Figure — Causal language modeling objective

Why "causal"? In the real world, causes come before effects — you can't be influenced by tomorrow. Forcing predictions to depend only on the past mirrors that arrow of time. This is the crucial difference from Masked Language Modeling, which is allowed to look both ways.

So is the whole game in one phrase: "probability of the next tile, given all tiles so far."


4. The parameters — the model's tunable knobs

The subscript (Greek letter "theta") appears on every .

therefore means "the probability according to this particular setting of the knobs". Training = nudging the sliders until the model's guesses match real text. The subscript reminds us: this is the model's opinion, not ground truth.


5. The product and the chain rule

To score a whole sentence, the parent multiplies many conditional probabilities together. The tall symbol for "multiply a list" is (capital Greek "pi").

Now the parent's key formula:

Why does the topic need it? Because it turns the impossible task "score an entire sentence at once" into a repeated, manageable task "score one next word at a time". That repetition is what a Transformer Decoder is built to do in parallel.


6. The logarithm — turning products into sums

Multiplying hundreds of tiny probabilities (each less than 1) gives an absurdly small number — computers round it to zero (underflow). The fix is a mathematical tool called the logarithm.

Why this tool and not another? We specifically need something that (a) converts a fragile product into a stable sum, and (b) is monotonic — bigger input still gives bigger output, so maximizing the log is the same as maximizing the original. The logarithm is the unique everyday function with both properties. Nothing else lets us keep the answer while dodging underflow.

Figure — Causal language modeling objective

7. The sum and the negative log-likelihood

Apply the log to the product from Section 5, and every becomes a . The "add up a list" symbol is (capital Greek "sigma").

Putting it together gives the parent's core loss:

Two last symbols to name:

  • (fancy script "L") = the loss, a single number measuring how bad the model is. Lower is better; a perfect model ( everywhere) scores .
  • The leading minus sign flips log's negatives into positives, so "worse guess" = "bigger loss". Minimizing this is identical to maximizing the sentence's probability. This quantity has a name from information theory: Cross-Entropy Loss.

8. Softmax and the vocabulary

The model doesn't output a probability directly. It first outputs a raw score for every possible token. The full list of possible tokens is the vocabulary, and its size is written (the bars mean "count of items in the set "). For GPT-2, .

The raw scores are called logits, — one real number per vocabulary entry ( just means "any real number", the superscript means "a list of that many of them").

Why and not just clamp negatives to zero? Because is smooth and always positive, it keeps every token in the running (none is ever hard-zeroed) while still sharply favouring high scores — ideal for the gentle nudging that training needs. Once you have this distribution, Sampling Strategies decide which token to actually pick at generation time.


Prerequisite map

Tokens and sequence x

Context x less than t

Probability P

Conditional P of A given B

Chain rule product

Logarithm turns product into sum

Negative log likelihood loss

Parameters theta

Softmax over vocabulary V

Causal LM objective

Everything on the left is a raw ingredient; they flow rightward into the single objective the parent topic optimizes.


Equipment checklist

Test yourself — reveal only after you've answered aloud.

What does the subscript in mean?
The position of the token in the sequence (the 3rd tile), not a multiplication.
Read in plain English.
The model's probability of the next token , given all the tokens before position .
What is ?
The context — every token at a position less than , i.e. everything to the left of the one being predicted.
Why is the modeling "causal"?
Predictions may depend only on the past, never on future tokens — mirroring cause-before-effect.
What does stand for?
All the model's tunable numbers (weights, embeddings) — the knobs training adjusts.
Why turn the product of probabilities into a sum of logs?
To avoid underflow (tiny products rounding to 0) while preserving the maximizer, since .
What does the minus sign in achieve?
It flips log's negatives into positives so a worse guess gives a bigger loss.
What does softmax do?
Converts raw logits into a valid probability distribution over the vocabulary that sums to .
What is ?
The size of the vocabulary — how many distinct tokens exist.
What is mean?
A perfect model that assigns probability to every true next token.