TL;DR
A model trained on one collection of data often fails on a different collection drawn from a somewhat different world: different cameras, hospitals, languages or years. Domain adaptation uses labeled data from the original setting, called the source, together with unlabeled data from the new setting, called the target, to make the model work on the target. The labels in the target are missing or too few to train on directly.
Builds on: Covariate Shift, Representation Learning, Empirical Risk Minimization · Leads to: Domain-Adversarial Training of Neural Networks, Out-of-Distribution Generalization
Where it's used
Image classifiers across camera types. A model trained on photographs from one phone camera can see a change in color balance, sharpness and framing when it runs on images from another phone. This is a shift in the input distribution. Labeling a new batch of target photos is slow, but collecting the unlabeled photos is cheap, so the target images can be used to adjust the model rather than retrain it from scratch.
Speech recognition across accents and microphones. A recognizer trained on read speech in a studio meets noisy phone calls and new accents. Teams usually have a large labeled set from the source and hours of unlabeled recordings from the target. Domain adaptation lets the unlabeled audio shape the model's internal features so that accents and noise matter less.
Medical imaging across scanners. Scans from one hospital's machine differ in resolution and contrast from scans made elsewhere. A model that was tuned on one scanner can drop in accuracy on another. Adapting the features with unlabeled scans from the new scanner is one of the approaches studied for this problem. This is an educational example about how the method is studied, not advice on any clinical use.
Text classification across years. A spam filter trained on email from one year sees new vocabulary and new tricks the next year. The labels for the old messages are known, which makes them supervised training data, and fresh messages arrive every day without labels. Domain adaptation treats the new messages as the target and the old labeled messages as the source.
How it works
We start with a tiny one-dimensional problem. Each example has a single feature and a label that is 0 or 1. The rule is that the label is 1 when is positive. The source domain has four labeled points, and a classifier that predicts 1 when gets all four right.
The target domain follows the same labeling rule, but its features are shifted by 3. The target points are and with label 0, and and with label 1. The rule is the same, but the points have moved. This is a Covariate Shift: the inputs move and the rule that gives labels stays put.
- Train on the source. The source points are and with label 0, and and with label 1. Fitting a rule fitted to the source gives a classifier that predicts 1 when . Check: all four source points are classified correctly.
- Apply to the target without adapting. The target points and are positive, so the classifier predicts 1 for them, but their labels are 0. The points and are predicted 1 and labeled 1. Check: two of four target points are right, so accuracy is 50 percent.
- Measure the shift without labels. Compute the mean of the source features, which is . Compute the mean of the target features, which is . These means use no target labels. Check: the difference is 3, the size of the shift.
- Align the target. Subtract 3 from each target feature. The aligned points are , , and , with the same labels as before. Check: the aligned target has the same mean as the source, 0.
- Apply the same classifier. The classifier predicts 1 for and , and 0 for and . Check: all four target labels are correct, and no target label was used to choose the adjustment.
The toy problem is solved by moving the target so that its features look like the source features. Real domain adaptation does the same thing in a learned feature space, and the next section writes the steps in symbols.
flowchart LR
S["Labeled source data"] --> T["Train classifier"]
T --> C["Classifier"]
U["Unlabeled target data"] --> A["Align features with the source"]
A --> C
C --> P["Predictions on the target"]
The math
A common special case is Covariate Shift. The rule that gives labels from inputs is the same in both domains, so , but the distribution of inputs changes, so . The one-dimensional example above is of this kind. In general the shift may also change the features, which is why representations are used.
Step 1 in symbols: learning from the source
The usual method is Empirical Risk Minimization on the source sample. Choose to minimize the average loss
In the toy example for the rule when . The problem is that this average is over , not .
Step 2 in symbols: reweighting under covariate shift
Under covariate shift, the target risk can be written as a source average with weights. Define the importance weight . Then
This holds because is replaced by times the ratio , and the conditional is the same in both domains. The estimate becomes a weighted average over the source sample. Weights are large for source examples that are rare in the source but common in the target, and small for the reverse.
Step 3 in symbols: aligning features
When the shift is not a pure reweighting, the approach is to find a feature map so that the feature distributions of the two domains are close. A simple measure is the difference of means. For the toy example , and the alignment is
where and are the source and target feature means. This uses only unlabeled target inputs. Richer methods replace the mean difference with a divergence such as the KL divergence between the two feature distributions, or train an adversary that tries to tell which domain a feature came from (gradient reversal is the standard trick for this). The model's features then do not reveal the domain, and the classifier on top is trained with source labels.
Worked examples
This is the toy problem from "How it works," with the same numbers.
- Source points with labels and classifier "predict 1 when ."
- Target points with labels . Unadapted predictions are .
- Compare each prediction with its label: and are predicted 1 but labeled 0, so they are wrong. and are right. Accuracy is percent.
- Source mean , target mean .
- Aligned target points are , , , .
- Predictions on the aligned target are , which match all four labels.
Interpretation: a translation of the features explains the whole failure, so removing it recovers the classifier with no target labels. Real shifts are rarely pure translations, which is why the next example uses reweighting.
A source sample has two kinds of input, and , and the importance weights reweight it. In the source, and . In the target, and . The labeling rule is the same in both. A model has average loss on inputs of type 0 and on inputs of type 1.
- Weights: and .
- Target risk directly: .
- Unweighted source risk: .
- Weighted source risk: .
- The weighted source risk equals the target risk. The unweighted risk, 0.36, underestimates the target risk by almost half.
Interpretation: the model is worse on the inputs that the target contains more of, and the weights make the source sample look like the target. Try it: suppose the loss is on inputs of type 0 and on inputs of type 1. What is the target risk, and what is the weighted source risk? Answer: the target risk is . The weighted source risk is , the same value.
Suppose a spam filter has accuracy 95 percent on held-out email from the year it was trained on. On email from the next year, its accuracy falls to 70 percent. These numbers are hypothetical, chosen to show the arithmetic.
- The source has labeled email from last year. The target has a large unlabeled stream from this year.
- The filter's features are the words and their frequencies. New words appear in the target, and some common words have changed meaning, so the target feature distribution has moved.
- A feature map is learned on both streams together so that the two feature distributions are close. This uses no target labels.
- The classifier on top is trained with the source labels on the aligned features.
- Suppose the adapted model reaches 88 percent on the target. The gap closes from 25 points to 7 points, so about 72 percent of the drop is recovered.
- Interpretation: adaptation does not reach the source accuracy, because some change is real, not a shift in how inputs look. Try it: suppose the adapted model reached 85 percent instead of 88. What fraction of the gap is recovered? Answer: the gap is points, the recovered part is points, and , so 60 percent.
Common mistakes and tradeoffs
It is tempting to think that domain adaptation fixes any change between domains. In fact it assumes that something is shared, most often that the labeling rule is the same, or that a shared feature space exists. If the rule itself changes, for example when the same words mean spam in one year and not the next, matching the inputs can make things worse, because the features now look alike while their labels differ.
It is tempting to think that making the feature distributions match is enough. In fact a feature map that matches the marginal distributions can map a target example of one class onto a source region of the other class. Matching must be combined with the source labels, so that the classifier keeps the class structure, and it is good practice to check accuracy on a small labeled target sample.
It is tempting to think that importance weighting is free. In fact the weights become very large where the source has few examples, which makes the weighted estimate noisy. A few heavy weights can dominate the average. Clipping the weights or regularizing the model reduces this, at the cost of some bias.
It is tempting to think that domain adaptation and domain generalization are the same. In fact adaptation uses unlabeled data from the target, while generalization has no target data at all and must work across unseen settings. If you can collect unlabeled target data, adaptation is the natural choice. If you cannot, methods from Out-of-Distribution Generalization apply instead.
Use domain adaptation when you have labeled source data, a stream of unlabeled target inputs, and a reason to believe the labeling rule carries over. Do not use it when the target has labels you can afford to collect; then Fine-Tuning on those labels is usually more reliable. For a small shift in the input distribution alone, reweighting with Importance Sampling ideas can be enough.
History
The idea of correcting for a change in input distribution by reweighting is older than the modern term. Hidetoshi Shimodaira described importance weighting for covariate shift in 2000, in the context of statistical model selection. In the mid-2000s, Shai Ben-David and coauthors gave theory that bounds the target error of a classifier by its source error plus a measure of how different the two domains are, and that theory guided feature-based methods. Interest grew with deep networks, because learned features made it practical to align distributions in a high-dimensional space.
In 2015 Yaroslav Ganin and Victor Lempitsky proposed domain-adversarial training, in which a feature extractor is trained so that a domain classifier cannot tell the source from the target features. That paper is the reference point for much of the later work. It caught on because it used standard Backpropagation and needed only unlabeled target inputs.
Recap
- Domain adaptation transfers a model from a labeled source to an unlabeled target when the two distributions differ.
- Under Covariate Shift the labeling rule is the same, and the target risk equals the source risk with importance weights .
- When the shift is broader, a feature map is learned so that source and target features look alike, and the classifier is trained with source labels.
- The method assumes something is shared between the domains. Check that assumption before relying on it.
Read next: Domain-Adversarial Training of Neural Networks for adversarial feature alignment, and Out-of-Distribution Generalization for the case where no target data is available.
Check yourself
QuestionIn a covariate shift (see the recap), which part of the joint distribution changes between the source and the target, and which stays the same?Show answer
QuestionA source has and the target has , with and . What are the importance weights for and ?Show answer
QuestionIn the one-dimensional example, the target features are shifted by 3 and the classifier is 'predict 1 when '. Why does subtracting the target mean and adding the source mean fix the problem?Show answer
QuestionA team says they used domain adaptation, so their model should work on any new hospital. Give one reason this is not guaranteed.Show answer
Sources
- Dataset shift (Wikipedia)
- Importance sampling (Wikipedia)
- Yaroslav Ganin, Victor Lempitsky, "Unsupervised Domain Adaptation by Backpropagation" and the follow-up "Domain-Adversarial Training of Neural Networks," 2015. arXiv:1505.07818