TL;DR
A model is usually trained and checked on data drawn from one source, and then used on data that may come from a different source. Out-of-distribution generalization asks how well the model still works in that new setting. Its score on held-out data from the same source can look excellent while its score in the new setting is much worse, so the two must be measured separately.
Builds on: Empirical Risk Minimization, Covariate Shift, Overfitting · Leads to: Transfer Learning, Adversarial Training
Where it's used
Medical models across hospitals. A model that reads scans or lab results is often trained on data from a few hospitals, which have their own scanners, patient mixes and recording habits. When the model is moved to another hospital, its accuracy can fall, even though the medical facts it learned are still true. Checking it only on a hold-out set from the first hospital hides the problem.
Spam and fraud filters. Messages and transactions change over time as senders adapt. A filter trained last year sees a different mix of tricks this year. The rule that a fraudulent transaction looks suspicious may still hold, but the inputs have moved.
Self-driving and robotics. A perception system, often a convolutional network, trained on daytime roads in one city meets snow, night and different road signs elsewhere. The model is asked to act well in conditions that were rare or absent in training, which is the central difficulty of this area.
How it works
Training uses a set of examples drawn from one source. The model is chosen to do well on that set, and then it is used on a new source. We trace one tiny case with round numbers.
Two kinds of patient come to a clinic. Call them group A and group B. Group A is common in the training data and group B is rare. A model is trained on the training data, and its accuracy is measured separately for each group.
- Describe the training data. Ninety per cent of the training examples are from group A and 10 per cent from group B. Check: the two shares add to 100 per cent.
- Measure the model on each group. The model is right on 95 per cent of group A examples and 60 per cent of group B examples. Check: group B is harder for it, which is what a rare group often looks like.
- Compute the training accuracy. Weight each group by its share of the training data: . Check: the training accuracy is 91.5 per cent, which looks strong.
- Describe the new setting. In the clinic where the model is now used, half the patients are from group A and half from group B. Check: the mix has changed, but the groups themselves are the same as before.
- Compute the accuracy in the new setting. Use the new shares: . Check: the accuracy is 77.5 per cent, which is 14 points below the training score.
- Read the gap. The model did not get worse at any group. The setting changed the weights, and the training score had quietly favoured the common group. This is the out-of-distribution problem in its simplest form.
The math
Let be the distribution that generated the training data, and the distribution in the new setting, where is an input and the correct output. For a model and a loss , which measures how wrong a prediction is, the training risk and the test risk are
Out-of-distribution generalization is the question of how small can be, given that was chosen to make small.
Empirical risk minimization chooses to make the average loss on the training sample small. That estimates . It says nothing about unless the two distributions are the same, which is the assumption that the new setting is just more of the old one.
Step 1 in symbols: the training distribution
In the walkthrough, group A has and group B has . The training accuracy is the expectation of the accuracy under :
This is Step 3 of the walkthrough.
Step 2 in symbols: the test distribution
The new setting has and , and the same group accuracies. So
The accuracies are unchanged. Only the weights moved. This is the situation called covariate shift when the groups are defined by the inputs.
Step 3 in symbols: importance weights
If we know the ratio of the two distributions, we can rewrite the test risk as a training-data average:
The weight is the importance weight. For the groups, and . Check: the weighted average of accuracies is , the same as Step 2.
Step 4 in symbols: why this needs an assumption
The rewriting in Step 3 needs , so that only the input weights change. If the rule that maps inputs to correct answers also changes, no reweighting of the training data can recover the test risk, because the training data describes the wrong rule. That case is called concept shift, and it is harder than covariate shift.
Worked examples
A model has accuracy 0.80 on group A and 0.40 on group B. Training data is 75 per cent group A and 25 per cent group B. The new setting is 25 per cent group A and 75 per cent group B.
- Training accuracy: .
- New-setting accuracy: .
- Check: the new accuracy is below the training accuracy, as expected when the rare group becomes common.
- The gap is , or 20 points.
Interpretation: the model did not change, and its accuracy on each group is the same in both settings. What changed is how much each group counts, so a single headline accuracy can mislead about what the model will do after the setting moves.
Four training examples have losses 0.2, 1.0, 0.5 and 0.0. Their probabilities under the training distribution are 0.4, 0.4, 0.1 and 0.1. Under the test distribution they are 0.2, 0.2, 0.6 and 0.0.
- Training risk: .
- Test risk, computed directly from the test probabilities: .
- Importance weights : , , , and .
- Weighted training risk: .
- Check: step 4 equals step 2.
Interpretation: with the ratio known, the training examples carry enough information to estimate the test risk. The example with weight 6 counts for much more than the others, which is why the weights matter.
A risk model is trained on records in which 15 per cent of patients are over 80. Its error rate is 5 per cent for patients under 80 and 20 per cent for patients over 80. These numbers are invented to show the arithmetic; they are not from a real model. The clinic where it is used has 40 per cent of patients over 80.
- Training error: , or 7.25 per cent.
- Error in the clinic: , or 11 per cent.
- Check: the clinic error is higher, and the mix explains why.
- The ratio of the two errors is , so the error is about 52 per cent higher than the training score suggests.
- A checker who only looked at the training records would have seen 7.25 per cent and no warning.
Interpretation: a model can keep its per-group accuracy and still lose headline performance when the mix of patients changes. Before relying on a model in a new setting, its error should be measured on data from that setting, broken down by group. Try it: suppose the clinic has 25 per cent of patients over 80 instead. What is the error? Answer: , or 8.75 per cent.
Common mistakes and tradeoffs
It is tempting to think a model that scores well on a held-out test set will also work in a new setting. In fact a held-out set drawn from the training source tests only the case where nothing has changed. It is a measure of in-distribution performance, and it can be very optimistic about the new setting.
It is tempting to think more training data fixes out-of-distribution failure. In fact adding more examples from the same source makes the model fit that source better, which can increase the gap. What helps is data from the new setting, or a model that depends on features that hold across settings.
It is tempting to think any unfamiliar input is out of distribution, and that the model should flag it. In fact a model's confidence is often unreliable far from its training data, so detecting shift needs its own tests. A reweighting scheme also has a cost: when some importance weights are very large, a few examples dominate the estimate and its variance grows, which is why the weights are often clipped.
Reweighting for covariate shift is the right tool when the input distributions differ but the labelling rule is the same, and when the ratio can be estimated. It is the wrong tool when the labelling rule changes, because no weighting of the old data gives the new rule. In that case, gather labelled data from the new setting and adapt the model, through Transfer Learning or Fine-Tuning. Adversarial training is a different tool: it makes the model robust to small, deliberate changes, not to broad changes in the source of the data.
History
Statisticians studied the problem for a long time under the name of sample selection bias. When the sample used to fit a model was not a random draw from the population of interest, estimates were biased. Econometricians, including James Heckman, developed corrections for this, and the issue became standard in that field.
In machine learning, the reweighting idea was developed as covariate shift in the early 2000s. Hidetoshi Shimodaira showed in 2000 that weighting the likelihood by the ratio of input densities gives better predictions when the inputs move, and the importance-weight form in this lesson comes from that line of work.
The problem became urgent in the 2010s, as models trained on large data sets were deployed in medicine, vision and language, where the new setting often differs from the training data. Researchers in computer vision showed that models trained on one set of photographs can fail badly on another, even when the task is the same. That observation pushed the field from measuring in-distribution accuracy toward measuring performance across settings.
Recap
- A model chosen by Empirical Risk Minimization is judged on its training source, which does not guarantee its score in a new setting.
- When only the inputs change, the test risk can be computed from the training data with importance weights.
- Reweighting fails when the labelling rule changes, and it can be unstable when weights are large.
- Measure performance on data from the new setting, broken down by group, before relying on a model there.
- Read next: Transfer Learning for adapting a model with new labelled data, and Adversarial Training for a different kind of robustness.