A Primer on Domain Adaptation Theory and Applications

Pirmin Lemberger, Ivan Panico, A Primer on Domain Adaptation
Theory and Applications, 2019.

机器学习分为训练和测试俩步骤, 且往往假设训练样本的分布和测试样本的分布是一致的, 但是这种情况在实际中并不一定成立. 作者就prior shift, covratie shift, concept shift, subspace mapping 四种情形给出相应的’解决方案".

主要内容

符号说明

x X R p \mathbf{x} \in \mathcal{X} \subset \mathbb{R}^p : 数据
y Y = { ω 1 , , ω k } y \in \mathcal{Y}=\{\omega_1,\ldots, \omega_k\} : 类别标签
S = { ( x 1 , y 1 ) , ( x m , y m ) } S=\{(\mathbf{x}_1,y_1), \ldots(\mathbf{x_m}, y_m)\} : 训练样本
h H : X Y h \in \mathcal{H}:\mathcal{X} \rightarrow \mathcal{Y} : 拟合函数/分类器
y ^ = h ( x ) \hat{y}=h(\mathbf{x}) :预测
: Y × Y R \ell: \mathcal{Y} \times \mathcal{Y} \rightarrow \mathbb{R} : 损失函数
R [ h ] : = E ( x , y ) p [ ( y , h ( x ) ] R[h]:= \mathbb{E}_{(\mathbf{x}, y) \sim p}[\ell(y, h(\mathbf{x})] : risk
R ^ [ h ] : = 1 m i = 1 m [ ( y i , h ( x i ) ] \hat{R}[h]:= \frac{1}{m} \sum_{i=1}^m [\ell(y_i, h(\mathbf{x}_i)] : 经验风险函数
p S p_S : 训练数据对应的分布
p T p_T : 目标数据对应的分布
p ^ \hat{p} :近似的分布

Prior shift

p S ( x y ) = p T ( x y ) p_S(\mathbf{x}|y)=p_T(\mathbf{x}|y) p S ( y ) p T ( y ) p_S(y) \not = p_T(y) . (如, 训练的时候,对每一类, 我们往往选择相同数目的样本以求保证类别的均衡).

在这里插入图片描述

假设根据训练样本 S S 和算法 A A ,我们得到了一个近似后验分布 p ^ S ( y x ) \hat{p}_S(y|\mathbf{x}) , 且近似的先验分布 p ^ S ( y = ω k ) = m k / S \hat{p}_S(y=\omega_k)=m_k/|S| , 并同样假设 p ^ S ( x y ) = p ^ T ( x y ) \hat{p}_S(\mathbf{x}|y)=\hat{p}_T(\mathbf{x}|y) , 有
p ^ T ( ω k x ) = w ^ ( ω k ) p ^ S ( ω k x ) k = 1 K w ^ ( ω k ) p ^ S ( ω k x ) , w ^ ( ω k ) : = p ^ T ( ω k ) p ^ S ( ω k ) . (9) \tag{9} \hat{p}_T(\omega_k|\mathbf{x})= \frac{\hat{w}(\omega_k)\hat{p}_S(\omega_k|\mathbf{x})}{\sum_{k'=1}^K\hat{w}(\omega_{k'})\hat{p}_S(\omega_{k'}|\mathbf{x})}, \hat{w}(\omega_k):=\frac{\hat{p}_T(\omega_k)}{\hat{p}_S(\omega_k)}.

倘若我们知道 p ^ T ( ω k ) , k = 1 , , K \hat{p}_T(\omega_k), k=1,\ldots, K , 那么我们就直接可以利用(9)式来针对目标数据集了, 而这里的真正的难点在于, 如果不知道, 应该怎么办.

假设, 我们的目标数据集的样本数据为 x 1 , , x m \mathbf{x}_1', \ldots, \mathbf{x}_m' , 则我们的目标是求出 p ^ T ( ω k x ) \hat{p}_T(\omega_k|\mathbf{x}') , 有
p ^ T ( ω k ) = i = 1 m p ^ T ( ω k , x i ) = 1 m i = 1 m p ^ T ( ω k x i ) , (10) \tag{10} \hat{p}_T(\omega_k)=\sum_{i=1}^m \hat{p}_T(\omega_k,\mathbf{x}_i')=\frac{1}{m} \sum_{i=1}^m \hat{p}_T(\omega_k|\mathbf{x}_i'),
其中在最后一个等号部分, 我们假设了 p ( x i ) = 1 m p(\mathbf{x}_i')=\frac{1}{m} , 这个假设并非空穴来风, 我们可以从EM算法角度去理解.

于是, 很自然地, 我们可以利用交替迭代求解
p ^ T ( s ) ( ω k x ) = w ^ ( ω k ) p ^ S ( ω k x ) k = 1 K w ^ ( ω k ) p ^ S ( ω k x ) , w ^ ( ω k ) : = p ^ T ( s ) ( ω k ) p ^ S ( ω k ) . p ^ T ( s + 1 ) ( ω k ) = 1 m i = 1 m p ^ T ( s ) ( ω k x i ) . (11) \tag{11} \hat{p}_T^{(s)}(\omega_k|\mathbf{x}')= \frac{\hat{w}(\omega_k)\hat{p}_S(\omega_k|\mathbf{x}')}{\sum_{k'=1}^K\hat{w}(\omega_{k'})\hat{p}_S(\omega_{k'}|\mathbf{x}')}, \hat{w}(\omega_k):=\frac{\hat{p}_T^{(s)}(\omega_k)}{\hat{p}_S(\omega_k)}. \\ \hat{p}_T^{(s+1)}(\omega_k)=\frac{1}{m} \sum_{i=1}^m \hat{p}_T^{(s)}(\omega_k|\mathbf{x}_i').

注: 在实际中, 由于各种因素, 这么做反而画蛇添足, 起到反效果, 我们可以通过假设检验来判断是否接受.
在这里插入图片描述

在这里插入图片描述

其趋向于 χ ( K 1 ) 2 \chi^2_{(K-1)} 对于足够多地样本.

Covariate shift

p S ( y x ) = p T ( y x ) p_S(y|\mathbf{x})=p_T(y|\mathbf{x}) , 但是 p S ( x ) p T ( x ) p_S(\mathbf{x})\not = p_T(\mathbf{x}) .

A covariate shift typically occurs when the cost or the difficulty of picking an observation with given features x strongly impacts the probability of selecting an observation (x, y) thus making it practically impossible to replicate the target feature distribution p T ( x ) p_T(\mathbf{x}) in the training set.
在这里插入图片描述

我们所希望最小化,
R T [ h ] : = E p T [ ( h ( x ) ) , y ) ] = E p S [ w ( x ) ( h ( x ) ) , y ) ] . (14,15) \tag{14,15} R_T[h]:= \mathbb{E}_{p_T}[\ell(h(\mathbf{x})),y)] =\mathbb{E}_{p_S}[w(\mathbf{x})\ell(h(\mathbf{x})),y)].
在实际中, 若我们有 w ( x ) = p T ( x ) / p S ( x ) w(\mathbf{x})=p_T(\mathbf{x})/p_S(\mathbf{x}) 或者其一个估计 w ^ ( x ) \hat{w}(\mathbf{x}) , 我们最小化经验风险
R ^ S , w [ h ] : = 1 m i = 1 m w ( x i ) ( h ( x i ) , y i ) . (16) \tag{16} \hat{R}_{S, w} [h]:= \frac{1}{m} \sum_{i=1}^m w(\mathbf{x}_i) \ell(h(\mathbf{x}_i),y_i).

注: 以下情况不适合用(16):

  1. p S ( x i ) = 0 p_S(\mathbf{x}_i)=0 但是 p T ( x ) i 0 p_T(\mathbf{x})_i \not=0 ;
  2. p S , p T p_S, p_T 二者差距很大, 使得 w w 波动很大.

p S p_S 最好是选取范围和 p T p_T 近似, 这些是根据下面的理论结果的到的:
在这里插入图片描述
(17)有 1 δ 1-\delta 的可信度.

w ^ \hat{w}

显然, 解决(16)的关键在于 w ^ : = p ^ T ( x ) / p ^ S ( x ) \hat{w}:=\hat{p}_T(\mathbf{x})/\hat{p}_S(\mathbf{x}) , 有很多的概率密度估计方法(如核密度估计(KDE)), 但是在实际应用中, 这种估计可能会导致不可控的差的结果.

一个策略是直接估计 w ^ \hat{w} , 而非分别估计 p ^ T , p ^ S \hat{p}_T, \hat{p}_S :

  • 期望均方误差 E p S [ ( w ^ p T / p S ) 2 ] \mathbb{E}_{p_S}[(\hat{w}-p_T/p_S)^2] (怎么玩?);
  • KL散度 K L ( p T w ^ p S ) \mathbf{KL}(p_T \| \hat{w}p_S) (怎么玩?);
  • 最大平均差异(maximum mean discrepancy, MMD).
KMM

选择kernel K ( x , y ) K(\mathbf{x}, \mathbf{y}) , 相当于将 x \mathbf{x} 映入一个希尔伯特空间(RKHS), x Φ x \mathbf{x} \rightarrow \Phi_{\mathbf{x}} , 其内积为 Φ x , Φ y = K ( x , y ) \langle \Phi_{\mathbf{x}}, \Phi_{\mathbf{y}} \rangle=K(\mathbf{x}, \mathbf{y}) . 则MMD定义为:
( M M D [ α , β ] ) 2 : = E x α [ Φ x ] E x β [ Φ x ] 2 = E x α [ Φ x ] 2 2 E x α [ Φ x ] , E x β [ Φ x ] + E x β [ Φ x ] 2 . (\mathrm{MMD}[\alpha, \beta])^2:=\|\mathbb{E}_{\mathbf{x} \sim \alpha} [\Phi_{\mathbf{x}}]-\mathbb{E}_{\mathbf{x} \sim \beta} [\Phi_{\mathbf{x}}]\|^2= \|\mathbb{E}_{\mathbf{x} \sim \alpha} [\Phi_{\mathbf{x}}]\|^2-2\langle \mathbb{E}_{\mathbf{x} \sim \alpha} [\Phi_{\mathbf{x}}],\mathbb{E}_{\mathbf{x} \sim \beta} [\Phi_{\mathbf{x}}] \rangle+ \|\mathbb{E}_{\mathbf{x} \sim \beta} [\Phi_{\mathbf{x}}]\|^2.

则令 α = w ^ p ^ S , β = p ^ T \alpha=\hat{w}\hat{p}_S, \beta=\hat{p}_T
在这里插入图片描述
( M M D [ p ^ T , w ^ p ^ S ] ) 2 = 1 m S 2 ( 1 2 w ^ T K w ^ k T w ^ ) + c o n s t , (21) \tag{21} (\mathrm{MMD}[\hat{p}_T, \hat{w} \hat{p}_S])^2 = \frac{1}{m_S^2} (\frac{1}{2} \hat{w}^TK \hat{w} - k^T\hat{w}) +\mathrm{const},
其中 w ^ : = ( w ^ ( x 1 ) , , w ^ ( x m S ) ) T \hat{w}:=(\hat{w}(\mathbf{x}_1),\ldots, \hat{w}(\mathbf{x}_{m_S}))^T , K i j : = 2 K ( x i , x k ) K_{ij}:=2K(\mathbf{x}_i,\mathbf{x}_k) , k i : = 2 m S m T j = 1 m T K ( x i , x j ) k_i:=\frac{2m_S}{m_T} \sum_{j=1}^{m_T} K(\mathbf{x}_i,\mathbf{x}_j) .

在实际中, 求解下面的优化问题
min w 1 2 w ^ T K w ^ k T w ^ s . t . w ^ ( x i ) [ 0 , B ] , 1 m S i = 1 m S w ^ ( x i ) 1 ϵ . \begin{array}{rc} \min_w & \frac{1}{2} \hat{w}^T K\hat{w} - k^T\hat{w} \\ \mathrm{s.t.} & \hat{w}(\mathbf{x}_i) \in [0,B], \\ & |\frac{1}{m_S} \sum_{i=1}^{m_S} \hat{w}(\mathbf{x}_i) -1| \le \epsilon. \end{array}
第一个条件为了保证 p ^ S , p ^ T \hat{p}_S,\hat{p}_T 之间差距不大, 第二个条件是为了保证概率的积分为1的性质.

Concept shift

p S ( y x ) p T ( y x ) p_S(y|\mathbf{x})\not= p_T(y|\mathbf{x}) p S ( x ) = p T ( x ) p_S(\mathbf{x})=p_T(\mathbf{x}) . 其往往是在时序系统下, 即分布 p p 与时间有关.

  1. 周期性地利用新数据重新训练模型;
  2. 保留部分旧数据, 结合新数据训练;
  3. 加入权重;
  4. 引入有效的迭代机制;
  5. 检测偏移, 并作出反应.

在这里插入图片描述

Subspace mapping

训练数据为 x \mathbf{x} , 而目标数据为 x = T ( x ) \mathbf{x}'=T(\mathbf{x}) , 且 p T ( T ( x ) , y ) = p S ( x , y ) p_T(T(\mathbf{x}), y) = p_S(\mathbf{x},y) ,且 T T 是未知的.

我们现在的目标是找到一个有关

Wasserstein distance

以离散情形为例, 介绍,
α : = i = 1 m α i δ z i , \alpha := \sum_{i=1}^m \alpha_i \delta_{\mathbf{z}_i},
其中 δ z \delta_{\mathbf{z}} 表示狄拉克函数.
T α : = i = 1 m α i δ T ( z i ) , T \alpha := \sum_{i=1}^m \alpha_i \delta_{T(\mathbf{z}_i)},
则, 自然地, 我们希望
arg min T , T α = β E z α [ c ( z , T ( z ) ) ] , \arg \min_{T, T\alpha = \beta} \mathbb{E}_{\mathbf{z} \sim \alpha} [c(\mathbf{z}, T(\mathbf{z}))],
其中 c ( , ) c(\cdot, \cdot) 是我们给定的一个损失函数, 这类问题被称为 Monge 问题.
在这里插入图片描述

但是呢, 这种方式找 T T 非常困难, 于是有了一种概率替代方案,
γ : = i , j γ i j δ z i , z j (30) \tag{30} \gamma := \sum_{i,j} \gamma_{ij} \delta_{\mathbf{z}_i,\mathbf{z}_j'}
为以离散概率分布, 则
E ( z , z ) γ [ c ( z , z ) ] : = i , j γ i , j c ( z i , z j ) , (33) \tag{33} \mathbb{E}_{(\mathbf{z},\mathbf{z}') \sim \gamma}[c(\mathbf{z},\mathbf{z}')]:=\sum_{i,j} \gamma_{i,j}c(\mathbf{z}_i,\mathbf{z}_j),
L c ( α , β ) : = min γ U ( α , β ) E ( z , z ) γ [ c ( z , z ) ] (34) \tag{34} \mathcal{L}_c (\alpha, \beta) := \min_{\gamma \in U(\alpha, \beta)} \mathbb{E}_{(\mathbf{z}, \mathbf{z}') \sim \gamma}[c(\mathbf{z}, \mathbf{z}')]
衡量了从分布 α \alpha 变换到分布 β \beta 的难易程度, 其中
U ( α , β ) : = { γ : j = 1 s γ i j = α i , i = 1 r γ i j = β j } , U(\alpha, \beta):=\{ \gamma: \sum_{j=1}^s \gamma_{ij} =\alpha_i, \sum_{i=1}^r \gamma_{ij} = \beta_j\},
注意这实际上是一个事实, 因为 α , β \alpha, \beta 是其联合分布 γ \gamma 的边缘分布.

而Wasserstein distance实际上就是
W p ( α , β ) : = [ L d p ( α , β ) ] 1 / p , c ( z , z ) = [ d ( z , z ) ] p , p 1. (35) \tag{35} W_p(\alpha,\beta) := [\mathcal{L}_{d^p} (\alpha, \beta)]^{1/p}, c(\mathbf{z},\mathbf{z}')=[d(\mathbf{z},\mathbf{z}')]^p, p\ge1.
d d 为一距离.

应用于 subspace mapping

策略一:
α = p ^ S ( x ) , β = p ^ T ( x ) \alpha=\hat{p}_S(\mathbf{x}), \beta=\hat{p}_T(\mathbf{x}')

注:为了防止 ( x i , y i ) , ( x j , y j ) , y i y j (\mathbf{x}_i,y_i),(\mathbf{x}_j,y_j), y_i \not =y_j

相关文章
相关标签/搜索