Foundations to Frontier
Tests
E. Deep Learning › E19 Continual, Multi-Task and Transfer Learning

Domain Adaptation: DANN and MMD-Based Methods

Domain adaptation trains a model on labelled data from one distribution (the source) so that it works on a different, unlabelled distribution (the target). Examples are simulation to reality, one satellite sensor to another, or daytime to night-time images. MMD-based methods add a loss that makes source and target feature distributions match. DANN (Ganin et al., 2016) trains a domain classifier and reverses its gradient into the features, so they become domain-indistinguishable. This page covers covariate shift, the Ben-David bound, MMD, CORAL, adversarial adaptation and its failure modes. In the exercises you implement linear and RBF MMD and the gradient reversal layer. You then adapt a classifier from two-moon data to a 40°-rotated, unlabelled copy. Source-only training scored 0.72 on the target, MMD alignment 0.88, DANN 0.85, and an oracle trained on target labels 1.00. Across seeds the gains ranged from +0.04 to +0.22. Matching feature means exactly (MMD gap 0.0003) did not guarantee the best accuracy.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Same task, different-looking data

A model trained on simulated images often fails on real camera images, even though the task is the same. The inputs simply look different. Domain adaptation uses unlabelled data from the new setting to fix this. It trains the network so that its internal features look the same for both kinds of data, while still solving the task on the labelled kind. The demo measures the gap between two sample clouds as one is shifted: the quantity MMD-based methods drive down.

Transfer and efficiency lab
Squared MMD between source (green) and target (red) samples: 0.281. Domain adaptation trains features that make this gap small while still classifying the source well.
Why engineers care

Sim-to-real transfer for robots and drones, adapting Earth-observation models to a new satellite, and moving vision systems between lighting or weather conditions are all domain adaptation problems.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Domain adaptation uses target data that is…
  2. 2. It tries to make features…
  3. 3. A classic example:

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Setting: labelled source samples from P_S(x, y) and unlabelled target samples from P_T(x). Covariate shift means P(y | x) is shared but P(x) differs.
  • Ben-David bound: target error ≤ source error + ½ d_HΔH(S, T) + λ*.
    • The divergence term measures how distinguishable the domains are to the hypothesis class.
    • λ* is the best achievable joint error.
    • For example, 0.05 + ½·0.4 + 0.02 = 0.27.
  • MMD: MMD² = E k(s, s′) + E k(t, t′) − 2 E k(s, t). With a linear kernel it is ‖μ_S − μ_T‖², the gap between feature means (for example 3² + 4² = 25).
  • CORAL: match second-order statistics with ‖C_S − C_T‖²_F / (4d²).
  • DANN: a domain classifier D(f) learns to tell domains apart. A gradient reversal layer multiplies its gradient by −λ on the way into the feature extractor, so the features learn to fool it. λ ramps up as 2/(1 + e^(−10p)) − 1 with training progress p.
  • Proxy A-distance: 2(1 − 2·err) of a domain classifier; 0 means indistinguishable.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Linear-kernel MMD² equals…
  2. 2. The gradient reversal layer multiplies gradients by…
  3. 3. Proxy A-distance with domain-classifier error 0.5:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Domain adaptation in code

Exercise 1.

  • Linear and RBF MMD grow with the shift between samples.
  • Gradient reversal flips and scales gradients.

Practice.

  • Libraries: Transfer-Learning-Library (tllib), ADAPT and PyTorch-DA.
  • A gradient reversal layer is a three-line torch.autograd.Function.
  • Use a multi-kernel MMD (several bandwidths) on the last hidden layer.
  • Choosing λ and stopping without target labels is hard. Hold out a small labelled target set if at all possible, or use proxy criteria (domain-classifier accuracy, entropy) with care.
class GradReverse(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, lam): ctx.lam = lam; return x.view_as(x)
    @staticmethod
    def backward(ctx, g): return -ctx.lam * g, None
loss = ce(clf(f(xs)), ys) + bce(dom(GradReverse.apply(f(torch.cat([xs, xt])), lam)), domain_labels)
🐍 Exercise 1: MMD and gradient reversal
Implement linear and RBF MMD and the gradient reversal backward pass.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Multi-kernel MMD uses…
  2. 2. The hardest practical issue is…
  3. 3. Gradient reversal's forward pass is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why alignment helps. The Ben-David bound says target error is controlled by source error plus how distinguishable the domains are in feature space. MMD and DANN reduce the divergence term while the classification loss keeps source error low.

Why alignment can fail. The bound also contains λ*, the best joint error. Aligning marginal feature distributions does not guarantee that class-conditional distributions align. Features can map source class 0 onto target class 1 and still match perfectly (Zhao et al., 2019). Label shift (different class proportions) also breaks marginal matching. Conditional and class-aware variants (CDAN, pseudo-labelling) address this.

MMD as a two-sample test. With a characteristic kernel (e.g. RBF), MMD = 0 if and only if the distributions are equal. With a linear kernel only the means are compared, a much weaker condition.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Aligning marginals can still fail because…
  2. 2. Linear-kernel MMD = 0 means…
  3. 3. RBF-kernel MMD = 0 means…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Do feature alignment methods recover accuracy under domain shift?

History.

  • S. Ben-David et al., “A theory of learning from different domains” (Machine Learning, 2010).
  • M. Long et al., “Learning transferable features with deep adaptation networks” (ICML 2015; multi-kernel MMD).
  • Y. Ganin et al., “Domain-adversarial training of neural networks” (JMLR 2016).

Exercise 2.

  • Data: 400 labelled source samples from the two-moons problem, and 400 unlabelled target samples from the same moons rotated by 40°. Tested on 1000 target samples.
  • Network: one tanh feature layer (32 units) and a logistic classifier; Adam for 1500 steps.
  • Methods:
    • source only;
    • linear-kernel MMD on features (λ = 1);
    • DANN with a logistic domain classifier and gradient reversal (λ = 1);
    • an oracle trained on target labels.
method target accuracy source accuracy feature mean gap
source only 0.717 1.000 1.421
MMD 0.884 1.000 0.0003
DANN 0.854 1.000 0.252
oracle (target labels) 0.998 0.963 0.275
  • Both alignment methods help: +0.17 (MMD) and +0.14 (DANN) on the target without any target labels, while keeping the source perfect.
  • A gap to the oracle remains: marginal alignment does not guarantee each class lands in the right place.
  • Matching means is not the goal in itself: MMD drives the mean gap to almost zero, yet the oracle’s features are far from mean-matched (0.275).
  • Seeds vary a lot:
seed source only MMD DANN
1 0.648 0.685 0.743
2 0.682 0.818 0.898

Gains range from +0.04 to +0.22, and which method wins changes with the seed. Adaptation is fragile without target labels to tune it.

🐍 Exercise 2 (research): do feature alignment methods recover accuracy under domain shift?
Adapt a two-moons classifier to a 40°-rotated unlabelled target with source-only training, linear MMD and DANN; compare with an oracle.

Domain adaptation in AI, robotics and aerospace

  • Sim-to-real: training perception and control in simulation, then adapting to real sensors (often combined with domain randomisation).
  • Earth observation: moving land-cover models between satellites, seasons and regions.
  • Fault detection: transferring vibration-based diagnostics between machines or operating conditions.
  • Design lesson: alignment helps but is fragile; keep a small labelled target set for validation.

References

  1. S. Ben-David et al. (2010). “A theory of learning from different domains.” Machine Learning 79.
  2. Y. Ganin et al. (2016). “Domain-adversarial training of neural networks.” JMLR 17.
  3. M. Long, Y. Cao, J. Wang and M. I. Jordan (2015). “Learning transferable features with deep adaptation networks.” Proc. ICML.
  4. H. Zhao, R. T. des Combes, K. Zhang and G. Gordon (2019). “On learning invariant representations for domain adaptation.” Proc. ICML.