What it is
A language model is a very large function with billions of adjustable numbers, its weights. Given some text, it outputs a probability for every possible next token. Fine-tuning changes those weights so that, given your kind of context, the model gives a high probability to the kind of reply you want. The training loop is the procedure that does this. It is not one clever operation but one simple step, repeated many thousands of times, each repetition nudging the weights slightly.
How one step works
1. Forward pass. A batch of examples goes through the model. At every position of each target reply, the model produces a probability distribution over its whole vocabulary, often 100,000 tokens or more, for what comes next. Because the true reply is already known, all positions are predicted in parallel in a single pass. The model always sees the correct earlier tokens rather than its own guesses, a technique called teacher forcing.
2. Loss. For each target token, the loss is the negative logarithm of the probability the model gave the correct token. The loss of the batch is the average over all its target tokens. Context tokens (the system prompt, earlier turns, the user's message) are read but masked out of this average, so only the replies you chose as targets are graded.
loss = − (1 / N) × Σ log p(correct token | everything before it)
N = the number of target tokens in the batch
context tokens are masked: read, never scored- The model gives the correct next token a probability of 0.9: loss = −ln 0.9 ≈ 0.11.
- A probability of 0.5: loss = −ln 0.5 ≈ 0.69.
- A probability of 0.05: loss = −ln 0.05 ≈ 3.00.
- Average over the three tokens: (0.11 + 0.69 + 3.00) ÷ 3 ≈ 1.26. One confidently wrong token dominates the score.
- For scale: a model guessing at random among 100,000 tokens would score ln 100,000 ≈ 11.5. Fine-tuning losses start far below that, because the base model is already a good guesser; training refines it on your replies.
3. Backward pass. Backpropagation computes the gradient of every trainable weight: how much the loss would change if that weight moved a tiny amount, and in which direction. It costs roughly twice the compute of the forward pass, and it needs the intermediate values (activations) saved during the forward pass. That is why longer examples and bigger batches need more GPU memory.
4. Update. The optimizer changes each weight against its gradient, scaled by the learning rate. In its simplest form, plain gradient descent, the rule is a single line.
gradient = ∂loss / ∂w how the loss changes when w changes
w (new) = w − learning_rate × gradientThe optimizer: Adam and AdamW
Language models are almost always trained with Adam or its variant AdamW rather than plain gradient descent. Adam keeps two running averages for every weight: the average gradient (momentum, which smooths out the noise from one batch to the next) and the average squared gradient (which scales each weight's step, so weights with consistently large gradients don't take huge steps and rarely updated ones aren't starved). The result is that each weight moves by roughly the learning rate per step, whatever the raw size of its gradient.
AdamW changes how weight decay, a gentle pull of the weights toward zero that discourages extreme values, is applied: it is subtracted from the weights directly instead of being mixed into the gradient, which works more reliably with Adam's per-weight scaling. It is the default optimizer in common training libraries.
m = β1 · m + (1 − β1) · g running average of gradients (momentum)
v = β2 · v + (1 − β2) · g² running average of squared gradients
w = w − lr · m̂ / (√v̂ + ε) a step scaled separately for every weight
w = w − lr · λ · w decoupled weight decay (the "W" in AdamW)
g = gradient, m̂ and v̂ = bias-corrected m and v
usual defaults: β1 = 0.9, β2 = 0.999, ε = 1e-8Those two running averages are stored in 32-bit precision for every trainable weight: 8 bytes per weight on top of the weights and gradients themselves. That is most of why full fine-tuning needs so much memory, and why LoRA, which trains only a small adapter, needs so little.
From steps to a finished model
- Steps add up to epochs. One pass over every training example is an epoch; a fine-tuning run is usually one to a few epochs.
- The learning rate changes during the run. A schedule usually ramps it up briefly (warm-up) and then lowers it toward zero, so early steps don't destabilise the model and late steps make fine adjustments.
- Validation runs alongside. Every so often the model is scored on held-out examples without updating any weights. That validation loss shows whether it is learning the pattern or memorising your examples.
- The result is new weights: either a complete updated model, or a small adapter that is applied on top of the unchanged base model.
Common mistakes
- Reading the loss as a quality score. Loss says how well the model predicts your target tokens, not whether its answers are good; an evaluation answers that.
- Comparing loss values across different base models. Each has its own tokenizer, so the numbers are not on the same scale.
- Expecting training to fix wrong targets. The loop faithfully lowers the loss on whatever replies you mark as targets, mistakes included.
- Assuming the model learns rules. It learns tendencies from many small nudges, so a behaviour shown in only two examples is barely learned.
References and further reading
External sources open in a new tab.
- 01Optimizing Model ParametersPyTorch Tutorials · pytorch.orgThe loop in about twenty lines of code: forward pass, loss, backward pass, optimizer step.
- 02Linear regression: Gradient descentGoogle Machine Learning Crash Course · developers.google.comAn interactive introduction to gradients and loss on the simplest possible model.
- 03Adam: A Method for Stochastic OptimizationKingma & Ba, 2014 · arXiv · arxiv.orgThe original Adam paper, with the update rule and its default settings.
- 04Decoupled Weight Decay RegularizationLoshchilov & Hutter, 2017 · arXiv · arxiv.orgIntroduces AdamW and explains why weight decay should be separated from the gradient.
- 05Cross-entropyWikipedia · en.wikipedia.org