Search

Search papers and lessons

Domain Adaptation

Level 3 · 13 min read

Domain adaptation adjusts a model trained on labeled data from one setting so it works on a different setting where the labels are missing or scarce.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Neural network basics
  • Learning basics
  • ReadReady to readNot yet

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 xx and a label yy that is 0 or 1. The rule is that the label is 1 when xx is positive. The source domain has four labeled points, and a classifier that predicts 1 when x>0x \gt 0 gets all four right.

The target domain follows the same labeling rule, but its features are shifted by 3. The target points are x=1x = 1 and x=2x = 2 with label 0, and x=4x = 4 and x=5x = 5 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.

  1. Train on the source. The source points are x=−2x = -2 and x=−1x = -1 with label 0, and x=1x = 1 and x=2x = 2 with label 1. Fitting a rule fitted to the source gives a classifier that predicts 1 when x>0x \gt 0. Check: all four source points are classified correctly.
  2. Apply to the target without adapting. The target points x=1x = 1 and x=2x = 2 are positive, so the classifier predicts 1 for them, but their labels are 0. The points x=4x = 4 and x=5x = 5 are predicted 1 and labeled 1. Check: two of four target points are right, so accuracy is 50 percent.
  3. Measure the shift without labels. Compute the mean of the source features, which is (−2−1+1+2)/4=0(-2 - 1 + 1 + 2)/4 = 0. Compute the mean of the target features, which is (1+2+4+5)/4=3(1 + 2 + 4 + 5)/4 = 3. These means use no target labels. Check: the difference is 3, the size of the shift.
  4. Align the target. Subtract 3 from each target feature. The aligned points are −2-2, −1-1, 11 and 22, with the same labels as before. Check: the aligned target has the same mean as the source, 0.
  5. Apply the same classifier. The classifier predicts 1 for 11 and 22, and 0 for −2-2 and −1-1. 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

Definition: Source and target domains
A domain is a probability distribution over inputs and labels. The source is pS(x,y)p_S(x, y) and the target is pT(x,y)p_T(x, y). We have labeled source examples (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) drawn from pSp_S, and unlabeled target inputs x1′,…,xm′x'_1, \dots, x'_m drawn from pTp_T. The goal is a function ff with small target risk, which is the expected loss E(x,y)∼pT[ℓ(f(x),y)]\mathbb{E}_{(x, y) \sim p_T}[\ell(f(x), y)].

A common special case is Covariate Shift. The rule that gives labels from inputs is the same in both domains, so pS(y∣x)=pT(y∣x)p_S(y \mid x) = p_T(y \mid x), but the distribution of inputs changes, so pS(x)≠pT(x)p_S(x) \neq p_T(x). 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 ff to minimize the average loss

R^S(f)=1n∑i=1nℓ(f(xi),yi)\hat{R}_S(f) = \frac{1}{n} \sum_{i=1}^{n} \ell(f(x_i), y_i)

In the toy example R^S(f)=0\hat{R}_S(f) = 0 for the rule f(x)=1f(x) = 1 when x>0x \gt 0. The problem is that this average is over pSp_S, not pTp_T.

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 w(x)=pT(x)/pS(x)w(x) = p_T(x) / p_S(x). Then

EpT[ℓ(f(x),y)]=EpS[w(x) ℓ(f(x),y)]\mathbb{E}_{p_T}[\ell(f(x), y)] = \mathbb{E}_{p_S}[w(x)\, \ell(f(x), y)]

This holds because pT(x)p_T(x) is replaced by pS(x)p_S(x) times the ratio w(x)w(x), and the conditional p(y∣x)p(y \mid x) 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 ϕ\phi so that the feature distributions of the two domains are close. A simple measure is the difference of means. For the toy example ϕ(x)=x\phi(x) = x, and the alignment is

ϕT(x′)=x′−μT+μS\phi_T(x') = x' - \mu_T + \mu_S

where μS\mu_S and μT\mu_T 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

Example
The one-dimensional shift

This is the toy problem from "How it works," with the same numbers.

  1. Source points −2,−1,1,2-2, -1, 1, 2 with labels 0,0,1,10, 0, 1, 1 and classifier "predict 1 when x>0x \gt 0."
  2. Target points 1,2,4,51, 2, 4, 5 with labels 0,0,1,10, 0, 1, 1. Unadapted predictions are 1,1,1,11, 1, 1, 1.
  3. Compare each prediction with its label: 11 and 22 are predicted 1 but labeled 0, so they are wrong. 44 and 55 are right. Accuracy is 2/4=502/4 = 50 percent.
  4. Source mean 00, target mean (1+2+4+5)/4=3(1 + 2 + 4 + 5)/4 = 3.
  5. Aligned target points are 1−3=−21 - 3 = -2, 2−3=−12 - 3 = -1, 4−3=14 - 3 = 1, 5−3=25 - 3 = 2.
  6. Predictions on the aligned target are 0,0,1,10, 0, 1, 1, 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.

Example
Importance weights for two groups

A source sample has two kinds of input, x=0x = 0 and x=1x = 1, and the importance weights reweight it. In the source, pS(0)=0.8p_S(0) = 0.8 and pS(1)=0.2p_S(1) = 0.2. In the target, pT(0)=0.4p_T(0) = 0.4 and pT(1)=0.6p_T(1) = 0.6. The labeling rule is the same in both. A model has average loss 0.20.2 on inputs of type 0 and 1.01.0 on inputs of type 1.

  1. Weights: w(0)=0.4/0.8=0.5w(0) = 0.4 / 0.8 = 0.5 and w(1)=0.6/0.2=3w(1) = 0.6 / 0.2 = 3.
  2. Target risk directly: 0.4×0.2+0.6×1.0=0.08+0.6=0.680.4 \times 0.2 + 0.6 \times 1.0 = 0.08 + 0.6 = 0.68.
  3. Unweighted source risk: 0.8×0.2+0.2×1.0=0.16+0.2=0.360.8 \times 0.2 + 0.2 \times 1.0 = 0.16 + 0.2 = 0.36.
  4. Weighted source risk: 0.8×0.5×0.2+0.2×3×1.0=0.08+0.6=0.680.8 \times 0.5 \times 0.2 + 0.2 \times 3 \times 1.0 = 0.08 + 0.6 = 0.68.
  5. 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 1.01.0 on inputs of type 0 and 0.00.0 on inputs of type 1. What is the target risk, and what is the weighted source risk? Answer: the target risk is 0.4×1.0+0.6×0.0=0.40.4 \times 1.0 + 0.6 \times 0.0 = 0.4. The weighted source risk is 0.8×0.5×1.0+0.2×3×0.0=0.40.8 \times 0.5 \times 1.0 + 0.2 \times 3 \times 0.0 = 0.4, the same value.

Example
A spam filter across years

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.

  1. The source has labeled email from last year. The target has a large unlabeled stream from this year.
  2. 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.
  3. A feature map is learned on both streams together so that the two feature distributions are close. This uses no target labels.
  4. The classifier on top is trained with the source labels on the aligned features.
  5. 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.
  6. 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 95−70=2595 - 70 = 25 points, the recovered part is 85−70=1585 - 70 = 15 points, and 15/25=0.615 / 25 = 0.6, 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 p(y∣x)p(y \mid x) 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 pT(x)/pS(x)p_T(x) / p_S(x) 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 pT(x)/pS(x)p_T(x) / p_S(x).
  • 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
The input distribution p(x)p(x) changes, while the conditional p(y∣x)p(y \mid x), the labeling rule, stays the same.
QuestionA source has pS(x=0)=0.5p_S(x = 0) = 0.5 and the target has pT(x=0)=0.25p_T(x = 0) = 0.25, with pS(x=1)=0.5p_S(x = 1) = 0.5 and pT(x=1)=0.75p_T(x = 1) = 0.75. What are the importance weights for x=0x = 0 and x=1x = 1?Show answer
The weight for x=0x = 0 is 0.25/0.5=0.50.25 / 0.5 = 0.5, and the weight for x=1x = 1 is 0.75/0.5=1.50.75 / 0.5 = 1.5. Inputs that are more common in the target get weights above 1.
QuestionIn the one-dimensional example, the target features are shifted by 3 and the classifier is 'predict 1 when x>0x \gt 0'. Why does subtracting the target mean and adding the source mean fix the problem?Show answer
The shift in the example is a translation. Subtracting the target mean and adding the source mean moves the target features back onto the source features, so the classifier that works on the source also works on the aligned target. It uses target inputs only and no target labels.
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
Domain adaptation assumes that something is shared, such as the labeling rule or a common feature space. A new site can change the labeling rule itself, for example through different patient populations or different definitions, and aligning inputs cannot fix that. Each new target needs its own check on labeled samples.

Sources