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

相關文章
相關標籤/搜索