Foundation Models: Tokens, Attention, And Generation · Lesson 9

How Next-Token Training Works

An observed next token provides a target. The probability assigned to that target determines a predictive loss; gradients and an optimizer use the combined objective to update model parameters. This learning signal is distinct from checking a new claim against the world.

Study And Save Progress

Watch The Lesson

7 min 14 sec

Read the video transcript

The training text says: The exhibition opens on Monday. Suppose Monday is the next target token in a supplied teaching sequence. A model gives that target probability 0.1 in 1 calculation and 0.8 in another. What makes the training process prefer the 2nd prediction?

Follow the observed target, the vocabulary scores, the loss calculation, and the parameter update. Then ask what this learning signal can tell us about a visitor's real question. A basic causal language-model objective uses an observed sequence to provide targets. At each prediction position, the preceding allowed tokens are the input and

the observed next token is the target. For this example we stipulate Monday as 1 token; an actual tokenizer determines the real units. The training target comes from the sequence. No researcher needs to write a separate question and answer for every position in this basic setup.

The model's hidden state reaches an output projection that computes logits, meaning unnormalized scores for vocabulary candidates. Softmax converts these scores into probabilities that sum to 1 across that vocabulary under the defined calculation. This distribution is over possible output tokens. In the attention example, softmax normalized shares

over source positions and combined value vectors. The same mathematical function now has a different set of objects and a different role. The target's probability supplies a simple loss calculation: take its natural logarithm and change the sign. The natural logarithm, written ln, uses

the mathematical constant e as its base. You can inspect the values with a calculator rather than derive logarithms to follow the argument. A target probability close to 1 gives a small loss; a target given very little probability receives a larger loss. With target probability 0.8,

negative ln of 0.8 is approximately 0.223. With target probability 0.1, negative ln of 0.1 is approximately 2.303. The 2nd loss is much larger. We have held the target token fixed while comparing the probabilities assigned to it. These are supplied numerical cases, not measured predictions from a museum model.

The lower loss rewards assigning more probability to this observed target under the objective. This calculation is a simple instance of cross-entropy loss for a single observed target. Training combines losses across eligible positions and examples under its setup. Gradients describe how a small change in a parameter would change that combined loss.

An optimizer uses this information to update parameters. The update is distributed through the learned computation; it does not append a new database row saying that Monday is always the correct answer. Nor does this example supply a parameter-update size.

That requires the actual model computation, derivatives, optimizer and learning rate. A training step aims to reduce the chosen objective, but it need not improve every individual example after a shared update. Different examples can pull shared parameters in different directions. Evaluation on separate data helps

us distinguish fitting the training examples from useful prediction beyond them. A complete training sequence supplies many prediction positions. A program can calculate multiple positions together while causal masks prevent each prediction from accessing prohibited later tokens. For the earlier prediction of opens,

Monday is still beyond its allowed prefix. Having the whole sequence in the training record does not authorize leaking that future token into the prediction. Ordinary generation has a different dependency: each newly selected output token supplies context for the next step. An evaluation measure called perplexity

is related to predictive loss. Under the natural-log convention, exponentiating the average token loss gives perplexity. Its value depends on the data, tokenization and evaluation procedure. Changing the tokenizer changes the units being scored, so raw perplexities across different tokenizers need careful interpretation.

The number also does not describe every downstream skill or the usefulness of a deployed service. The 2020 GPT-3 paper reported Penn Tree Bank perplexity and omitted other language-modeling datasets because of training overlap. That choice illustrates why the evaluation record matters: prediction on previously encountered

text can overstate performance on separate data. For the museum, lower held-out prediction loss is useful evidence about that objective. An opening-hours answer still needs the applicable current schedule and a task-level check. Pause at the 2 supplied target probabilities. Calculate negative natural log loss for 0.8 and 0.1 to 3 decimal places.

Which receives the larger penalty, and why does the smaller loss alone fail to establish the truth of a new opening-hours answer? The losses are approximately 0.223 and 2.303. The 0.1 target probability receives the larger penalty because the observed

target was assigned less probability. The smaller loss supports this prediction objective on the specified example. It has not independently checked a new schedule, selected the applicable date or verified a generated factual claim. Those require evidence and evaluation beyond this calculation.

Try a transfer case with supplied target probabilities 0.5 and 0.25. A calculator gives natural logarithms of approximately negative 0.693 and negative 1.386. Record each negative-log loss and identify the preferred prediction under this objective. Then a colleague proposes comparing average token losses from 2 different tokenizers

as a direct ranking of service reliability. What information is missing? The losses are approximately 0.693 and 1.386, so assigning 0.5 to the same target produces the smaller loss. A comparison across tokenizers needs the tokenization, data and scoring protocol, including how different units are made comparable.

Service reliability also requires task-level tests. Neither a training-loss value nor its perplexity transformation establishes the correctness of every response. Text contains patterns that support many useful tasks, so learning to predict it can build useful representations and behavior. The objective we inspected rewards prediction under the data and training design.

Suitable data, adaptation, application evidence and evaluation help turn that capability into a dependable service. You can now distinguish the observed target, its assigned probability, the loss and the update. Next we will follow a fixed model generating a response, where tokens are selected

without an ordinary inference-time parameter update.

Main Point

An observed next token provides a target. The probability assigned to that target determines a predictive loss; gradients and an optimizer use the combined objective to update model parameters. This learning signal is distinct from checking a new claim against the world.

Key Points

1. Specify the target and allowed input

The supplied teaching sequence says “The exhibition opens on Monday”. Stipulate “Monday” as 1 token in this illustration. The actual tokenizer determines real token boundaries. To predict this target, the causal model uses the preceding allowed prefix.

At an earlier prediction of “opens”, later text including “Monday” is outside the allowed prefix. A complete training record supplies targets, but causal masking prevents each prediction from reading prohibited future positions.

2. From hidden state to vocabulary probability

The model’s output projection computes logits: unnormalized scores for vocabulary candidates. Softmax produces a distribution that sums to 1 over the defined vocabulary. The target receives 1 probability from that distribution.

This is a different object from the attention shares in L06. Attention normalized scores over source positions to combine value vectors. Here the distribution concerns possible output tokens.

3. Calculate the single-target loss

The natural logarithm, written `ln`, uses e as its base. For this single-target view:

`loss = −ln(probability assigned to the observed target)`

Same target, supplied probabilityNegative-log loss, approximatelyTo 3 decimal places
0.80.22314355130.223
0.12.30258509302.303

The 0.1 prediction has the larger penalty. Hold the observed target fixed when comparing these cases. These are supplied calculations, not measurements from a museum model. A probability approaching 1 produces loss approaching 0; a probability approaching 0 produces a growing penalty.

4. Use the combined signal to update parameters

Training combines losses across eligible positions/examples under its setup. A gradient describes how a small parameter change affects that objective. An optimizer uses the gradient and its configured rule to update parameters through the learned computation.

The update is shared across the model, rather than adding a database row that makes “Monday” universally correct. Its numerical size requires actual derivatives, optimizer settings and a learning rate, none of which these 2 probability cases supply. A shared update need not improve every example individually.

5. Separate parallel training from generation

Multiple target positions can be processed together with the appropriate causal mask. In ordinary autoregressive generation, each selected output token becomes part of the context for a subsequent prediction. This later selected output is unavailable before the earlier selection. The 2 processes therefore have different parallelism.

6. Interpret evaluation loss and perplexity

Under a natural-log average token loss, `perplexity = exp(average token loss)`. Its interpretation requires the evaluation data, tokenization, scoring and aggregation procedure. Different tokenizers divide text into different units, so raw perplexities need not be directly comparable.

A historical example is Brown and colleagues’ 2020 GPT-3 paper: section 3.1.1 reports Penn Tree Bank perplexity and explains the omission of other language-modeling datasets with training overlap. This is an evaluation-design example, not a current model ranking or evidence about the museum’s opening-hours service.

For that service, inspect the applicable current schedule and test factual behavior. Predictive loss and perplexity are useful objective-specific evidence; task quality and resource value need their own measurements.

7. Closing

You can now follow target → vocabulary probability → loss → parameter-update signal, while keeping dataset fit and independent factual checking separate. The next lesson follows a fixed model generating output through repeated token selection.

8. Learning Checks

Attempt the questions before opening the explanations. The worksheet provides blank working space.

M14-L09-Q01

For the same observed target, calculate −ln(0.8) and −ln(0.1) to 3 decimal places. Which target probability receives the larger penalty? Why does the smaller loss alone fail to establish the truth of a new opening-hours answer?

Your calculation and explanation: ____________________

Show explanation after your attempt

The losses are approximately 0.223 and 2.303. Probability 0.1 receives the larger penalty because the observed target was assigned less probability. Lower loss supports the defined target-prediction objective on this example. It has not checked an applicable current schedule or independently verified a new generated claim. That requires relevant evidence and task-level evaluation.

M14-L09-P01

A new example assigns the same target probability 0.5 or 0.25. Supplied values are ln(0.5)≈−0.693 and ln(0.25)≈−1.386. Record each loss and identify the preferred prediction under the objective. A colleague also compares average token losses from 2 different tokenizers as a direct service-reliability ranking. What information is missing?

Your calculation and explanation: ____________________

Show explanation after your attempt

The negative-log losses are approximately 0.693 and 1.386. Assigning probability 0.5 to the same target gives the smaller loss. Comparing different tokenizers requires the tokenization, evaluation data and scoring protocol, including a defensible way to compare different units. Service reliability also needs task-level tests. Average loss or its perplexity transformation alone is insufficient.

Sources

Learner Materials

Download Lesson 9 Learner Materials

Use the supplied fictional cases and templates for practice.

Practice

Write your answer, compare it with the model answer and decide what to revisit. These written checkpoints are self-assessed.

For the same observed target, calculate −ln(0.8) and −ln(0.1) to 3 decimal places. Which target probability receives the larger penalty? Why does the smaller loss alone fail to establish the truth of a new opening-hours answer? Your calculation and explanation: ____________________

Show The Model Answer

The losses are approximately 0.223 and 2.303. Probability 0.1 receives the larger penalty because the observed target was assigned less probability. Lower loss supports the defined target-prediction objective on this example. It has not checked an applicable current schedule or independently verified a new generated claim. That requires relevant evidence and task-level evaluation.

Compare your reasoning with the model answer. These written checks are self-assessed.

A new example assigns the same target probability 0.5 or 0.25. Supplied values are ln(0.5)≈−0.693 and ln(0.25)≈−1.386. Record each loss and identify the preferred prediction under the objective. A colleague also compares average token losses from 2 different tokenizers as a direct service-reliability ranking. What information is missing? Your calculation and explanation: ____________________

Show The Model Answer

The negative-log losses are approximately 0.693 and 1.386. Assigning probability 0.5 to the same target gives the smaller loss. Comparing different tokenizers requires the tokenization, evaluation data and scoring protocol, including a defensible way to compare different units. Service reliability also needs task-level tests. Average loss or its perplexity transformation alone is insufficient.

Compare your reasoning with the model answer. These written checks are self-assessed.

Open Practice And Save Your Work