Concepts / Evaluating Model Generalization

Evaluating Model Generalization

Choosing parameters by test-set accuracy means the test data helps shape the model, so the resulting score can be overly optimistic.

  • Programming

A Score Worth Questioning

Suppose you have built a classifier, tried many different parameter settings, and found one that scores 97% accuracy on the data you were checking against. It is tempting to write that number in a report as the model's quality. This topic explains why that conclusion can be wrong even when every individual calculation was done correctly. The problem is not arithmetic. The problem is which data was used to make the choice.

What do you think happens?

The best of many tuned settings scored 97% on the test data. Is that number a fair description of how the model will behave on new data?

  • Yes, the model was tuned to do well, so the score is its true strength

  • Not necessarily, because the score was selected from many attempts measured on the test data

Reveal answer
Answer:

Not necessarily. The score was not produced by one fixed model. It was the best of many attempts, each judged on the test data, so it may overstate how well the model will do on new data.

The common belief treats the best test score as the model's real strength. That belief fails because the reported number came from a search over settings, and the search used the test data to decide which setting to keep.

Following the Test Data Through Tuning

Parameter tuning is a loop. You change a setting, evaluate the model, compare the result with the others, and keep the best. Each pass through the loop is a separate attempt. When the evaluation step uses the test data, the test data becomes part of the training process, even though the model never fits its parameters to it directly. Every time a setting is kept or discarded based on test accuracy, a little information about the test data flows into the final choice.

Change a settingEvaluate on test dataCompare test accuracyKeep best setting?Reported test scoretry settingtest accuracyrank attemptstry another settingbest attempt chosen
How does a loop that picks parameters based on test scores leak test information into the model and inflate the reported score?

Trace the arrows in the diagram. The test data reaches the comparison step on every pass, so the setting that survives is the one that fit the test data best. The final number that comes out of this loop has already been shaped by the test data. Nothing in that number tells you how the model behaves on data it has never been compared against.

Why Many Attempts Inflate a Score

The common belief goes like this: the model was tuned until it performed well on the test data, so its performance on the test data is a fair description of how it will behave later. Under that belief, the best test score is simply the model's true strength, and nothing more needs checking.

The belief breaks down because the score was not produced by a single fixed model. It was produced by a search. A score selected from many attempts is not the same kind of measurement as a score from one pre-decided model. When one attempt is picked because it looked best on the test data, the picking itself is part of the result, and the report usually hides it.

With many attempts, the highest test score is likely to be one where the chosen setting fit the particular quirks of the test set well. Those quirks are not guaranteed to appear in new data, so the score can overstate the model's ability to generalize. The source describes this as overfitting arising from parameter tuning: the model is shaped toward the test data rather than toward the underlying problem.

Why the Test Set Stops Being a Judge

A judge is only useful if it has not already seen the evidence it is weighing. In a sound workflow, the training data fits the model, and a separate dataset that played no role in fitting or tuning gives the final verdict. In the flawed workflow, the test data is used for both the choosing and the judging, so the judge has already influenced the outcome it is meant to assess.

Once the test data has been used for tuning, it can no longer be used to assess the model. It has moved from being the judge to being part of the decision-making. Its later accuracy is no longer an independent measurement, because the settings were partly chosen to match it.

The key practical point is that tuning and judging must use different data. If the test set has already guided parameter selection, it can no longer serve as the unbiased assessment. An unbiased estimate requires a dataset that stayed out of every decision made about the model.

Separating Tuning Data From Evaluation Data

Two roles are easy to blur together. One dataset helps choose parameters, and another dataset gives the final score. The table below contrasts them using the roles described in the source: the data used to make choices must be kept separate from the data used to judge the result.

QuestionTuning dataEvaluation data
What is it used for?Comparing parameter settings and choosing the best oneProducing the final performance estimate
Does it influence the final model?Yes, because its results decide which settings are keptNo, it must stay out of every decision
Can its score be reported as unbiased?No, the choices were made by looking at itYes, if it was never used to create or tune the model

Tuning data shapes the choice; evaluation data judges the outcome.

Tracing a Sound Workflow

A clear way to see the problem is to follow the data step by step. The diagram below traces a sound workflow. Training data fits the model. Tuning data guides the choice of parameters. A separate evaluation dataset gives the final score. Read the arrows from left to right and notice that no dataset is used in two roles.

Training dataFit modelTuning dataChoose parametersFinal modelEvaluation dataFinal scorefitscandidate modelscompares settingschosen settingsmeasuredjudges
Which data split is used at each stage, and in what order does data move from training to tuning to final evaluation?
Worked example

Following three datasets through one project

A team builds a classifier and wants an honest accuracy figure to report. They have three separate collections of labeled examples. Trace where each collection is used.

  1. 1
    Set aside the evaluation collection first

    Before any model is built, one collection is locked away. Nothing about the model will be decided by looking at it.

  2. 2
    Fit the model on the training collection

    The training collection is the only data used to fit the model's internal parameters.

  3. 3
    Tune on the tuning collection

    Several parameter settings are tried. Each is scored on the tuning collection, and the best one is kept. The tuning collection now influences the choice, so it cannot be the final judge.

  4. 4
    Score once on the evaluation collection

    The chosen model is evaluated a single time on the locked-away collection. That number is the one that can be reported as an estimate of performance on new data.

Result

Each collection played exactly one role. The reported score comes from data that influenced no decision about the model, so it is not inflated by the search.

Conditions for an Independent Dataset

An independent dataset is one that was not used to create or tune the model. Once the parameters have been chosen, that dataset can measure performance without the bias introduced by the search. The earlier split into training and test sets was made for the same reason: a dataset that never influenced the model is needed to evaluate it.

Training dataModelTuning dataParametersEvaluation dataUnbiased scorefitsguidessetsmeasuredjudgesmust not overlapmust not overlap
Which relationships between training, tuning, and evaluation data would make the evaluation dataset independent enough for an unbiased estimate?

Read the diagram as a checklist. The evaluation data must not feed the fitting relationship, must not feed the tuning relationship, and must not be a part of any decision about the parameters. When all of these hold, the evaluation data stayed out of every decision made about the model, and its score can be read as an unbiased estimate.

Mistakes That Reintroduce Bias

  • Common mistake

    Reporting the best test score after trying many settings as the model's quality

    What it looks like

    A classifier is tried with twenty parameter settings, and the best test accuracy is written into the report as its expected performance.

    Why it fails

    The reported number was selected from many attempts measured on the test data, so it can be overly optimistic.

    Do this instead

    Report a score from data that played no role in choosing the settings.

  • Common mistake

    Using the test set again as the final judge after it has guided tuning

    What it looks like

    The test set picks the best parameters, and then the same test set is used to report the final accuracy.

    Why it fails

    Once the test data has been used for tuning, it can no longer be used to assess the model.

    Do this instead

    Keep a separate evaluation dataset that was never used to make decisions.

  • Common mistake

    Assuming a dataset is independent just because it was split off at the start, without checking whether tuning later looked at it

Checking Your Workflow

MEDIUM

A colleague says: I tried fifteen parameter settings on our test set, picked the one with the highest accuracy, and that accuracy is what we will report to the client. Explain what is wrong with this plan, and describe which data should be used for the final score.

Hints
  • Ask which data influenced the choice of the reported setting.
  • Think about what it means for a score to come from a search rather than from one pre-decided model.
  • The source says an independent dataset is one that was not used to create or tune the model.
EASY

Sketch the three data roles for a new project in your own words: which dataset fits the model, which one guides parameter choices, and which one gives the final score. Then mark any arrow in your sketch that would let the final dataset influence a decision.

Hints
  • Each dataset should appear in exactly one role.
  • Any arrow from the evaluation dataset back into a tuning step breaks independence.

Key Takeaways

  • Choosing parameters by test-set accuracy means the test data helps shape the model, so the resulting score can be overly optimistic.
  • The best of many tuned results does not necessarily carry over to new data; this is overfitting caused by parameter tuning.
  • Once the test data has guided tuning, it can no longer serve as the unbiased assessment of the model.
  • An independent dataset, one that was not used to create or tune the model, is needed for an unbiased estimate of performance.
  • In a sound workflow, training data fits the model, tuning data guides parameter choice, and a separate evaluation dataset gives the final verdict.

Related concepts

More concepts starting with E