Concepts / Using a Fine-Tuned Model for Classification

Using a Fine-Tuned Model for Classification

Once a spam classifier has been fine-tuned, the natural next question is whether it works.

  • Programming

Why One Accuracy Number Is Not Enough

Once a spam classifier has been fine-tuned, the natural next question is whether it works. A single accuracy number is tempting to report, but a fine-tuned model is judged on several datasets, and each one answers a slightly different question. The training set shows how well the model fits the examples it learned from. The validation set was used while development decisions were being made. The test set is held back so that it can serve as a fresh check on the finished model.

What do you think happens?

After fine-tuning, must the validation accuracy always be higher than the test accuracy?

  • Yes, validation always beats test

  • No, the ordering is a tendency rather than a rule

Reveal answer
Answer:

No, the ordering is a tendency rather than a rule

Validation accuracy is usually somewhat higher than test accuracy, because tuning targets the validation set. Whether it sits above or below test on a given run depends on what happened during development, so the ordering is evidence to interpret rather than a check that must pass.

Three Datasets, Three Questions

Each of the three datasets plays a different role, and understanding those roles is what makes the evaluation meaningful. The training data is the material the model learned from, so its accuracy describes how well the model fits examples it has already seen. The validation data is used during development: settings are adjusted and the model is compared against the validation set to see which choices work best. The test data is kept out of those decisions and is used at the end as a check on the finished model.

Training dataexamples the model learnedfromValidation dataused while makingdevelopment decisionsFinished modelevaluated on all threeTest dataheld back as a fresh checkfit shown by training accuracysettings chosen against itneutral final check
How each dataset relates to the finished model: training data shapes the fit, validation data guides development choices, and the held-back test data checks the finished model.

The evaluation is run in a deliberate order. Accuracy is computed on the training, validation, and test loaders, and each value is printed as a percentage with two decimal places. The source notes that this evaluation runs across the entire dataset, which is why the code does not define an eval_iter value. Limiting the number of evaluation batches is a different choice, and it is not used here.

Reading the Gap Between Training and Test

Overfitting is indicated when training accuracy is significantly higher than test accuracy. The word significantly matters. A small difference between the two numbers is not evidence of overfitting. In the reported results, training and test accuracy sit close together, and the slight gap of 1.54 points between them suggests minimal overfitting of the training data.

Training accuracyclose to testTraining accuracyhigh ninetiesTest accuracy1.54 points lowerTest accuracyfar below training
A small drop from training to test accuracy is the ignorable pattern; a large drop, with training far above test, is the pattern that signals overfitting.
Worked example

Deciding whether a train-test gap needs attention

A fine-tuned classifier reports a training accuracy in the high nineties and a test accuracy far below that value. Does this pattern suggest overfitting, and what should be investigated?

  1. 1
    Identify the comparison

    The relevant comparison is training accuracy against test accuracy, because the definition of overfitting is stated in terms of training being significantly higher than test.

  2. 2
    Judge the size of the gap

    The gap is large, not slight. The source identifies a large difference, with a training score in the high nineties and a test score far below it, as the pattern to investigate.

  3. 3
    Conclude

    The pattern points toward overfitting. The model fits the examples it learned from much better than it generalizes to the held-back test set, so the result should be investigated rather than reported as a success.

Result

A large drop from training to test accuracy signals overfitting. A small drop, like the 1.54-point gap in the reported results, suggests minimal overfitting and can be ignored.

Why Validation Often Scores Higher

Development is an iterative process. Over many rounds of adjusting settings and comparing against the validation set, those choices are shaped to the validation data, so validation scores can drift upward in a way that the untouched test set does not reflect. The test set was not used to make decisions, so its score is a more neutral estimate of how the model will behave on messages it has never seen. This is why the expected pattern is validation somewhat higher than test.

The expectation is a tendency, not a rule. If a validation score comes out lower than the test score, that is not automatically a failure. It simply means the tuning effect was weaker in that run. The ordering should be read as evidence to interpret.

If the gap between validation and test accuracy is larger than you want, the source names two hyperparameters to examine: the dropout rate, set through drop_rate, and the weight_decay parameter in the optimizer configuration. Raising the dropout rate or the weight decay is the suggested adjustment, and the source notes that it could potentially reduce that gap. Each change should be re-measured, because the effect is not guaranteed.

Tracing a New Message to a Class Name

After evaluation, the model is put to use. The classify_review function takes a piece of text and returns the name of its class. Its parameters are the text, the fine-tuned model, the tokenizer, the device, an optional max_length, and a pad_token_id that defaults to 50256.

python
Notes on the code
  • text: the raw message to classify
  • model: the fine-tuned classifier
  • tokenizer: turns the text into token IDs
  • device: where the model computation runs
  • max_length: optional limit on sequence length
  • pad_token_id: defaults to 50256

The function follows the same preprocessing ideas used for the SpamDataset earlier in the workflow. The text is turned into token IDs, the model then predicts an integer class label, and the function returns the matching class name. Each stage hands its result to the next, so a new message moves through the classifier in a fixed sequence.

New messageraw text enters thefunctionTokenizerproduces input_idsTruncationcut to supported contextlengthFine-tuned modelpredicts an integer labelLabel to class namemaps the integer back to anameClass namefor example spam or notspamtextinput_idstoken IDsinteger labelclass name
The stages a single message passes through: the tokenizer produces input_ids, sequences longer than the supported context length are truncated, the model predicts an integer label, and the function maps that label back to a class name.

Notice that the classifier's output is a name, not a number. The integer label produced by the model is a stepping stone, and the function's last job is to map that label back to a readable class. This also means a wrong answer can come from several places: the text might be prepared differently from training, the truncation might cut off content that mattered, or the model might predict the wrong integer. Knowing the sequence of stages makes it easier to locate where a mismatch began.

Mistakes When Reading Evaluation Results

  • Common mistake

    Treating validation beating test as a required order

    What it looks like

    Expecting that validation accuracy must always exceed test accuracy, and flagging a run where test is higher as broken.

    Why it fails

    The ordering is a tendency. Validation is often somewhat higher because tuning targets it, but whether it sits above or below test depends on what happened during development.

    Do this instead

    Read the ordering as evidence to interpret. A validation score below the test score means the tuning effect was weaker in that run.

  • Common mistake

    Calling any train-test difference overfitting

    What it looks like

    Reporting overfitting because training accuracy is a little higher than test accuracy.

    Why it fails

    Overfitting is indicated when training accuracy is significantly higher than test accuracy. A slight gap, such as the 1.54-point gap in the reported results, suggests minimal overfitting.

    Do this instead

    Judge the size of the gap. Investigate a large drop, and treat a small one as something that can be ignored.

  • Common mistake

    Using the test set to make development decisions

    Why it fails

    The test set was not used to make decisions, so its score is a neutral estimate. Using it for tuning would erode that role.

    Do this instead

    Make development choices against the validation set and keep the test set for the final check on the finished model.

  • Common mistake

    Reporting a single accuracy number without the training, validation, and test values

Interpreting a Train-Test Gap

MEDIUM

A fine-tuned spam classifier reports a training accuracy of 99 percent and a test accuracy of 62 percent. Does this pattern suggest overfitting? Explain using the rule that overfitting is indicated when training accuracy is significantly higher than test accuracy, and name one place in the workflow you would inspect next.

Hints
  • Compare the size of the gap, not just the direction.
  • Think about which dataset the model was never allowed to tune against.
  • The pipeline runs from raw text, through the tokenizer and truncation, to the model and label mapping.

Key Takeaways

  • Final evaluation measures accuracy on the training, validation, and test sets, and the test set serves as the held-back check on the finished model.
  • A large drop from training to test accuracy signals overfitting; a small drop, such as the 1.54-point gap in the reported results, suggests minimal overfitting.
  • Validation accuracy is often somewhat higher than test accuracy because tuning targets the validation set, but this is a tendency and not a guaranteed ordering.
  • The classify_review function turns a new message into token IDs, predicts an integer label, and maps that label back to a class name, so errors can be traced through those stages.

Related concepts

More concepts starting with U