How a language model learns: pretraining and fine-tuning — Part 2

A language model can get a training target from the same text it is reading. Move the text over by one token: each input position now has a next token to predict. No one has to write a separate answer for every prefix. The catch is that the model must not be allowed to peek at that answer.

The text already contains a training target

Consider the sentence “The kettle is on the stove.” As before, we will pretend each word is one token and leave out punctuation. A real tokenizer may split it differently.

After “The”, the next token in this sentence is “kettle”. After “The kettle”, it is “is”. Each position gives us a prediction to compare with the token that actually follows.

Five prefixes from one sentence, each paired with its next-token target.

The input and target sequences are offset by one position. Word-sized tokens are a simplification for this example.

This is why we can train a language model from ordinary text without manually writing an answer for every prefix. The text supplies the target.

However, later tokens are present in the training sequence. If the position predicting “kettle” could read “kettle” from a later position, the exercise would give away the answer. The causal-attention rule from part one prevents this: each prediction can use only its own and earlier input positions. We can therefore calculate predictions at several positions together without allowing them to read ahead.

“Stove” is the target for this particular sentence. “Table” might also be a reasonable continuation in another sentence. The training target records what came next in the example; it does not establish the only acceptable continuation in the language.

Measure the prediction, then update the parameters

Suppose the model assigns “stove” a probability of 0.1 after “The kettle is on the”. Another set of parameters assigns it 0.5. For this example, the second prediction fits the observed next token better.

We need a number that expresses that difference. For next-token training, cross-entropy loss penalizes a low probability for the target token. At a single position, the loss is the negative logarithm of that probability. We do not need to work through the logarithm here: the useful relationship is that assigning more probability to the target gives a lower loss.

Those probabilities are illustrative, not measurements from a training run. In practice, we combine losses across the training positions we are evaluating, usually over a batch of several sequences.

The model’s parameters include the embedding values and the weights in its layers. Backpropagation calculates gradients: the local sensitivity of the loss to changes in each parameter. An optimizer uses those gradients to choose an update. In a simple gradient-descent update, it moves parameters a small step in the direction that locally reduces the loss; the learning rate controls the size of that step. Then we process another batch and repeat.

Training flows from predictions to loss, gradients and parameter updates; evaluation has no update step.

Training changes parameters. Evaluation measures a fixed version of the model on separate data.

One update does not guarantee a better answer to every prompt. Parameters are shared across many examples, and training involves repeatedly adjusting them using many batches. A lower loss on one batch can also come with worse predictions on other examples.

What pretraining gives us—and what to check

When we train on a broad collection of text in this way, we call the stage pretraining. Starting from initialized parameters, the model learns patterns that help it predict tokens across those examples.

Training loss tells us how well it fits the examples used to update it. We also need separate, held-out data that does not contribute to those updates. Otherwise, a model that remembers training examples can look better than it is at handling unfamiliar text. If training loss keeps falling while held-out loss rises, that is one sign of overfitting: the improvements on the training examples are not carrying over. When we use a validation set to choose settings or when to stop training, we should keep a separate test set for the final assessment.

Even a good held-out language-model loss does not answer every question about the model. If we want reliable summaries, we must check summaries. If we want a classifier, we must evaluate its labels. Predicting text well and meeting the requirements of a particular application are related, but different, things to measure.

Data quality matters throughout. Training on a poor target does not make that target correct; the loss still rewards predicting it.

Fine-tuning gives the learning a more specific purpose

After pretraining, we can start with the learned parameters and train further on examples suited to a task. That is fine-tuning. Two examples make the distinction useful.

For classification, suppose we want to sort messages into categories. The message “Your parcel arrives tomorrow” could have the label “delivery update”. One approach adapts the model with an output layer that produces scores for the available labels, then trains on message-and-label examples. The desired output is a category. This is the classifier-head approach; it is not the only way to use a language model for classification.

For instruction fine-tuning, the examples contain requests and desired text responses. A request might be “Rewrite ‘Send the file’ more politely”, paired with “Could you please send the file?” The model still predicts text tokens. During training, a prediction for a response token can use the request and the earlier response tokens provided by the example. At generation time, it must continue using the response tokens selected so far. The training examples teach it patterns for responding to requests, rather than simply continuing whatever kind of text happened to come before.

Pretraining branches into classification and instruction fine-tuning, with different examples and outputs.

These are two adaptation paths, not successive requirements. The messages and responses are invented teaching examples.

The difference is more than the amount of data. The starting parameters, examples, and desired outputs determine what we are training the model to do. A classifier head produces label scores; an instruction-following language model produces token scores used to generate a response.

Fine-tuning does not guarantee correct facts or perfect instruction following. We still need evaluation that matches the intended use. Other post-training methods, including learning from preferences, also exist; their mechanics are beyond this introduction.

What does building “from scratch” mean?

There are several pieces of work we should distinguish. We can implement an architecture so that it performs the calculations described in part one. We can train newly initialized parameters using the loop in this article. Or we can load pretrained parameters and adapt them, avoiding the need to repeat all of pretraining.

A small teaching model can make these pieces visible. We can inspect the inputs, predictions, loss, and updates without needing to understand a large production training system first. Training a capable large model adds much greater demands on data, compute and evaluation. Writing the architecture is only one part of that effort.

The same sentence plays two roles. During generation it is context for a prediction. During training, its following tokens also provide the answers used to calculate loss and change the weights. The update step is what turns prediction into learning.

Sources & references

This explanation of training and fine-tuning draws on Sebastian Raschka’s Developing an LLM: Building, Training, Finetuning. Credit for the teaching material belongs to him; the examples and diagrams here are my own explanations.

See also Raschka’s original slides.

Comments

Popular posts from this blog

getting android emulator to work normally on 64 bit linux

specs 2, Scala and maven with IntelliJ 12.1 and getting things right

me, akka and scala