Concepts / Fine-Tuning Large Language Models

Fine-Tuning Large Language Models

Evaluating a classification model involves calculating the classification accuracy.

  • Programming

Why a Pretrained Model Is Not Yet Enough

Imagine you have finished building a language model that has already gone through the earlier work of this course. It can produce fluent text, but it has not been shaped for any particular job. Ask it to sort customer emails into categories, or to answer a specific question about a product, and it has no particular reason to do either task well. This article follows one practical question: once a pretrained model exists, how do you turn it into one that performs a specific job?

What do you think happens?

A model that has only been pretrained is asked to sort customer emails into categories. How well would you expect it to do that job?

  • Very well, because it can write fluent text

  • Not necessarily, because it has not been shaped for that particular job

  • It would refuse the task entirely

Reveal answer
Answer:

Not necessarily, because it has not been shaped for that particular job.

The model can produce fluent text, but it has not been shaped for any particular job, so it has no particular reason to sort emails well. Closing that gap is the purpose of fine-tuning.

Where Fine-Tuning Fits in Building an LLM

The course builds an LLM in stages, and the third stage is the one this article covers. In that stage, the course takes a pretrained LLM and fine-tunes it. Read that sentence as a sequence: the pretrained model comes first, and fine-tuning is applied to it afterward. Fine-tuning is the step that comes after pretraining, and it is where the model starts to be aimed at real work.

Earlier coursestagesBuild the pretrained modelPretrained LLMStarting objectFine-tuning (Stage 3)Adjusts the model for namedtasksTask-ready LLMAimed at specific workproducesfine-tuning is applied to itresults in
What comes first, what comes next, and at which stage does fine-tuning happen after pretraining? Data moves from the pretrained model, through the fine-tuning step, to a model ready for specific tasks.

The diagram shows the order as a flow. Fine-tuning does not begin from an empty model. It begins from a pretrained one, so every fine-tuning step inherits whatever the pretrained model already knows how to do.

What Fine-Tuning Adjusts

Definition

Fine-tuning adjusts a pretrained model so that it follows instructions for named tasks. The starting object is a pretrained LLM, the action is adjustment, and the purpose is specific tasks.

Notice the two halves of this definition. The first half names the starting object and the action performed on it. The second half names the purpose. A learner who keeps both halves in mind is less likely to confuse fine-tuning with building a model from nothing. Fine-tuning changes an existing model; it does not create one from an empty starting point.

Pretrained and Fine-Tuned Models Compared

Setting the two models side by side makes the difference concrete. Both are LLMs, and both are built on the same foundation, but they differ in where they begin and what they are for.

FeaturePretrained LLMFine-tuned LLM
Starting pointThe model produced by the earlier stages of building an LLMA pretrained LLM that has been adjusted
What happens to itIt exists as a general model that can produce fluent textFine-tuning adjusts it so it follows instructions
PurposeGeneral language ability without a specific job in mindPerforming named tasks such as answering queries or classifying texts
Reason to do a given task wellNot shaped for any particular jobShaped for named tasks

Pretrained versus fine-tuned LLM by starting point and purpose

The table shows what changes between the two models. The starting point stays the same kind of object: a pretrained LLM. What changes is the adjustment applied to it and the purpose that adjustment serves.

The Two Task Families

The source names two example tasks for a fine-tuned model: answering queries and classifying texts. It explains why they matter: they are among the most common tasks in real-world applications and in research. Each task asks something different of the model. Answering a query asks the model to produce a response to a question. Classifying a text asks the model to assign an input to a category. Both are ways of following an instruction, which is the behaviour the fine-tuning stage aims to build.

Fine-tuned LLMtasksAnswering queriesProduce a response to aquestionProduct questionAnswer a specific questionabout a productClassifying textsAssign an input to acategoryCustomer emailsortingSort emails into categoriesincludesincludesexampleexample
Which practical problems belong under each task family, and how do they relate to fine-tuning? Both families are ways a fine-tuned model follows an instruction.

To match a practical problem to a family, ask what the model must produce. If the output is a response to a question, the problem belongs under answering queries. If the output is a choice of category for an input, it belongs under classifying texts. The two examples in the diagram are generated illustrations of these families, not tasks taken from the source.

What Classification Accuracy Measures

Once a classification model has been trained, the next question is whether it works. A classifier takes an input and assigns it to a category, such as spam or not spam, or a diagnosis label. Each assignment is either right or wrong, and the simplest way to summarize that behaviour is to count how often the model is right. This summary is called classification accuracy. Evaluating a classification model involves calculating the classification accuracy.

Rule

The formula is the definition written as a ratio. Count the predictions the model got right, divide by the total number of predictions it made, and the result is a value between 0 and 1. Multiply by 100 if you prefer a percentage. A result of 0.8 and a result of 80 percent describe the same accuracy.

Calculating Accuracy From Predictions

Consider a model that sorts ten emails as spam or not spam. Each email has a true label and a predicted label. Accuracy depends on lining up each prediction with the true label for the same email, then counting the rows where the two agree. In the diagram, each node is one email, its sublabel gives the true and predicted labels, and the emails the model got wrong are marked as changed.

Email 1true spam, predicted spamEmail 2true not spam, predictednot spamEmail 3true spam, predicted spamEmail 4true not spam, predictedspamEmail 5true not spam, predictednot spamEmail 6true spam, predicted spamEmail 7true not spam, predictednot spamEmail 8true spam, predicted notspamEmail 9true not spam, predictednot spamEmail 10true not spam, predictednot spam
How does each prediction line up with its true label, and which matches count toward the accuracy fraction? Emails 4 and 8 are the two mismatches.
Worked example

Accuracy for ten spam predictions

A model predicts a label for each of ten emails. Eight predictions match the true labels and two do not. What is the classification accuracy as a decimal and as a percentage?

  1. 1
    Line up the pairs

    Compare each prediction with the true label for the same email. Email 1 is truly spam and predicted spam, so it counts as correct.

  2. 2
    Count the matches

    Emails 1, 2, 3, 5, 6, 7, 9 and 10 match. That gives 8 correct predictions. Emails 4 and 8 are the wrong ones.

  3. 3
    Count the total

    The model made 10 predictions in all.

  4. 4
    Divide

    Accuracy is 8 divided by 10, which equals 0.8.

  5. 5
    Convert to a percentage

    Multiply 0.8 by 100 to get 80 percent.

Result

The accuracy is 0.8, or 80 percent. The number does not say which two emails were wrong; that information is in the rows, not in the fraction.

What Accuracy Does and Does Not Tell You

Accuracy is a single summary number, so it does not reveal which predictions were wrong or what kinds of errors the model made. In the email example, the calculation never asked which two emails were wrong. The number 80 percent would be the same whether the two errors were harmless messages or dangerous ones. That is the central limitation of accuracy, and it is why you should always ask what the errors were, not only how many there were.

Read a high value as evidence of frequent correct answers, not as proof that the model is good in every case that matters. Read a low value as evidence of frequent wrong answers, not as a diagnosis of what went wrong. In both cases the next step is to examine the predictions that were wrong.

Mistakes Beginners Make

  • Common mistake

    Thinking fine-tuning means building a model from an empty starting point

    What it looks like

    Assuming that a fine-tuned model was created from nothing and so has no earlier training.

    Why it fails

    Fine-tuning begins from the pretrained model rather than from an empty one.

    Do this instead

    Always identify the pretrained LLM as the starting object, then the fine-tuning step as the adjustment applied to it.

  • Common mistake

    Treating 0.8 and 80 percent as different accuracies

    What it looks like

    Concluding that a result of 0.8 is worse than a result of 80 percent.

    Why it fails

    Accuracy can be reported as a decimal between 0 and 1 or as a percentage by multiplying by 100. Both describe the same value.

    Do this instead

    Convert between the two forms by multiplying or dividing by 100 before comparing results.

  • Common mistake

    Judging a classifier by accuracy alone

    What it looks like

    Deciding a spam filter is safe because it is 80 percent accurate, without checking which emails it got wrong.

    Why it fails

    Accuracy is a single summary number that does not reveal which predictions were wrong or what kinds of errors the model made.

    Do this instead

    After computing accuracy, list the wrong predictions and ask what each error means for the task.

  • Common mistake

    Confusing the two task families by the input rather than the output

Practice Calculating and Matching

EASY

A classifier labels 20 support tickets and gets 15 of them right. What is its classification accuracy as a decimal, and what is it as a percentage?

Hints
  • Accuracy is correct predictions divided by total predictions.
  • Correct predictions are 15 and total predictions are 20.
  • Divide 15 by 20 to get the decimal, then multiply by 100 for the percentage.
MEDIUM

A help desk tool does two jobs. In the first, it answers a customer's question about a product. In the second, it tags each incoming message as billing or technical. Which job belongs to answering queries, which belongs to classifying texts, and why?

Hints
  • Ask what the model must produce: a response to a question, or a category for an input.
  • Answering a query asks the model to produce a response; classifying a text asks it to assign a category.

Key Takeaways

  • Classification accuracy is the number of correct predictions divided by the total number of predictions, reported as a decimal between 0 and 1 or as a percentage.
  • Accuracy is a single summary number, so it cannot show which predictions were wrong or what kinds of errors the model made.
  • Fine-tuning is the stage that comes after pretraining: it adjusts a pretrained LLM so it follows instructions for named tasks.
  • The two example task families for fine-tuned models are answering queries and classifying texts, and both are common in real-world applications and research.

Related concepts

More concepts starting with F