Concepts / Pre-Trained Language Models

Pre-Trained Language Models

Fine-tuning adjusts a pre-trained LLM to a specific task, here converting active sentences to passive voice, using a single training call that sets the optimizer, epochs, and evaluation settings.

  • Programming

Why Start From a Model That Already Knows Language

Imagine you want a program that can write fluent sentences. You could try to write rules for grammar and word choice by hand, but language is too varied for that to work. A different approach is to let a model learn patterns from a large body of text and then use what it learned to generate new text. The process of building that learned starting point is called pretraining. It is the stage where a model is first trained on text before anyone asks it to do a specific job.

A pre-trained language model already knows a great deal about language, but it has not yet learned to follow the kind of instructions you care about. Fine-tuning closes that gap by adjusting the pre-trained model so that it fits a specific task or dataset. In this article the task is concrete: the model is trained on instruction data until it can convert an active sentence into its passive voice counterpart. The article follows one path, from the foundations that come before pretraining, through the cost decision of training from scratch versus loading open weights, to the fine-tuning call and the way you judge its result.

Pretraining in the Course Path

In this course, pretraining is treated as the second major stage of work. The first stage prepares the ingredients: it covers the data preprocessing steps and the attention mechanism that sits at the core of modern LLMs. The second stage puts those ingredients together, showing how to code and pretrain a GPT-like model that can generate new text, and then how to evaluate the result. Keeping these stages in mind helps you see why each topic exists.

Raw textcollected materialData preprocessingstage 1Attention mechanismstage 1Pretrainingstage 2Evaluationboth routesLoad open weightsalternative routeprepared for learningdesign ingredientworking partalternativemeasuredmeasured
What comes before pretraining, and what comes after it, and how these steps feed into each other?

Read the diagram from left to right. The first two steps belong to stage 1 and are about preparing data and understanding attention. The pretraining step belongs to stage 2. The branch for loading open model weights sits beside pretraining because it is an alternative way to reach a trained model without running the full training yourself. Both routes end at evaluation, because a model needs to be measured whichever way it was obtained.

Ingredients From Stage One

Stage 1 supplies two foundations that pretraining depends on. Each is worth understanding before you move on to the model itself, because the later steps assume they are already in place.

Training From Scratch Versus Loading Open Weights

Pretraining an LLM from scratch is a significant undertaking. The source states that it demands thousands to millions of dollars in computing costs for GPT-like models. That range is wide, and the bottom and top ends differ enormously, so the practical lesson is that full-scale pretraining is a major commitment rather than a routine exercise.

This cost is the reason the course narrows its goal. Instead of pretraining at full scale, stage 2 focuses on implementing training for educational purposes with a small dataset. Loading openly available model weights is the other route. The table compares the two options so you can see what each one is for.

AspectTraining from scratchLoading open model weights
CostThousands to millions of dollars in computing for GPT-like modelsAvoids the full cost of running pretraining yourself
PurposeFull-scale pretraining is a major commitmentReach a trained model without running the full training yourself
Course focusStage 2 uses a small dataset for educational purposesAn alternative route to a trained model
Required next stepEvaluationEvaluation

Two routes to a trained model. Both routes still require evaluation.

Reading the Fine-Tuning Call

By the time training starts, the model and its data loaders are already prepared. What remains is a single training call that wires everything together. Because that call is where fine-tuning actually happens, it is the best place to begin. Treat it as a checklist of decisions: which optimizer updates the weights, how many passes over the data to make, how often to check progress, and what to ask the model to generate along the way.

Start by reading the optimizer line. It creates an AdamW optimizer over the model's parameters, which means the optimizer is the component that will update the model during training. Two of its settings are stated explicitly: the learning rate is 0.00005 and the weight decay is 0.1. Those two numbers, together with the optimizer choice, make up the optimizer configuration for this fine-tuning run.

AdamW optimizerupdates model parametersLearning rate 0.00005optimizer settingWeight decay 0.1optimizer settingEpochs 2passes over the dataEvaluation frequency5check intervalEvaluation iterations5checks per evaluationStarting contextfirst validationinstructionTokenizerencodes and decodes text
Which parameter in the training call sets the optimizer, the learning rate, the weight decay, the epoch count, and the evaluation settings?

The remaining arguments decide the shape of the run. The number of epochs is set to 2. The evaluation frequency is 5, and the evaluation iteration count is also 5, so the run checks its progress at regular intervals rather than only at the end. The call also receives a starting context, built by formatting the first validation instruction, which the model uses to generate a response while training is underway. The tokenizer is passed in so that text can be encoded and decoded during this process.

The call also returns three things you will read afterward: a list of training losses, a list of validation losses, and a count of tokens seen. The script records the start and end time around the call and reports the elapsed minutes. Timing does not change what the model learns, but it is useful when you compare runs.

Predicting the Loss Curves

Two kinds of loss are tracked. The training loss is computed on the data the model is learning from. The validation loss is computed on held-out instruction examples. Both matter. A model whose training loss falls while its validation loss does not is not doing what you want, so the two must be read together.

Before you look at the output of any run, decide what a healthy result would look like. You have the configuration in front of you: two epochs, evaluation every five iterations, and two loss curves that the run will produce. Commit to an answer first, because the reasoning you do now is what makes the output meaningful when it arrives.

What do you think happens?

Across the two epochs of this run, how should the training loss and the validation loss change?

Reveal answer
Answer:

Both should decrease consistently across the two epochs.

The source reads consistently decreasing training and validation loss across the two epochs as evidence that the model is learning effectively within two epochs. What matters is the direction and consistency of the change, not a single lower value.

Judging Whether a Run Succeeded

Once the run has finished, the losses are your main evidence. The source treats a steady decrease as the signal of improvement. A single lower number at one checkpoint is weaker evidence than a sequence of values that keeps falling as training continues. Look at the whole run, and then check the responses.

The training call sends a starting context built from the first validation instruction, and the responses generated at the end of each epoch let you see the model's behavior directly. This is the most readable check in the whole run, because you can compare the model's answer against what the task asked for. For this task, that means asking whether the answer is the passive voice counterpart of the active sentence.

Worked example

Deciding whether a two-epoch run succeeded

A run uses two epochs, evaluation every five iterations, and produces both loss lists. Training loss falls at each checkpoint, validation loss falls at each checkpoint, and the generated response turns an active sentence into a correct passive sentence. Did the run succeed?

  1. 1
    Check the configuration

    Confirm the run used two epochs and evaluation every five iterations, as the call specifies. The settings match the plan.

  2. 2
    Check the training loss

    The training loss keeps decreasing across the run, which shows a consistent direction rather than one lucky checkpoint.

  3. 3
    Check the validation loss

    The validation loss also decreases consistently, so the improvement is not limited to the data the model learns from.

  4. 4
    Check the generated response

    The response matches what the task asked for, which confirms the loss values in a way you can read directly.

  5. 5
    Decide on more epochs

    The losses are still falling and the responses are correct within two passes, so there is no evidence that a third epoch is needed.

Result

The run succeeded. Both losses decreased consistently, the generated response is correct, and two epochs were sufficient. Adding epochs without evidence would not be justified.

The configuration uses exactly two epochs, and the source explains why that choice is reasonable. Continuing beyond that point is not essential and may even hurt, since it can lead to overfitting, where the model fits the training examples so closely that it generalizes less well. Decide epoch counts from the results rather than by habit.

Finding Where a Run Goes Wrong

Sometimes a run departs from expectation. The useful habit is to check in a fixed order: the configuration first, then the metrics, then the generated outputs. Each check rules out a stage. If the configuration does not match what you intended, the problem is in the setup. If the configuration is right but the losses do not behave as described, the problem is in the training itself. If the losses look right but the responses are wrong, the problem is in what the model has learned to produce.

Check configurationoptimizer, rate, decay,epochs, evaluationSetup stage failedsettings differ from intentTraining stage failedlosses not fallingconsistentlyLearned behaviorfailedlosses fine, answers wrongCheck loss valuestraining and validationtogetherCheck generatedresponsescompare with the taskRun succeededall three checks agreemismatchmatchesinconsistentconsistentwrongcorrect
In what order do I check the configuration, the metrics, and the generated outputs, and which check tells me which stage failed?

The order matters because each check narrows where the fault lies. Reading the outputs first, before the configuration and metrics, tells you that something is wrong but not which stage caused it. Working in order turns a vague sense that the run failed into a specific stage you can correct.

Mistakes to Avoid When Fine-Tuning

  • Common mistake

    Judging a run by training loss alone

    What it looks like

    Training loss falls steadily, so the run is declared a success without checking validation loss.

    Why it fails

    The source bases its evaluation on both losses. A model whose training loss falls while its validation loss does not is not doing what you want.

    Do this instead

    Read training and validation losses together, then check the generated responses against the task.

  • Common mistake

    Adding epochs out of habit

    What it looks like

    Setting a third or fourth epoch because more training seems safer.

    Why it fails

    The source says two epochs were sufficient here and that continuing without evidence can lead to overfitting.

    Do this instead

    Decide the epoch count from the results of the run.

  • Common mistake

    Reading the loss curves before deciding what healthy looks like

    Why it fails

    Without a prediction, you cannot tell whether the output is meaningful or simply a number you accept.

    Do this instead

    Commit to an expected pattern from the configuration first, then compare the actual output with it.

  • Common mistake

    Treating a single lower loss value as proof

    Why it fails

    A single lower number at one checkpoint is weaker evidence than a consistent sequence of values.

    Do this instead

    Look at the direction and consistency of the change across the whole run.

  • Common mistake

    Skipping evaluation because the model was loaded rather than trained

    Why it fails

    Both routes still require evaluation. The weights came from elsewhere, so you cannot assume they behave as you expect in your own setting.

    Do this instead

    Measure any model you build or load before relying on it.

Why Evaluation Is Required

The third topic in stage 2 is evaluating LLMs. The source describes this as essential for developing capable NLP systems. A trained model is a set of learned values, and those values alone do not tell you whether the model produces good text. Evaluation is the step that measures what the model does, so that you can compare versions and decide what to change.

Evaluation applies to both routes. A model you pretrain on a small dataset needs to be evaluated, and so does a model whose weights you loaded from an open source. Because the weights in the second case came from elsewhere, you cannot assume they behave as you expect in your own setting. Measuring is how you find out.

Practice Reading a Run

EASY

Using only the configuration described in this article, list the optimizer, the learning rate, the weight decay, the epoch count, the evaluation frequency, and the evaluation iteration count. Then state the order in which you would check the run if its output departed from expectation.

Hints
  • The optimizer line names the optimizer and the two numbers stated explicitly with it.
  • The remaining arguments set the number of epochs and how often and how long progress is checked.
  • Work through configuration, then metrics, then generated outputs, and note which check would reveal each kind of failure.

Key Takeaways

  • Fine-tuning adjusts a pre-trained LLM to a specific task, here converting active sentences to passive voice, through a single training call that sets the optimizer, learning rate, weight decay, epochs, and evaluation settings.
  • Pretraining is the second major stage of the course, and it depends on the first stage's data preprocessing and attention mechanism. Training from scratch is costly, so the course uses a small dataset for education, and loading open weights is an alternative route.
  • Judge a run by reading training and validation losses together and confirming with the generated responses. Check configuration, then metrics, then outputs, and let the evidence decide the epoch count rather than habit.
  • Evaluation is required whether a model was pretrained or loaded from open weights, because measuring is how you learn what the model actually does.

Related concepts

More concepts starting with P