【机器学习】Softmax 和Logistic Regression回归Sigmod

 二分类问题Sigmod

  在 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}

我们将训练模型参数 \textstyle \theta,使其能够最小化代价函数 :


\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}

多分类问题

   在一个多分类问题中,因变量y有k个取值,即。例如在邮件分类问题中,我们要把邮件分为垃圾邮件、个人邮件、工作邮件3类,目标值y是一个有3个取值的离散值。这是一个多分类问题,二分类模型在这里不太适用。

  主要应用就是多分类,sigmoid函数只能分两类,而softmax能分多类,softmax是sigmoid的扩展。

  Logistic函数只能被使用在二分类问题中,但是它的多项式回归,即softmax函数,可以解决多分类问题。

  在 softmax回归中,我们解决的是多分类问题(相对于 logistic 回归解决的二分类问题),类标 \textstyle y 可以取 \textstyle k 个不同的值(而不是 2 个)。因此,对于训练集 \{ (x^{(1)}, y^{(1)}), \ldots, (x^{(m)}, y^{(m)}) \},我们有 y^{(i)} \in \{1, 2, \ldots, k\}。(注意此处的类别下标从 1 开始,而不是 0) 

  对于给定的测试输入 \textstyle x,我们想用假设函数针对每一个类别j估算出概率值 \textstyle p(y=j | x)。也就是说,我们想估计 \textstyle x 的每一种分类结果出现的概率。因此,我们的假设函数将要输出一个 \textstyle k 维的向量(向量元素的和为1)来表示这 \textstyle k 个估计的概率值。 具体地说,我们的假设函数 \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}


  其中 \theta_1, \theta_2, \ldots, \theta_k \in \Re^{n+1} 是模型的参数。请注意 \frac{1}{ \sum_{j=1}^{k}{e^{ \theta_j^T x^{(i)} }} } 这一项对概率分布进行归一化,使得所有概率之和为 1 。


  为了方便起见,我们同样使用符号 \textstyle \theta 来表示全部的模型参数。在实现Softmax回归时,将 \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}

代价函数

\textstyle 1\{ 值为假的表达式 \textstyle \}=0。举例来说,表达式 \textstyle 1\{2+2=4\} 的值为1 ,\textstyle 1\{1+1=5\}的值为 0。我们的代价函数为:


\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}


值得注意的是,上述公式是logistic回归代价函数的推广。logistic回归代价函数可以改为:


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


可以看到,Softmax代价函数与logistic 代价函数在形式上非常类似,只是在Softmax损失函数中对类标记的 k 个可能值进行了累加。注意在Softmax回归中将 x 分类为类别 \textstyle 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)}} }
.


对于 \textstyle J(\theta) 的最小化问题,目前还没有闭式解法。因此,我们使用迭代的优化算法(例如梯度下降法,或 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}" 的含义。\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 J(\theta)。 例如,在梯度下降法的标准实现中,每一次迭代需要进行如下更新: \textstyle \theta_j := \theta_j - \alpha \nabla_{\theta_j} J(\theta)(\textstyle j=1,\ldots,k)。

当实现 softmax 回归算法时, 我们通常会使用上述代价函数的一个改进版本。

Softmax回归与Logistic 回归的关系

当类别数 \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}


利用softmax回归参数冗余的特点,我们令 \textstyle \psi = \theta_1,并且从两个参数向量中都减去向量 \textstyle \theta_1,得到:


\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回归是一致的。

广义线性模型

linear,Logistic,Softmax 都是一个东西推导出来的。
这些分布之所以长成这个样子,是因为我们对y进行了假设。
当y是两点分布-------->linear model
当y是正态分布-------->Logistic model
当y是多项式分布-------->Softmax

http://ufldl.stanford.edu/wiki/index.php/Softmax回归#Softmax.E5.9B.9E.E5.BD.92.E4.B8.8ELogistic_.E5.9B.9E.E5.BD.92.E7.9A.84.E5.85.B3.E7.B3.BB

posted @ 2017-06-04 14:43  陈泽泽  阅读(523)  评论(0编辑  收藏  举报