Domain Adaptation¶
- Domain adaptation is a data-driven method to adapt to shifts between train and test data
Domain Adaptation Definition¶
In domain adaptation, the training and test data distributions are different. In real-world applications, this is quite common ocurence.
We will consider only supervised learning tasks
Training data: $\mathcal{D}^\text{(tr)}=\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p^\text{(tr)}(x,y) = p^\text{(tr)}(y|x)p^\text{(tr)}(x)$.
Test data: $\mathcal{D}^\text{(te)}=\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p^\text{(te)}(x,y) = p^\text{(te)}(y|x)p^\text{(te)}(x)$.
Two simpler domain adaptation tasks:
- Covariate shift
- Only the distribution of $X$ changes between training and testing
- $p^\text{(tr)}(x,y) = p(y|x)p^\text{(tr)}(x)$.
- $p^\text{(te)}(x,y) = p(y|x)p^\text{(te)}(x)$.
- Extra data given in the form $\{x_j^\text{(te)}\}_j$ i.i.d. sampled from $p^\text{(te)}(x)$.
- Only the distribution of $X$ changes between training and testing
- Target shift (a.k.a. class imbalance in classification tasks)
- Consider a set of targets $\mathbb{Y}$
- Class imbalance is actually a case of target shift: $P^\text{(tr)}(y) \neq P^\text{(te)}(y)$
- Only the distribution of $Y$ changes between training and testing
- Note that the dataset distribution will change
- $p^\text{(tr)}(x,y) = p(x|y)p^\text{(tr)}(y)$.
- $p^\text{(te)}(x,y) = p(x|y)p^\text{(te)}(y)$.
- Mechanism shift
- In mechanism shift, $p^\text{(tr)}(x|y) \neq p^\text{(te)}(x|y)$
- This is the hardest type of data shift. We will not cover this shift.
Representation Learning View¶
- Consider a classification task with $C \geq 2$ classes.
- Through the universal approximation theorem of neural networks, there exists
- a representation function (neural network) $\Gamma$ with parameters ${\bf W}_{\Gamma}$ and
- a link function (also a neural network) $g$ with parameters ${\bf W}_{g}$
- such that: $$P(Y=y|X=x) = g(\Gamma(x; {\bf W}_{\Gamma}); {\bf W}_{g})_y.$$
Example: For image classification, $\Gamma(x;{\bf W}_\Gamma)$ can be the convolutional layers of a CNN. While $g$ can be the remaining fully connected layers of the CNN.
Overall domain adaptation approach¶
In this lecture we will explore methods that will allow us to perform domain adaptation.
Overall, these methods work in two ways:
- Either by making the training data look like the test data.
- Or, specific to covariate shift, by making the input representation robust to the shift in $p(x)$.
Method 1: Importance reweighting¶
The simplest way to make the loss over the training data to look like the loss over the test data is via importance reweighting.
- In what follows we will consider distributions over discrete spaces (although the results are valid for continuous probability distributions)
Importance reweighting transforms the average of a function ${L}$ over samples of a random variable $(X,Y) \sim P(x,y)$ into an average of ${L}$ over samples of $(X',Y') \sim Q(x,y)$.
- Requirement: $Q(x,y)$ and $P(x,y)$ must have the same support.
- The support of a distribution $P(x,y)$ is defined as $\operatorname{supp}(P)=\{(x,y) \in \mathbb{X}\times \mathbb{Y}\,|\,P(x,y) > 0\}$.
Importance reweighting approach:
- Consider a proposal distribution $(X',Y') \sim Q(x,y)$
- Consider a target distribution $(X,Y) \sim P(x,y)$
- Goal: estimate $E_{(X,Y) \sim P(x,y)}\left[{L}(Y,f(X))\right]$ using samples from $(X',Y')$
- For us:
- $Q(x,y)$ will be the training distribution
- $P(x,y)$ will be the test distribution
- $f$ will be a neural network with softmax outputs
- ${L}$ will be the loss function (the negative log-likelihood)
- For us:
- Importance reweighting: For any finite-valued functions $\mathcal{L}$, $f$, and equal support distributions $\operatorname{supp}(Q)=\operatorname{supp}(P)$, the following equality holds: $$E_{(X',Y') \sim Q(x,y)}\left[{L}(Y',f(X')) \frac{P(X',Y')}{Q(X',Y')} \right] = E_{(X,Y) \sim P(x,y)}\left[{L}(Y,f(X))\right]$$
Target shift adaptation via importance reweighting¶
Target shift:
- Consider a finite set of labels $\mathbb{Y}$
- Only the distribution of $Y \in \mathbb{Y}$ changes between training and testing
- $p^\text{(tr)}(x,y) = p(x|y)p^\text{(tr)}(y)$.
- $p^\text{(te)}(x,y) = p(x|y)p^\text{(te)}(y)$.
- Consider the empirical loss $$\mathcal{L}(\mathcal{D}^\text{(tr)}) \propto \frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} L(y^\text{(tr)}_i,g(\Gamma(x^\text{(tr)}_i))).$$
Importance reweighting of loss $\mathcal{L}$: $$ \mathcal{L}'(\mathcal{D}^\text{(tr)}) \propto \frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} \frac{p^\text{(te)}(y^\text{(tr)}_i)}{p^\text{(tr)}(y^\text{(tr)}_i)} L(y^\text{(tr)}_i,g(\Gamma(x^\text{(tr)}_i))), $$ which is also known as class rebalancing, creates an unbiased empirical estimate of the loss over label distribution $p^\text{(te)}(y)$.
- Sampling-based importance reweighting:
- "upsampling" (replicating cases from the minority) and
- "downsampling" (ignoring cases from the majority) are sampling-based procedures for importance reweighting
Importance reweighting drawbacks¶
- We must know $p^\text{(te)}(x)/p^\text{(tr)}(x)$ for covariate shift and $p^\text{(te)}(y)/p^\text{(tr)}(y)$ for target shift.
- Assume we know $p^\text{(te)}(y)/p^\text{(tr)}(y)$ is an OK assumption for target shifts in classification problems
- Often, we will just assume $p^\text{(te)}(y) = 1/\vert \mathbb{Y} \vert$.
- But this is rarely the case for covariate shifts
- Rather the be given $p^\text{(te)}(x)$ we are given samples from $X^\text{(te)} \sim p^\text{(te)}(x)$
Method 2: Shift-Invariant Representation Distributions¶
Covariate shift adaptation with samples from $X \sim p^\text{(te)}(x)$ but without $p^\text{(te)}(x)$¶
Recall task: Covariate shift
- Only the distribution of $X$ changes between training and testing
- $p^\text{(tr)}(x,y) = p(y|x)p^\text{(tr)}(x)$.
- $p^\text{(te)}(x,y) = p(y|x)p^\text{(te)}(x)$.
- Training data: $\mathcal{D}^\text{(tr)}=\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p^\text{(tr)}(x,y) = p^\text{(tr)}(y|x)p^\text{(tr)}(x)$
- Extra data given in the form $\mathcal{D}^\text{(adpt)} = \{x_j^\text{(te)}\}_j$ i.i.d. sampled from $p^\text{(te)}(x)$.
Rather than reweighting the samples in the loss, because we can't, we will adapt the representation.
The core idea is a follows:
- Let $\Gamma(X^\text{(tr)})$ be a random variable describing the representations given to samples from $p^\text{(tr)}(x)$
- Let $\Gamma(X^\text{(te)})$ be a random variable describing the representations given to samples from $p^\text{(te)}(x)$
- Then, $\Gamma$ must be such that $\Gamma(X^\text{(tr)}) | (Y^\text{(tr)}=y) \stackrel{d}{=} \Gamma(X^\text{(te)}) | (Y^\text{(te)}=y)$, $\:\forall y \in \text{supp}(Y^\text{(tr)}) \cap \text{supp}(Y^\text{(te)})$, where $\stackrel{d}{=}$ means equality in distribution.
- But note that we do not have $Y^\text{(te)}$.
WARNING:
- Common flaw of data-driven covariate shift adaptation methods
- But since we do not have $Y^\text{(te)}$ most papers try:
- $\Gamma(X^\text{(tr)}) \stackrel{d}{=} \Gamma(X^\text{(te)})$ is not quite what we needed to do
- The correct approach would be $\Gamma(X^\text{(tr)}) | (Y^\text{(tr)}=y) \stackrel{d}{=} \Gamma(X^\text{(te)}) | (Y^\text{(te)}=y)$, $\:\forall y \in \text{supp}(Y^\text{(tr)}) \cap \text{supp}(Y^\text{(te)})$.
- But since we do not know $Y^\text{(te)}$, we can only hope $\Gamma(X^\text{(tr)}) \stackrel{d}{=} \Gamma(X^\text{(te)})$ does the trick.
- Better-Than-Nothing™: Much like in transfer learning, this is better than nothing. And it tends to work OK in practice.
- Newer work addresses this issue (Ruan et al., 2022), also discussed in earlier work (Zhao et al., 2019). We will not focus on these works, since most of the principles we will teach are the same.
Figure from (Ruan et al., 2022) showing how distribution match and support match adaptations by themselves are not enough.
- But since we do not have $Y^\text{(te)}$ most papers try:
In reality we have the following as training data:
- $\Gamma^\text{(tr)} = \{\Gamma(x): x \in \mathcal{D}^\text{(tr)}\}$
- $\Gamma^\text{(adpt)} = \{\Gamma(x): x \in \mathcal{D}^\text{(adpt)}\}$
- And we need to know how to adapt $\Gamma$ to make these two empirical distributions match
- We also need to eliminate trivial solutions (e.g., $\Gamma$ is constant)
How do we know two set of samples come from the same distribution?¶
Given independent and identically distributed samples $X \sim P(x)$ and $X' \sim Q(x)$, our goal is to define a statistical test that distinguishes between the null hypothesis $H_0 : P = Q$ and the alternative hypothesis $H_A : P \neq Q$.
- This is known as a two-sample test
- In order to learn $\Gamma$ we will actually need a loss: some notion of distance of how far $P$ is from $Q$.
- Just knowing that $P$ and $Q$ are different is not enough to efficiently learn $\Gamma$
- We will call this "distribution distance" the distribution divergence
- This distribution divergence loss will be added as a regularization to the original loss ${L}$
Distribution divergence and distances¶
In what follows we will discribe three different distribution divergence measures:
- Kullback-Leibler divergence and its generalization ($f$-divergence)
- Maximum Mean Discrepancy (MMD)
- Wasserstein Distance (e.g., Earth-mover distance)
KL-Divergence¶
The Kullback-Leibler divergence between two discrete random variables $X \sim P(x)$ and $X' \sim Q(x)$ defined on the same probability space, $\mathbb{X}$, the Kullback–Leibler divergence of $Q$ from $P$ is defined to be $$ D_{\text{KL}}(P \parallel Q) = \sum _{x\in {\mathbb{X}}} P(x)\log \left(P(x)\right) - \sum _{x\in {\mathbb{X}}} P(x) \log \left( Q(x) \right)=\sum _{x\in {\mathbb{X}}}P(x)\log \left({\frac {P(x)}{Q(x)}}\right). $$
For continuous distributions $p$ and $q$, the Kullback–Leibler divergence is defined to be the integral $$ D_{\text{KL}}(P\parallel Q)=\int _{-\infty }^{\infty }P(x)\log \left({\frac {P(x)}{Q(x)}}\right)\,dx, $$ where $0 \cdot \log(0/0)$ is taken to be zero.
import numpy as np
def KL(p, q):
"""Kullback-Leibler divergence D(P || Q) for discrete distributions (represented as vectors)
---------
Parameters:
p, q : probability distribution vectors
"""
p = np.asarray(p, dtype=float)
q = np.asarray(q, dtype=float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
p = [1/3,1/3,1/3]
q = [0.7,0.2,0.1]
print (KL(p, q))
0.3242870277875164
KL-divergence interpretation and properties¶
The quantity $D_{\text{KL}}(P\parallel Q)$ can be interpreted several ways:
- A divergence between the distriubtions $Q$ and $P$
- The information gain going from the proposal (prior) $X' \sim Q(x)$ to the posterior $X \sim P(x)$
- The relative entropy of $X$ with respect to $X'$
- The average surprise in seeing $X'$ when you expected $X$
Properties: The KL-divergence has the following properties:
- The Kullback–Leibler divergence is always non-negative, $$D_{\text{KL}}(P\parallel Q)\geq 0.$$
- It is zero if $X$ and $X'$ have the same distribution, i.e., $P=Q$ and, thus, $$D_{\text{KL}}(P\parallel P) = 0.$$
KL-divergence and KL-inspired distances¶
$D_{\text{KL}}$ is not a distance, since it is not symmetric: $\exists Q,P$ such that $D_{\text{KL}}(P\parallel Q) \neq D_{\text{KL}}(Q\parallel P)$
Kullback and Leibler in their original paper actually defined the divergence as: $$D_{\text{KL}}(P\parallel Q)+D_{\text{KL}}(Q\parallel P),$$ which is a distance since it is symmetric and nonnegative.
The Jensen–Shannon divergence is also a distance, defined as $$ D_{\text{JS}}={\frac {1}{2}}D_{\text{KL}}(P\parallel M)+{\frac {1}{2}}D_{\text{KL}}(Q\parallel M)$$ where $M$ is the average of the two distributions, $$ M={\frac {1}{2}}(P+Q). $$
ƒ-divergences¶
An ƒ-divergence is a function $D_f(P \parallel Q)$ that measures the difference between two probability distributions $P$ and $Q$. For continuous differentiable functions, it is defined as $$ D_{f}(P\parallel Q)=\int _{{\mathbb{X} }}f\left({\frac {P(x)}{Q(x)}}\right)Q(x)\,dx, $$ where $f:[0,+\infty] \to \mathbb{R}\cup\{+\infty\}$ is a continuous convex function.
- The Kullback-Leibler divergence is defined as $f(a) = a \log a$: $$ D_{f}(P\parallel Q)=\int _{-\infty }^{\infty }p(x)\log \left({\frac {P(x)}{Q(x)}}\right)\,dx, $$
- A classical theorem due to Blackwell (1951): If a model (classifier) A has a smaller $f$-divergence than a model (classifier) B (for some fixed $f$), then there exist some set of prior probabilities such that model A has a smaller probability of error than model B
- True for any definition of "error"
Challenges with KL-divergence and $f$-divergences¶
- Only immediately useful if we know how to compute the model distribution $Q(x)$.
- Useful for target shifts, not as useful for covariate shifts, since $\Gamma(x)$ does not output an obvious distribution.
- Interesting connection: Connection between Target Shift and Supervised Learning (via KL-divergence):
- In supervised learning, for a given model $P^{(g,\Gamma)}(Y|X=x) = g(\Gamma(x)),$ we have the conditional target distribution $P^{(g,\Gamma)}(Y|X=x^\text{(tr)}_i) = g(\Gamma(x^\text{(tr)}_i)),$ $i=1,\ldots,n^\text{(tr)}.$
- We also have the empirical conditional target distribution $p^\text{(tr)}(Y|X=x^\text{(tr)}_i)$ (from our training data), $i=1,\ldots,n^\text{(tr)}.$
- Supervised learning's goal is to reduce the target shift between $P^{(g,\Gamma)}(Y|X=x)p^\text{(tr)}(x)$ and $p^\text{(tr)}(Y|X=x)p^\text{(tr)}(x)$.
- The target shift minimization using $D_\text{KL}$ and the empirical distribution of $p^\text{(tr)}(Y|X=x)$ in the training data is also known as Maximum Likelihood Estimation (MLE).
- MLE is simply minimizing the KL-divergence between:
- the model empirical distribution $P^{(g,\Gamma)}(Y|X=x_i)$ and
- the empirical distribution in the training data.
- MLE is simply minimizing the KL-divergence between:
Maximum Mean Discrepancy (MMD) divergence¶
The MMD tests whether distributions $p$ and $q$ are different on the basis of samples drawn from each of them, by finding a smooth function (the witness function) which is large on the points drawn from $p$, and small (as negative as possible) on the points from $q$.
(Gretton et al. 2012) proposed to use as test statistic the difference between the mean function values on the two samples, or maximum mean discrepancy (MMD):
- When this difference is large, the samples are likely from different distributions.
- Smoothness is enforced by restricting the function to a unit ball in a reproducing kernel Hilbert space.
- The MMD is an instance of an integral probability metric.
The Maximum Mean Discrepancy (MMD) can be described as follows:
- Definition. Let $\mathcal{F}$ be a class of smooth functions $f : \mathbb{X} \to \mathbb{R}$ and let $P$, $Q$, $x$, $x'$, $X$, $X'$ be defined as before. Then, the maximum mean discrepancy (MMD) is defined as $$ \operatorname{MMD}(\mathcal{F},P,Q) \equiv \sup_{f \in \mathcal{F}} (E_{X \sim P(x)}[f(X)] - E_{X' \sim Q(x)}[f(X')]). $$ We want a smooth function $f \in \mathcal{F}$ that maximizes the difference in the expectations of the function over $P$ and $Q$.
Empirical MMD: A biased empirical estimate of the MMD can be obtained by replacing the population expectations with empirical expectations computed on the samples $X \sim P(x)$ and $X' \sim Q(x')$.
- Let $m$ and $m'$ be the number of observations from $X$ and $X'$, respectively. Then, the Empirical MMD is defined as: $$ \operatorname{MMD}_b(\mathcal{F},X,X') \equiv \sup_{f \in \mathcal{F}} \left(\frac{1}{m} \sum_{i=1}^m f(x_i) - \frac{1}{m'} \sum_{j=1}^{m'} f(x'_j)\right), $$ noting that $E[\operatorname{MMD}_b(\mathcal{F}, \{x^\text{(te)}_j\}_{j=1}^{n^\text{(te)}}, \{x^\text{(tr)}_i\}_{i=1}^{n^\text{(tr)}} )] \geq \operatorname{MMD}(\mathcal{F}, P, Q)$ by Jensen’s inequality.
Final solution: MMD is added as a regularization term over the empirical training loss $$\mathcal{L}(\mathcal{D}^\text{(tr)}) \propto \frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} L(y^\text{(tr)}_i,g(\Gamma(x^\text{(tr)}_i))) + \operatorname{MMD}_b(\mathcal{F},\{\Gamma(x^\text{(te)}_j)\}_{j=1}^{n^\text{(te)}},\{\Gamma(x^\text{(tr)}_i)\}_{i=1}^{n^\text{(tr)}}).$$
Choosing the smooth class $\mathcal{F}$¶
Choosing $\mathcal{F}$: The quality of the MMD as a statistic depends on the class $\mathcal{F}$ of smooth functions.
- On one hand, F must be “rich enough” so that the population MMD vanishes if and only if $P = Q$.
- On the other hand, for the test to be consistent in power, $\mathcal{F}$ needs to be “restrictive” enough for the empirical estimate of the MMD to converge quickly to its expectation as the sample size increases.
Defining $\mathcal{F}$ as a Reproducing Kernel Hilbert Space (RKHS):
- Then, MMD can accomplish this efficiently via the kernel embedding trick: $$ \operatorname{MMD}(P(x) \Vert Q(x')) = E_{P(x_1), P(x_2)}[k(x_1, x_2)] + E_{Q(x'_1), Q(x'_2)}[k(x'_1, x'_2)] - 2 E_{P(x_1), Q(x'_1)}[k(x_1, x'_1)] $$ where $k(\cdot,\cdot)$ is any universal kernel in a RKHS, such as the Gaussian kernel $$k(z, z') = \exp\left(-\frac{\Vert z - z' \Vert^2}{2\sigma^2}\right).$$ A kernel can be intuitively interpreted as a function that measures the “similarity” of the two samples.
PS: Kuiper Two-sample Loss¶
- We actually recommend using the kernel $k$ but replacing the MMD loss above with the Kuiper two-sample loss approximation of (Mouli et al., 2019).
- Kuiper better accounts for the uncertainty in the number of observations.
Neural networks as the smooth class $\mathcal{F}$ (State-of-the-art MMD)¶
Recently, the ML community has adopted a variation of MMD using neural networks and adversarial training.
Let $f$ is a "smooth" neural network parameterized by ${\bf W}_f$
Let $\mathcal{F} = \{{\bf W}_f | f(;{\bf W}_f)\}$ be a neural network architecture (a hypothesis space).
We define the MMD over $\mathcal{F}$ as: $$\begin{align} {\bf W}^\star_g,{\bf W}^\star_\Gamma,{\bf W}^\star_f = \arg \min_{{\bf W}_g,{\bf W}_\Gamma}\max_{{\bf W}_f} & \frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} L\left(y^\text{(tr)},g(\Gamma(x^\text{(tr)}_i; {\bf W}_\Gamma); {\bf W}_g)\right) \\ &+ \lambda d\left(\frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} f(\Gamma(x^\text{(tr)}_i; {\bf W}_\Gamma); {\bf W}_f) - \frac{1}{n^\text{(adpt)}} \sum_{j=1}^{n^\text{(adpt)}} f(\Gamma(x^\text{(adpt)}_j; {\bf W}_\Gamma); {\bf W}_f)\right), \end{align} $$ where $d(\cdot)$ is some distance metric.
The optimization is performed by:
- An agent responsible for performing gradient descent over ${\bf W}_g,{\bf W}_\Gamma$ as to minimize the loss ($\min$)
- An adversary performing gradient ascent over ${\bf W}_f$ as to maximize the loss ($\max$)
- We will revisit this min-max optimization game when we introduce Generative Adversarial Networks (GANs).
Question:¶
How can we use MMD and the data
- $\Gamma^\text{(tr)} = \{\Gamma(x): x \in \mathcal{D}^\text{(tr)}\}$
- $\Gamma^\text{(adpt)} = \{\Gamma(x): x \in \mathcal{D}^\text{(adpt)}\}$
to learn how to mitigate covariate shifts in an image classification task?
Wasserstein Distance (Optimal transport metric)¶
The $k^{\text{th}} > 0$ Wasserstein distance between two probability distributions $P$ and $Q$ over the joint support $\mathbb{X} = \operatorname{supp}(P) \cup \operatorname{supp}(Q)$ is defined as $$ \operatorname{W}_{k}(P ,Q ):=\left(\inf \operatorname{E}_{(X,X')\sim \theta(x,x') : \theta \in \Theta(P,Q)} {\big [}d(X,X')^{k}{\big ]}\right)^{1/k}, $$ where $d(\cdot,\cdot)$ is a distance metric, and $\theta \in \Theta(P,Q)$ denotes the collection of all probability distributions on $\mathbb{X} \times \mathbb{X}$ with marginals $P$ and $Q$ on the first and second dimensions, respectively.
Example of a transport $\theta \in \Theta(P,Q)$:
Wasserstein Distance of discrete probability spaces¶
Wasserstein distance between two discrete probability distributions $P$ and $Q$ over the joint support $\mathbb{X} = \operatorname{supp}(P) \cup \operatorname{supp}(Q)$ is defined by the doubly stochastic matrix $\theta \in \Theta(P,Q)$, where:
$\Theta(P,Q)$ is the set of all doubly stochastic matrices whose row sums are equal to $P$ and column sums are equal to $Q$.
A doubly stochastic matrix is a nonnegative matrix whose rows and columns all sum to one.
Example with $\mathbb{X}=\{0,\ldots,4\}$:
The Wasserstein distance of discrete distributions can be computed using a method based on the Sinkhorn-Knopp algorithm (Sinkhorn distance) (see (Cuturi, NeurIPS 2013)).
Recall that the Wasserstein distance provides a principled way to measure the distance between two distributions $P$ and $Q$ by finding the optimal transport plan $\theta \in \Theta(P,Q)$: $$ \operatorname{W}_{k}(P, Q) := \left(\inf_{\theta \in \Theta(P,Q)} \operatorname{E}_{(X,X') \sim \theta} \left[ d(X,X')^k \right] \right)^{1/k}. $$
When applied to domain adaptation, we typically align the marginal representation distributions: $$ \operatorname{W}_k\left(p^\text{(tr)}(\Gamma(x)),\; p^\text{(te)}(\Gamma(x))\right). $$
Joint Distribution Optimal Transport (JDOT)¶
(Courty et al., TPAMI 2017) proposed to transport not just features but jointly features and labels. The key insight is to define a ground cost that incorporates both feature distance and label disagreement.
- Ground cost for transporting $(x_i^\text{(tr)}, y_i^\text{(tr)})$ to $x_j^\text{(te)}$: $$ c_{ij} = \alpha \cdot d\!\left(\Gamma(x_i^\text{(tr)}),\; \Gamma(x_j^\text{(te)})\right) + L\!\left(y_i^\text{(tr)},\; g(\Gamma(x_j^\text{(te)}))\right), $$ where:
- $d(\cdot, \cdot)$ is a distance in representation space (e.g., squared Euclidean distance $\|\Gamma(x_i^\text{(tr)}) - \Gamma(x_j^\text{(te)})\|^2$),
- $L(\cdot, \cdot)$ is the task loss (e.g., cross-entropy),
- $\alpha > 0$ balances feature alignment and label consistency.
The first term encourages transporting to nearby points in representation space. The second term penalizes transport plans that pair a source sample with a target sample whose predicted label disagrees with the source label.
JDOT Optimization¶
The JDOT objective is: $$ \min_{{\bf W}_g, {\bf W}_\Gamma} \;\min_{\theta \in \Theta} \; \sum_{i=1}^{n^\text{(tr)}} \sum_{j=1}^{n^\text{(adpt)}} \theta_{ij} \left[\alpha \cdot d\!\left(\Gamma(x_i^\text{(tr)}),\; \Gamma(x_j^\text{(te)})\right) + L\!\left(y_i^\text{(tr)},\; g(\Gamma(x_j^\text{(te)}))\right)\right], $$ where:
- $\theta \in \Theta$ is the set of valid transport plans, i.e., $\theta_{ij} \geq 0$ with row sums equal to $\frac{1}{n^\text{(tr)}}$ and column sums equal to $\frac{1}{n^\text{(adpt)}}$ (assuming uniform empirical distributions).
- The optimization alternates between:
- Fixing ${\bf W}_g, {\bf W}_\Gamma$ and solving for $\theta$ (an optimal transport problem, solvable via the Sinkhorn-Knopp algorithm from earlier in these notes).
- Fixing $\theta$ and updating ${\bf W}_g, {\bf W}_\Gamma$ via gradient descent on the weighted loss.
DeepJDOT¶
(Damodaran et al., ECCV 2018) extended JDOT to deep neural networks by jointly learning the representation $\Gamma$ and the transport plan:
$$ \min_{{\bf W}_g, {\bf W}_\Gamma} \; \underbrace{\frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} L(y_i^\text{(tr)}, g(\Gamma(x_i^\text{(tr)})))}_{\text{supervised loss on source}} \;+\; \lambda \underbrace{\min_{\theta \in \Theta} \sum_{i,j} \theta_{ij} \, c_{ij}}_{\text{JDOT transport cost}}, $$
where $c_{ij} = \alpha \cdot \|\Gamma(x_i^\text{(tr)}) - \Gamma(x_j^\text{(te)})\|^2 + L(y_i^\text{(tr)}, g(\Gamma(x_j^\text{(te)})))$ as before.
- The supervised loss ensures the model learns discriminative representations from labeled source data.
- The JDOT transport cost encourages the representation $\Gamma$ to bring source and target samples with compatible predicted labels closer together.
- Note that the transport plan $\theta$ is recomputed (via Sinkhorn) at each training step or every few steps.
Why JDOT Does not Necessarily Addresses the Conditional Alignment Problem¶
- Standard marginal OT aligns $p^\text{(tr)}(\Gamma(x))$ to $p^\text{(te)}(\Gamma(x))$ — this can merge class-conditional clusters, as discussed earlier.
- JDOT's label-aware cost $c_{ij}$ penalizes transporting source sample $(x_i^\text{(tr)}, y_i^\text{(tr)})$ to a target sample $x_j^\text{(te)}$ if $g(\Gamma(x_j^\text{(te)}))$ disagrees with $y_i^\text{(tr)}$.
- But still, this is not a guarantee the target labels are correctly aligned.
- The idea is that the transport plan $\theta$ is more likely to discover a class-conditional coupling: source samples of class $c$ are preferentially matched to target samples that the model predicts as class $c$.
- This helps but is not a perfect solution: it relies on the model's current predictions on target data being reasonable — but it is a principled improvement over purely marginal alignment.
Computational Considerations¶
The Sinkhorn-Knopp step has complexity $O(n^\text{(tr)} \cdot n^\text{(adpt)})$ per iteration, which can be expensive for large datasets.
- Mini-batch OT (Fatras et al., AISTATS 2020): Approximate the transport plan using mini-batches. Compute $\theta$ only within each mini-batch pair, reducing cost to $O(B^2)$ where $B$ is the batch size.
- Note that mini-batch OT introduces bias, since the optimal transport within a mini-batch is generally not equal to the restriction of the global optimal transport to that mini-batch.
Sinkhorn regularization parameter $\epsilon$ (entropic regularization): $$ \min_{\theta \in \Theta} \sum_{i,j} \theta_{ij} \, c_{ij} + \epsilon \sum_{i,j} \theta_{ij} \log \theta_{ij}. $$
- Larger $\epsilon$ makes the optimization smoother and faster to converge but introduces a bias toward the uniform coupling.
- Smaller $\epsilon$ recovers the true OT solution but may cause numerical instability.
Method 3: Self-Training / Pseudo-Labeling¶
Self-training is an iterative domain adaptation approach that does not align representation distributions. Instead, it uses the model's own predictions on target data as supervision.
Key Idea¶
- Train $g \circ \Gamma$ on $\mathcal{D}^\text{(tr)}$ to obtain an initial model.
- For each $x_j^\text{(te)} \in \mathcal{D}^\text{(adpt)}$, compute the pseudo-label: $\hat{y}_j = \arg\max_{y \in \mathbb{Y}} \; g(\Gamma(x_j^\text{(te)}))_y.$
- Select a confident subset $\mathcal{D}^\text{(pseudo)}$ using a confidence threshold $\tau$: $$ \mathcal{D}^\text{(pseudo)} = \left\{(x_j^\text{(te)}, \hat{y}_j) \;\middle|\; \max_{y \in \mathbb{Y}} \; g(\Gamma(x_j^\text{(te)}))_y \geq \tau \right\}. $$
- Retrain on the combined loss: $$ \mathcal{L} = \frac{1}{n^\text{(tr)}} \sum_{i=1}^{n^\text{(tr)}} L(y_i^\text{(tr)}, g(\Gamma(x_i^\text{(tr)}))) + \frac{\lambda}{|\mathcal{D}^\text{(pseudo)}|} \sum_{(x_j, \hat{y}_j) \in \mathcal{D}^\text{(pseudo)}} L(\hat{y}_j, g(\Gamma(x_j))), $$ where $\lambda > 0$ controls the influence of pseudo-labeled data. Note that pseudo-labels $\hat{y}_j$ are treated as fixed targets (not differentiated through).
- Repeat from step 2 until convergence.
The main risk is confirmation bias: incorrect pseudo-labels are reinforced across iterations.
Class-Balanced Self-Training (CBST)¶
(Zou et al., ECCV 2018) observed that naive self-training is biased toward "easy" classes (confident classes accumulate more pseudo-labels), further skewing the distribution. The fix is to replace the global threshold $\tau$ with per-class thresholds $\tau_c$, chosen so that a fixed proportion of target samples is selected per class. This connects back to target shift adaptation from earlier in these notes.
Connection to the Conditional Alignment Problem¶
- Distribution-matching methods (MMD, Wasserstein) enforce the weaker marginal condition $\Gamma(X^\text{(tr)}) \stackrel{d}{=} \Gamma(X^\text{(te)})$.
- Self-training implicitly attempts class-conditional alignment: each pseudo-labeled sample $(x_j, \hat{y}_j)$ contributes to the loss for class $\hat{y}_j$ specifically, encouraging the model to learn class-conditional structure in the target domain, provided the pseudo-labels are sufficiently accurate.
Summary¶
Today we have seen how to perform test data adaptation.
- If $p^\text{(te)}(x)$ is known, we can easily use importance reweighting
- If $p^\text{(te)}(x)$ is unknown, but we can obtain the model distribution $Q(x)$. We can adapt the model via KL-divergence or Wasserstein distance
- If $p^\text{(te)}(x)$ is unknown and we cannot easily obtain the model distribution $Q(x)$, we must adapt the representation $\Gamma(x)$ via MMD or (the empirical) Wasserstein distance