1. 程式人生 > >CS229 6.9 Neurons Networks softmax regression

CS229 6.9 Neurons Networks softmax regression

SoftMax迴歸模型,是logistic迴歸在多分類問題的推廣,即現在logistic迴歸資料中的標籤y不止有0-1兩個值,而是可以取k個值,softmax迴歸對諸如MNIST手寫識別庫等分類很有用,該問題有0-9 這10個數字,softmax是一種supervised learning方法。

在logistic中,訓練集由 \textstyle m 個已標記的樣本構成:\{ (x^{(1)}, y^{(1)}), \ldots, (x^{(m)}, y^{(m)}) \} ,其中輸入特徵x^{(i)} \in \Re^{n+1}(特徵向量 \textstyle x 的維度為 \textstyle n+1,其中 \textstyle x_0 = 1 對應截距項 ), logistic 迴歸是針對二分類問題的,因此類標記 y^{(i)} \in \{0,1\}。假設函式(hypothesis function) 如下:

\begin{align}
h_\theta(x) = \frac{1}{1+\exp(-\theta^Tx)},
\end{align}

損失函式為負log損失函式:


\begin{align}
J(\theta) = -\frac{1}{m} \left[ \sum_{i=1}^m y^{(i)} \log h_\theta(x^{(i)}) + (1-y^{(i)}) \log (1-h_\theta(x^{(i)})) \right]
\end{align}

找到使得損失函式最小時的模型引數 \textstyle \theta ,帶入假設函式即可求解模型。

在softmax迴歸中,對於訓練集\{ (x^{(1)}, y^{(1)}), \ldots, (x^{(m)}, y^{(m)}) \} 中的類標 \textstyle y 可以取 \textstyle k 個不同的值(而不是 2 個),即有 y^{(i)} \in \{1, 2, \ldots, k\}(注意不是由0開始), 在MNIST中有K=10個類別。

在softmax迴歸中,對於輸入x,要計算x分別屬於每個類別j的概率\textstyle p(y=j | x),即求得x分別屬於每一類的概率,因此假設函式要設定為輸出一個k維向量,每個維度代表x被分為每個類別的概率,假設函式 \textstyle h_{\theta}(x) 形式如下:


\begin{align}
h_\theta(x^{(i)}) =
\begin{bmatrix}
p(y^{(i)} = 1 | x^{(i)}; \theta) \\
p(y^{(i)} = 2 | x^{(i)}; \theta) \\
\vdots \\
p(y^{(i)} = k | x^{(i)}; \theta)
\end{bmatrix}
=
\frac{1}{ \sum_{j=1}^{k}{e^{ \theta_j^T x^{(i)} }} }
\begin{bmatrix}
e^{ \theta_1^T x^{(i)} } \\
e^{ \theta_2^T x^{(i)} } \\
\vdots \\
e^{ \theta_k^T x^{(i)} } \\
\end{bmatrix}
\end{align}

請注意 \frac{1}{ \sum_{j=1}^{k}{e^{ \theta_j^T x^{(i)} }} } 這一項對概率分佈進行歸一化,使得所有概率之和為 1 。當類別數 \textstyle k = 2

 時,softmax 迴歸退化為 logistic 迴歸。這表明 softmax 迴歸是 logistic 迴歸的一般形式。具體地說,當 \textstyle k = 2 時,softmax 迴歸的假設函式為:
\begin{align}
h_\theta(x) &=

\frac{1}{ e^{\theta_1^Tx}  + e^{ \theta_2^T x^{(i)} } }
\begin{bmatrix}
e^{ \theta_1^T x } \\
e^{ \theta_2^T x }
\end{bmatrix}
\end{align}
,對該式進行化簡得到:


\begin{align}
h(x) &=

\frac{1}{ e^{\vec{0}^Tx}  + e^{ (\theta_2-\theta_1)^T x^{(i)} } }
\begin{bmatrix}
e^{ \vec{0}^T x } \\
e^{ (\theta_2-\theta_1)^T x }
\end{bmatrix} \\


&=
\begin{bmatrix}
\frac{1}{ 1 + e^{ (\theta_2-\theta_1)^T x^{(i)} } } \\
\frac{e^{ (\theta_2-\theta_1)^T x }}{ 1 + e^{ (\theta_2-\theta_1)^T x^{(i)} } }
\end{bmatrix} \\

&=
\begin{bmatrix}
\frac{1}{ 1  + e^{ (\theta_2-\theta_1)^T x^{(i)} } } \\
1 - \frac{1}{ 1  + e^{ (\theta_2-\theta_1)^T x^{(i)} } } \\
\end{bmatrix}
\end{align}

另 \textstyle \theta'來表示\textstyle \theta_2-\theta_1,我們就會發現 softmax 迴歸器預測其中一個類別的概率為 \textstyle \frac{1}{ 1  + e^{ (\theta')^T x^{(i)} } },另一個類別概率的為 \textstyle 1 - \frac{1}{ 1 + e^{ (\theta')^T x^{(i)} } },這與 logistic迴歸是一致的。

其中 \theta_1, \theta_2, \ldots, \theta_k \in \Re^{n+1} 是模型的引數。把引數 \textstyle \theta 表示為矩陣形式有, \textstyle \theta 是一個 \textstyle k \times(n+1) 的矩陣,該矩陣是將 \theta_1, \theta_2, \ldots, \theta_k 按行羅列起來得到的:


\theta = \begin{bmatrix}
\mbox{---} \theta_1^T \mbox{---} \\
\mbox{---} \theta_2^T \mbox{---} \\
\vdots \\
\mbox{---} \theta_k^T \mbox{---} \\
\end{bmatrix}

有個假設函式(Hypothesis Function),下面來看代價函式,根據代價函式求解出最優引數值帶入假設函式即可求得最終的模型,先引入函式\textstyle 1\{\cdot\},對於該函式有:

\textstyle 1\{值為真的表示式 \textstyle \}=1    \textstyle 1\{ 值為假的表示式 \textstyle \}=0

舉例來說,表示式 \textstyle 1\{2+2=4\} 的值為1 ,\textstyle 1\{1+1=5\}的值為 0 。

則softmax的損失函式為:


\begin{align}
J(\theta) = - \frac{1}{m} \left[ \sum_{i=1}^{m} \sum_{j=1}^{k}  1\left\{y^{(i)} = j\right\} \log \frac{e^{\theta_j^T x^{(i)}}}{\sum_{l=1}^k e^{ \theta_l^T x^{(i)} }}\right]
\end{align}

 

當k=2時,即有logistic的形式,下邊是推倒:

 

另上式中的便得到了logistic迴歸的損失函式。

可以看到,softmax與logistic的損失函式只是k的取值不同而已,且在softmax中將類別x歸為類別j的概率為:


p(y^{(i)} = j | x^{(i)} ; \theta) = \frac{e^{\theta_j^T x^{(i)}}}{\sum_{l=1}^k e^{ \theta_l^T x^{(i)}} }
.

需要注意的一個問題是softmax迴歸中的模型引數化問題,即softmax的引數集是“冗餘的”。

假設從引數向量 \textstyle \theta_j 中減去了向量 \textstyle \psi,這時,每一個 \textstyle \theta_j都變成了 \textstyle \theta_j - \psi(\textstyle j=1, \ldots, k)。此時假設函式變成了以下的式子:


\begin{align}
p(y^{(i)} = j | x^{(i)} ; \theta)
&= \frac{e^{(\theta_j-\psi)^T x^{(i)}}}{\sum_{l=1}^k e^{ (\theta_l-\psi)^T x^{(i)}}}  \\
&= \frac{e^{\theta_j^T x^{(i)}} e^{-\psi^Tx^{(i)}}}{\sum_{l=1}^k e^{\theta_l^T x^{(i)}} e^{-\psi^Tx^{(i)}}} \\
&= \frac{e^{\theta_j^T x^{(i)}}}{\sum_{l=1}^k e^{ \theta_l^T x^{(i)}}}.
\end{align}

也就是說,從 \textstyle \theta_j 中減去 \textstyle \psi 完全不影響假設函式的預測結果,這就說明 Softmax 模型被過度引數化了。對於任意一個用於擬合數據的假設函式,可以求出多組引數值,這些引數得到的是完全相同的假設函式 \textstyle h_\theta,也就是說如果引數集合 \textstyle (\theta_1, \theta_2,\ldots, \theta_k) 是代價函式 \textstyle J(\theta) 的極小值點,那麼\textstyle (\theta_1 - \psi, \theta_2 - \psi,\ldots,
\theta_k - \psi) 同樣也是它的極小值點,其中 \textstyle \psi 可以是任意向量,到底是什麼造成的呢?從巨集觀上可以這麼理解,因為此時的損失函式不是嚴格非凸的,也就是說在區域性最小值點附近是一個”平坦”的,所以在這個引數附近的值都是一樣的了。平坦假設函式空間的Hessian 矩陣是奇異的/不可逆的,這會直接導致採用牛頓法優化就遇到數值計算的問題。因此使 \textstyle J(\theta) 最小化的解不是唯一的。但此時 \textstyle J(\theta) 仍然是一個凸函式,因此梯度下降時不會遇到區域性最優解的問題。

還有一個值得注意的地方是:當 \textstyle \psi = \theta_1 時,我們總是可以將 \textstyle \theta_1替換為\textstyle \theta_1 - \psi = \vec{0}(即替換為全零向量),並且這種變換不會影響假設函式。因此我們可以去掉引數向量 \textstyle \theta_1(或者其他 \textstyle \theta_j 中的任意一個)而不影響假設函式的表達能力。實際上,與其優化全部的 \textstyle k\times(n+1) 個引數 \textstyle (\theta_1, \theta_2,\ldots, \theta_k) (其中 \textstyle \theta_j \in \Re^{n+1}),我們可以令 \textstyle \theta_1 =
\vec{0},只優化剩餘的 \textstyle (k-1)\times(n+1) 個引數,這樣演算法依然能夠正常工作。比如logistic就是這樣的。

在實際應用中,為了使演算法看起來更直觀更清楚,往往保留所有引數 \textstyle (\theta_1, \theta_2,\ldots, \theta_n) ,而不任意地將某一引數設定為 0。但此時需要對代價函式做一個改動:加入權重衰減。權重衰減可以解決 softmax 迴歸的引數冗餘所帶來的數值問題。

目前對損失函式 \textstyle J(\theta) 的最小化還沒有封閉解(closed-form),因此使用迭代的方法求解,如(Gradient Descent或者L-BFGS),經過求導,得到的梯度公式:


\begin{align}
\nabla_{\theta_j} J(\theta) = - \frac{1}{m} \sum_{i=1}^{m}{ \left[ x^{(i)} \left( 1\{ y^{(i)} = j\}  - p(y^{(i)} = j | x^{(i)}; \theta) \right) \right]  }
\end{align}

\textstyle \nabla_{\theta_j} J(\theta) 本身是一個向量,它的第 \textstyle l 個元素 \textstyle \frac{\partial J(\theta)}{\partial \theta_{jl}} 是 \textstyle J(\theta)\textstyle \theta_j 的第 \textstyle l 個分量的偏導數。在梯度下降法的標準實現中,每一次迭代需要進行如下更新: \textstyle \theta_j := \theta_j - \alpha \nabla_{\theta_j} J(\theta)(\textstyle j=1,\ldots,k)。(\textstyle \nabla_{\theta_j} J(\theta) 為方向,a代表在這個方向的步長)

由於引數數量的龐大,所以可能需要權重衰減項來防止過擬合,一般的演算法中都會有該項。新增一個權重衰減項 \textstyle \frac{\lambda}{2} \sum_{i=1}^k \sum_{j=0}^{n} \theta_{ij}^2 來修改代價函式,這個衰減項會懲罰過大的引數值,現在我們的代價函式變為:


\begin{align}
J(\theta) = - \frac{1}{m} \left[ \sum_{i=1}^{m} \sum_{j=1}^{k} 1\left\{y^{(i)} = j\right\} \log \frac{e^{\theta_j^T x^{(i)}}}{\sum_{l=1}^k e^{ \theta_l^T x^{(i)} }}  \right]
              + \frac{\lambda}{2} \sum_{i=1}^k \sum_{j=0}^n \theta_{ij}^2
\end{align}


有了這個權重衰減項以後 (\textstyle \lambda > 0),代價函式就變成了嚴格的凸函式,這樣就可以保證得到唯一的解了。 此時的 Hessian矩陣變為可逆矩陣,並且因為\textstyle J(\theta)是凸函式,梯度下降法和 L-BFGS 等演算法可以保證收斂到全域性最優解。

為了使用優化演算法,我們需要求得這個新函式 \textstyle J(\theta) 的導數,如下:


\begin{align}
\nabla_{\theta_j} J(\theta) = - \frac{1}{m} \sum_{i=1}^{m}{ \left[ x^{(i)} ( 1\{ y^{(i)} = j\}  - p(y^{(i)} = j | x^{(i)}; \theta) ) \right]  } + \lambda \theta_j
\end{align}

通過最小化 \textstyle J(\theta),我們就能實現一個可用的 softmax 迴歸模型。

 

最後一個問題在logistic的文章裡提到過,關於分類器選擇的問題,是使用logistic建立k個分類器呢,還是直接使用softmax迴歸,這取決於資料之間是否是互斥的,k-logistic演算法可以解決互斥問題,而softmax不可以解決,比如將影象分到三個不同類別中。(i) 假設這三個類別分別是:室內場景、戶外城區場景、戶外荒野場景。 (ii) 現在假設這三個類別分別是室內場景、黑白圖片、包含人物的圖片

考慮到處理的問題的不同,在第一個例子中,三個類別是互斥的,因此更適於選擇softmax迴歸分類器 。而在第二個例子中,建立三個獨立的 logistic迴歸分類器更加合適。最後補一張k-logistic的圖片: