Skip to content
AI360XpertBeta

Domain Adaptation

A model trained on one data distribution gets deployed on a shifted one, and domain adaptation closes that gap without collecting new labels.

Two classes shift right by 3 under a new camera so a threshold at 0 scores 0.67, and subtracting the mean gap of 3.0 restores 1.00 without any new labels
Two classes shift right by 3 under a new camera so a threshold at 0 scores 0.67, and subtracting the mean gap of 3.0 restores 1.00 without any new labels

Why Does This Exist?

A factory defect detector trained on thousands of labeled photos under one lighting rig. The factory upgrades its cameras: brighter light, different color temperature, a slightly new angle. The defects look the same to a person. The model's accuracy collapses anyway.

Nothing about the task moved. Still two classes, still the same definition of defective. What moved is what the inputs look like: covariate shift, a mismatch between the source domain of training and the target domain of deployment. Fresh labels under the new camera would fix it, at exactly the labeling cost this technique exists to avoid.

This page covers the no-new-labels fix. The labeled-data cousin is transfer learning. The case where the task definition itself moves is concept drift. The monitoring that catches the shift early is data drift detection.

Think of It Like This

A darts player under a moved lamp

A darts player practices for a year under one pub lamp and learns exactly where to stand and how the board shadows fall. The pub replaces the lamp, two feet to the right. The player's arm did not get worse, but every throw lands left of where the old shadows said it would.

Relearning to throw from nothing would waste the year. Instead the player measures one thing, how far the light moved, and shifts stance by exactly that. Domain adaptation is that measured sidestep: the skill transfers, only the frame moved.

Where it stops: a lamp moves everything rigidly, while real shifts also stretch and warp the data. A pure sidestep then recovers only part of the loss, and the rest needs heavier machinery.

How It Actually Works

Naming the shift: covariate shift versus concept shift

Let xx be the input and yy the label. Covariate shift means P(x)P(x), what inputs look like, changes between source and target, while P(y∣x)P(y \mid x), what each input means, stays fixed: new lighting, same defects. This is the case this page solves. Concept shift means the meaning itself changed: what counted as defective no longer does. Aligning input statistics cannot fix a changed definition, and trying can make things worse by forcing genuinely different situations to look the same.

Feature alignment: making domains indistinguishable

One family of methods trains the model's internal features so source and target examples become statistically indistinguishable. Domain-adversarial training does this directly: beside the main task, a small classifier guesses which domain each internal representation came from, while the main network learns features that make that classifier fail. If no classifier can tell the domains apart at the feature level, behavior learned on the source domain transfers, because the target now looks like more of the same.

Worked example: a shift of 3, a threshold at 0

Six source points with labels, three per class, and a rule that predicts class B when x>0x > 0:

xsrc=[−3,−2,−1,1,2,3],y=[0,0,0,1,1,1]x_{src} = [-3, -2, -1, 1, 2, 3], \quad y = [0, 0, 0, 1, 1, 1]

Every point lands on the right side of 0, so source accuracy is 6/6=1.006/6 = 1.00. The new camera shifts everything right by 3:

xtgt=[0,1,2,4,5,6]x_{tgt} = [0, 1, 2, 4, 5, 6]

The rule says x>0x > 0 strictly, so the first point at exactly 0 now reads as class A wrongly, and the rest stay right: accuracy falls to 4/6≈0.674/6 \approx 0.67. The diagram shows these two rows of dots around the threshold line.

Now recenter. The source mean is 0.00.0 and the target mean is 3.03.0, so the gap is 3.0−0.0=3.03.0 - 0.0 = 3.0. Subtract it before scoring:

xtgt−3.0=[−3,−2,−1,1,2,3]x_{tgt} - 3.0 = [-3, -2, -1, 1, 2, 3]

That is the source row back exactly, so accuracy returns to 6/6=1.006/6 = 1.00 with zero new labels. One subtraction fixed a pure translation, which is why recentering is always the first thing to try.

Why this differs from ordinary transfer learning

Transfer learning assumes at least some labeled target data to fine-tune on, even if far less than training from scratch would need. Domain adaptation is the harder unsupervised case: the target domain has no labels at all, and the only leverage is the assumption that the task stayed fixed while the inputs moved.

Code

The point is the check: replay the shift and the one-line fix, and confirm 1.00, then 0.67, then 1.00.

src = [-3, -2, -1, 1, 2, 3]labels = [0, 0, 0, 1, 1, 1]
def accuracy(xs: list, ys: list) -> float:    preds = [1 if x > 0 else 0 for x in xs]    return sum(p == y for p, y in zip(preds, ys)) / len(ys)
print(f"source accuracy: {accuracy(src, labels):.2f}")  # -> source accuracy: 1.00
tgt = [x + 3 for x in src]  # camera shift of +3print(f"shifted accuracy: {accuracy(tgt, labels):.2f}")  # -> shifted accuracy: 0.67
gap = sum(tgt) / len(tgt) - sum(src) / len(src)print(f"mean gap: {gap:.1f}")  # -> mean gap: 3.0print(f"recentered accuracy: {accuracy([x - gap for x in tgt], labels):.2f}")  # -> recentered accuracy: 1.00

Change the shift from a rigid +3+3 to a stretch, such as doubling every value, and rerun: recentering alone no longer restores 1.00, which is exactly when adversarial alignment earns its keep.

Watch Out For

Applying domain adaptation to concept shift instead of covariate shift

Every method here assumes the input-output relationship stayed fixed and only the inputs moved. If the task itself genuinely changed, aligning feature statistics will not repair a broken label relationship and can force different situations together. The symptom is adaptation that improves a distribution metric while target accuracy keeps falling. Fix: confirm with concept drift checks whether the meaning moved before aligning anything.

Trusting mean-recentering when the shift is not a translation

Mean-shift repair works when the target is approximately a shifted copy of the source. If the shift also changes spread or shape, one subtracted mean helps only partly and can fake a success. The symptom is source-like aggregate statistics beside stubbornly low sliced accuracy. Fix: score a held-out labeled slice of the target with data drift detection habits instead of assuming the one-line fix generalized.

The Quick Version

  • Domain adaptation fixes covariate shift: inputs change between training and deployment while the true input-output relationship stays fixed.
  • Concept shift, where the task definition itself changes, needs different handling and no input alignment fixes it.
  • Domain-adversarial training pushes features toward a form a domain classifier cannot distinguish, so source behavior transfers.
  • In the worked example a shift of +3 drops a threshold rule from 1.00 to 0.67, and subtracting the mean gap of 3.0 restores 1.00.
  • The setting is unsupervised: no target labels exist, which separates it from transfer learning with fine-tuning data.
  • Recentering is the first fix to try and the first to distrust when the shift stretches rather than slides.