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

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

主要内容

符号说明

xXRp\mathbf{x} \in \mathcal{X} \subset \mathbb{R}^p: 数据
yY={ω1,,ωk}y \in \mathcal{Y}=\{\omega_1,\ldots, \omega_k\}: 类别标签
S={(x1,y1),(xm,ym)}S=\{(\mathbf{x}_1,y_1), \ldots(\mathbf{x_m}, y_m)\}: 训练样本
hH:XYh \in \mathcal{H}:\mathcal{X} \rightarrow \mathcal{Y}: 拟合函数/分类器
y^=h(x)\hat{y}=h(\mathbf{x}):预测
:Y×YR\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]:=1mi=1m[(yi,h(xi)]\hat{R}[h]:= \frac{1}{m} \sum_{i=1}^m [\ell(y_i, h(\mathbf{x}_i)]: 经验风险函数
pSp_S: 训练数据对应的分布
pTp_T: 目标数据对应的分布
p^\hat{p}:近似的分布

Prior shift

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

A Primer on Domain Adaptation Theory and Applications

假设根据训练样本SS和算法AA,我们得到了一个近似后验分布p^S(yx)\hat{p}_S(y|\mathbf{x}), 且近似的先验分布p^S(y=ωk)=mk/S\hat{p}_S(y=\omega_k)=m_k/|S|, 并同样假设p^S(xy)=p^T(xy)\hat{p}_S(\mathbf{x}|y)=\hat{p}_T(\mathbf{x}|y), 有
p^T(ωkx)=w^(ωk)p^S(ωkx)k=1Kw^(ωk)p^S(ωkx),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)式来针对目标数据集了, 而这里的真正的难点在于, 如果不知道, 应该怎么办.

假设, 我们的目标数据集的样本数据为x1,,xm\mathbf{x}_1', \ldots, \mathbf{x}_m', 则我们的目标是求出p^T(ωkx)\hat{p}_T(\omega_k|\mathbf{x}'), 有
p^T(ωk)=i=1mp^T(ωk,xi)=1mi=1mp^T(ωkxi),(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(xi)=1mp(\mathbf{x}_i')=\frac{1}{m}, 这个假设并非空穴来风, 我们可以从EM算法角度去理解.

于是, 很自然地, 我们可以利用交替迭代求解
p^T(s)(ωkx)=w^(ωk)p^S(ωkx)k=1Kw^(ωk)p^S(ωkx),w^(ωk):=p^T(s)(ωk)p^S(ωk).p^T(s+1)(ωk)=1mi=1mp^T(s)(ωkxi).(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').

注: 在实际中, 由于各种因素, 这么做反而画蛇添足, 起到反效果, 我们可以通过假设检验来判断是否接受.
A Primer on Domain Adaptation Theory and Applications

A Primer on Domain Adaptation Theory and Applications

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

Covariate shift

pS(yx)=pT(yx)p_S(y|\mathbf{x})=p_T(y|\mathbf{x}), 但是pS(x)pT(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 pT(x)p_T(\mathbf{x}) in the training set.
A Primer on Domain Adaptation Theory and Applications

我们所希望最小化,
RT[h]:=EpT[(h(x)),y)]=EpS[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)=pT(x)/pS(x)w(\mathbf{x})=p_T(\mathbf{x})/p_S(\mathbf{x})或者其一个估计w^(x)\hat{w}(\mathbf{x}), 我们最小化经验风险
R^S,w[h]:=1mi=1mw(xi)(h(xi),yi).(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. pS(xi)=0p_S(\mathbf{x}_i)=0 但是pT(x)i0p_T(\mathbf{x})_i \not=0;
  2. pS,pTp_S, p_T二者差距很大, 使得ww波动很大.

pSp_S最好是选取范围和pTp_T近似, 这些是根据下面的理论结果的到的:
A Primer on Domain Adaptation Theory and Applications
(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:

  • 期望均方误差EpS[(w^pT/pS)2]\mathbb{E}_{p_S}[(\hat{w}-p_T/p_S)^2](怎么玩?);
  • KL散度KL(pTw^pS)\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定义为:
(MMD[α,β])2:=Exα[Φx]Exβ[Φx]2=Exα[Φx]22Exα[Φx],Exβ[Φx]+Exβ[Φ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
A Primer on Domain Adaptation Theory and Applications
(MMD[p^T,w^p^S])2=1mS2(12w^TKw^kTw^)+const,(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^(x1),,w^(xmS))T\hat{w}:=(\hat{w}(\mathbf{x}_1),\ldots, \hat{w}(\mathbf{x}_{m_S}))^T, Kij:=2K(xi,xk)K_{ij}:=2K(\mathbf{x}_i,\mathbf{x}_k), ki:=2mSmTj=1mTK(xi,xj)k_i:=\frac{2m_S}{m_T} \sum_{j=1}^{m_T} K(\mathbf{x}_i,\mathbf{x}_j).

在实际中, 求解下面的优化问题
minw12w^TKw^kTw^s.t.w^(xi)[0,B],1mSi=1mSw^(xi)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

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

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

A Primer on Domain Adaptation Theory and Applications

Subspace mapping

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

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

Wasserstein distance

以离散情形为例, 介绍,
α:=i=1mαiδzi, \alpha := \sum_{i=1}^m \alpha_i \delta_{\mathbf{z}_i},
其中δz\delta_{\mathbf{z}}表示狄拉克函数.
Tα:=i=1mαiδT(zi), T \alpha := \sum_{i=1}^m \alpha_i \delta_{T(\mathbf{z}_i)},
则, 自然地, 我们希望
argminT,Tα=βEzα[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 问题.
A Primer on Domain Adaptation Theory and Applications

但是呢, 这种方式找TT非常困难, 于是有了一种概率替代方案,
γ:=i,jγijδzi,zj(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,jc(zi,zj),(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),
Lc(α,β):=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=1sγij=αi,i=1rγij=β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实际上就是
Wp(α,β):=[Ldp(α,β)]1/p,c(z,z)=[d(z,z)]p,p1.(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.
dd为一距离.

应用于 subspace mapping

策略一:
α=p^S(x),β=p^T(x)\alpha=\hat{p}_S(\mathbf{x}), \beta=\hat{p}_T(\mathbf{x}'), 通过(34)可以找到一个γ\gamma, 再利用γ\gamma把训练数据SS映射到p^T\hat{p}_T分布上, 再利用新的训练数据重新训练模型. (? 如何利用γ\gamma变换呢?)

注:为了防止(xi,yi),(xj,yj),yiyj(\mathbf{x}_i,y_i),(\mathbf{x}_j,y_j), y_i \not =y_j变换到同一个新数据, 需要添加一个惩罚项.

策略二:
α=p^S(x,y),β=p^T(x,y)\alpha=\hat{p}_S(\mathbf{x},y), \beta=\hat{p}_T (\mathbf{x}',y'), 但是yy'我们是不知道的, 所以用h(x)h(\mathbf{x}')代替, 且
p^Th(x,y):=p^T(x)δy=h(x), \hat{p}_T^h(\mathbf{x}',y'):= \hat{p}_T(\mathbf{x}') \delta_{y'=h(\mathbf{x}')},
于是
hOT:=argminhHW1(p^S,p^Th),(37) \tag{37} h_{OT} := \arg \min_{h \in \mathcal{H}} W_1(\hat{p}_S, \hat{p}_T^h),


hOT=argminhHminγU(p^S,p^Th)i,jγijd((xi,yi),(xj,h(xj))).(38) \tag{38} h_{OT} = \arg \min_{h \in \mathcal{H}} \min_{\gamma \in U(\hat{p}_S, \hat{p}_T^h)} \sum_{i,j} \gamma_{ij} d((\mathbf{x}_i,y_i),(\mathbf{x}_j', h(\mathbf{x}_j'))).
其中
d((x,y),(x,y)):=λρ(x,x)+(y,y). d((\mathbf{x},y),(\mathbf{x}', y')) := \lambda \rho(\mathbf{x},\mathbf{x}') + \ell(y,y').
在实际使用中, 视实际情况而定, 加入惩罚项
hOT=argminhHminγU(p^S,p^Th)(i,jγij[λρ(xi,xj)+(yi,h(xj))]+ϵreg[h]).(39) \tag{39} h_{OT} = \arg \min_{h \in \mathcal{H}} \min_{\gamma \in U(\hat{p}_S, \hat{p}_T^h)} \big(\sum_{i,j} \gamma_{ij} [ \lambda \rho(\mathbf{x}_i,\mathbf{x}_j') + \ell(y_i,h(\mathbf{x}_j'))] + \epsilon \mathrm{reg}[h] \big).

Prior shift 的EM解释

考虑联合概率pθ(x1,,xm;z1,,zm)p_{\theta}(\mathbf{x}_1, \ldots, \mathbf{x}_m; \mathbf{z}_1,\ldots, \mathbf{z}_m), 其中zi,i=1,,m\mathbf{z}_i,i=1,\ldots, m为隐变量, xi,i=1,,m\mathbf{x}_i, i=1,\ldots,m为观测变量,EM算法步骤如下:

  1. E步: Ez[logpθ(x1,,xm;z1,,zm)]\mathbb{E}_{\mathbf{z}}[\log p_{\theta}(\mathbf{x}_1, \ldots, \mathbf{x}_m; \mathbf{z}_1,\ldots, \mathbf{z}_m)](下面是离散情况)

A Primer on Domain Adaptation Theory and Applications
2. M步:

A Primer on Domain Adaptation Theory and Applications

Prior shift中, θ:=[pT(ω1),,pT(ωK)]T\theta:= [p_T(\omega_1), \ldots, p_T(\omega_K)]^{\mathrm{T}}, 隐变量zi:=(zi1,,ziK)\mathbf{z}_i:=(z_{i1},\ldots, z_{iK})yi{ω1,,ωK}y_i \in \{\omega_1,\ldots, \omega_K\}的one-hot-encodings. 则

A Primer on Domain Adaptation Theory and Applications
其对数似然为
A Primer on Domain Adaptation Theory and Applications
条件概率为
A Primer on Domain Adaptation Theory and Applications
且易知
A Primer on Domain Adaptation Theory and Applications
A Primer on Domain Adaptation Theory and Applications
所以:
A Primer on Domain Adaptation Theory and Applications
因为θk\theta_k满足kθk=1\sum_k \theta_k=1并不相互独立, 所以利用拉格朗日乘子法
A Primer on Domain Adaptation Theory and Applications
取得极值的必要条件为
A Primer on Domain Adaptation Theory and Applications

A Primer on Domain Adaptation Theory and Applications

相关文章: