第三部分:
1.指数分布族
2.高斯分布--->>>最小二乘法
3.泊松分布--->>>线性回归
4.Softmax回归
指数分布族:
结合Ng的课程,在看这篇博文:http://blog.csdn.net/acdreamers/article/details/44663091
泊松分布:
这里是一个扩展,看不看都可以:http://www.ruanyifeng.com/blog/2015/06/poisson-distribution.html
Softmax回归:
有点难度的,看了3个多小时才看懂。自己就不重复造轮子了,以下是在原文的基础上做的笔记,直接看真的很懵逼!
简介:
在本节中,我们介绍Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签
![\textstyle y](https://i-blog.csdnimg.cn/blog_migrate/2363af4f6144678cf56a3c41a24bca75.png)
回想一下在 logistic 回归中,我们的训练集由
![\textstyle m](https://i-blog.csdnimg.cn/blog_migrate/8346ed03fe2679b3d456d2675b11314b.png)
![\{ (x^{(1)}, y^{(1)}), \ldots, (x^{(m)}, y^{(m)}) \}](https://i-blog.csdnimg.cn/blog_migrate/f36f01c6eace252362eca757a8073425.png)
![x^{(i)} \in \Re^{n+1}](https://i-blog.csdnimg.cn/blog_migrate/61a8d492837caf7a33c496d806827e61.png)
![\textstyle x](https://i-blog.csdnimg.cn/blog_migrate/de801af62f7ddd0ff4edde9cfd76568a.png)
![\textstyle n+1](https://i-blog.csdnimg.cn/blog_migrate/4b031a7181796fb8b29fb09008b36c2a.png)
![\textstyle x_0 = 1](https://i-blog.csdnimg.cn/blog_migrate/8363435cdf2ea5b8687f9bd899572c89.png)
![y^{(i)} \in \{0,1\}](https://i-blog.csdnimg.cn/blog_migrate/dd451cd55050020e477c271a186441c1.png)
注释:这是已经利用泊松分布概率推到的函数。
![\begin{align}
h_\theta(x) = \frac{1}{1+\exp(-\theta^Tx)},
\end{align}](https://i-blog.csdnimg.cn/blog_migrate/50ec70dbaddf0f5add4ca9c60c24b66e.png)
我们将训练模型参数
![\textstyle \theta](https://i-blog.csdnimg.cn/blog_migrate/2e702f591fb7cbab0ff89c9ea10be2ea.png)
注释:代价函数在这里的理解就是所有样本概率求和函数。
![\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}](https://i-blog.csdnimg.cn/blog_migrate/3e06cbcef214178f7244f38e8a8d0630.png)
在 softmax回归中,我们解决的是多分类问题(相对于 logistic 回归解决的二分类问题),类标
![\textstyle y](https://i-blog.csdnimg.cn/blog_migrate/1bb9e0fa6bab62b15328ced1836e290a.png)
![\textstyle k](https://i-blog.csdnimg.cn/blog_migrate/7027b93a00941ab7f2446524cff5e01e.png)
![\{ (x^{(1)}, y^{(1)}), \ldots, (x^{(m)}, y^{(m)}) \}](https://i-blog.csdnimg.cn/blog_migrate/2b46c07843597d2448f674a2d1afcf03.png)
![y^{(i)} \in \{1, 2, \ldots, k\}](https://i-blog.csdnimg.cn/blog_migrate/4e8073068c661e1d39da06e5d551eeef.png)
![\textstyle k=10](https://i-blog.csdnimg.cn/blog_migrate/8b0fb97719f14f9ab7d3d87e837ef0b8.png)
对于给定的测试输入
![\textstyle x](https://i-blog.csdnimg.cn/blog_migrate/5afb5129772e4f8efb54052d507a6797.png)
![\textstyle p(y=j | x)](https://i-blog.csdnimg.cn/blog_migrate/a183d270e84359a5322627aeda3091c3.png)
![\textstyle x](https://i-blog.csdnimg.cn/blog_migrate/69bffb4bd4a05c021d7833e7bd488053.png)
![\textstyle k](https://i-blog.csdnimg.cn/blog_migrate/bec82506cc33f0ebe8edcf29cc5f9962.png)
![\textstyle k](https://i-blog.csdnimg.cn/blog_migrate/c2222356066dabf36e4f279e09d5da27.png)
![\textstyle h_{\theta}(x)](https://i-blog.csdnimg.cn/blog_migrate/95d2c030cf016ec0d2ce5e4b87c75d1a.png)
注释:此图是对下面表达式的说明。
![](https://i-blog.csdnimg.cn/blog_migrate/af5196fcc779ead80020ed8bce2ffefa.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/867bf9e2dbe491d1bbad13955f5cd0b7.png)
其中
![\theta_1, \theta_2, \ldots, \theta_k \in \Re^{n+1}](https://i-blog.csdnimg.cn/blog_migrate/fa5f7f31b45566fc7d2fed10384ccfc4.png)
![\frac{1}{ \sum_{j=1}^{k}{e^{ \theta_j^T x^{(i)} }} }](https://i-blog.csdnimg.cn/blog_migrate/19336754597645583b7b7a8f774248b8.png)
为了方便起见,我们同样使用符号
![\textstyle \theta](https://i-blog.csdnimg.cn/blog_migrate/3842f01b1400b38f64b7b6944fe2cf24.png)
![\textstyle \theta](https://i-blog.csdnimg.cn/blog_migrate/78fbd2e69680864815ab4c5e1fb98bae.png)
![\textstyle k \times(n+1)](https://i-blog.csdnimg.cn/blog_migrate/f11bd9e867d848bd46a416b6b83d1835.png)
![\theta_1, \theta_2, \ldots, \theta_k](https://i-blog.csdnimg.cn/blog_migrate/f6b795302079f457f6d2cabc60f14614.png)
注释:每个thea都是n行向量,前面求解回归方程已经说明。
![\theta = \begin{bmatrix}
\mbox{---} \theta_1^T \mbox{---} \\
\mbox{---} \theta_2^T \mbox{---} \\
\vdots \\
\mbox{---} \theta_k^T \mbox{---} \\
\end{bmatrix}](https://i-blog.csdnimg.cn/blog_migrate/38d1022590b14cfb13f917634ef413f5.png)
代价函数:
现在我们来介绍 softmax 回归算法的代价函数。在下面的公式中,
![\textstyle 1\{\cdot\}](https://i-blog.csdnimg.cn/blog_migrate/6ef49c33202b3d434fdced8b0aa4da32.png)
注释:这里是个判断函数,在数学表达式中很少,但是程序直接写 print_value = a==b ? 1 : 0;
1{值为真的表达式 }=1,
![\textstyle 1\{](https://i-blog.csdnimg.cn/blog_migrate/5f0f8335780b9c1acf8b672a7403b4c0.png)
![\textstyle \}=0](https://i-blog.csdnimg.cn/blog_migrate/37e1466010476a965fde0291303f6e9f.png)
举例来说,表达式
![\textstyle 1\{2+2=4\}](https://i-blog.csdnimg.cn/blog_migrate/30d231134c8d3cbf8ca2596f17864294.png)
![\textstyle 1\{1+1=5\}](https://i-blog.csdnimg.cn/blog_migrate/661df4a959b47841e4ff882318b23e65.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/6fdc8ac57bc8db67dd2a8dcb3e07addd.png)
值得注意的是,上述公式是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}](https://i-blog.csdnimg.cn/blog_migrate/24f93554538f551c8b2bdc5b842c1d4c.png)
可以看到,Softmax代价函数与logistic 代价函数在形式上非常类似,只是在Softmax损失函数中对类标记的
![\textstyle k](https://i-blog.csdnimg.cn/blog_migrate/d1b665a9d0659557a6edfc88285003c7.png)
![\textstyle x](https://i-blog.csdnimg.cn/blog_migrate/5cda73959e89de11de639bef9bc5816d.png)
![\textstyle j](https://i-blog.csdnimg.cn/blog_migrate/e78385e98417fc2533c089df629269c0.png)
对于
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/0ac1fca74988595dd4725e5a3f5068dc.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/710b69382de0470da0ed807679892599.png)
让我们来回顾一下符号 "
![\textstyle \nabla_{\theta_j}](https://i-blog.csdnimg.cn/blog_migrate/c86bad3c48ea88bb494a9200ae773e27.png)
![\textstyle \nabla_{\theta_j} J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/4a8175f694eb74e6049ad77e03c47659.png)
![\textstyle l](https://i-blog.csdnimg.cn/blog_migrate/3e22af3c4e7b8e7e304b5a21ce15be01.png)
![\textstyle \frac{\partial J(\theta)}{\partial \theta_{jl}}](https://i-blog.csdnimg.cn/blog_migrate/4cc0e2c3ef6413fa722892e9ae2d8a25.png)
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/89318f12e0628f95cb729f1d27a784f7.png)
![\textstyle \theta_j](https://i-blog.csdnimg.cn/blog_migrate/b9e5deb065b17601325aacee8f9c2530.png)
![\textstyle l](https://i-blog.csdnimg.cn/blog_migrate/71a207ed8c956d8f46423e0b156a1456.png)
有了上面的偏导数公式以后,我们就可以将它代入到梯度下降法等算法中,来最小化
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/c59f1f27170bf616908ed24165045601.png)
![\textstyle \theta_j := \theta_j - \alpha \nabla_{\theta_j} J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/d7d9c49b89f9cc07e371c47d7898f615.png)
![\textstyle j=1,\ldots,k](https://i-blog.csdnimg.cn/blog_migrate/70fe534b8d8c7c419518ef1051c143f9.png)
当实现 softmax 回归算法时, 我们通常会使用上述代价函数的一个改进版本。具体来说,就是和权重衰减(weight decay)一起使用。我们接下来介绍使用它的动机和细节。
Softmax回归模型参数化的特点:
Softmax 回归有一个不寻常的特点:它有一个“冗余”的参数集。为了便于阐述这一特点,假设我们从参数向量
![\textstyle \theta_j](https://i-blog.csdnimg.cn/blog_migrate/7b74f5f52b732e654cde360a4632bbc9.png)
![\textstyle \psi](https://i-blog.csdnimg.cn/blog_migrate/296f39f3da3c7d3750d3cbccb0b7723d.png)
![\textstyle \theta_j](https://i-blog.csdnimg.cn/blog_migrate/50190e5e73d99917756b02b5e420d6bc.png)
![\textstyle \theta_j - \psi](https://i-blog.csdnimg.cn/blog_migrate/50779d6bf427ca02e705e8d501feb194.png)
![\textstyle j=1, \ldots, k](https://i-blog.csdnimg.cn/blog_migrate/11ce1065c1fccc272c33b409ac4b66b9.png)
注释:这个“冗余”的意思是参数太多,N方程解N个未知数,现在出现N个方程N+1个未知数,那么出现的结果就是未知数的解不唯一。
![\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}](https://i-blog.csdnimg.cn/blog_migrate/2ca99c5ccf7f2ccdd75d37592b0b08cf.png)
换句话说,从
![\textstyle \theta_j](https://i-blog.csdnimg.cn/blog_migrate/bce55d9c2cbda02de97eaf3d6c00dd02.png)
![\textstyle \psi](https://i-blog.csdnimg.cn/blog_migrate/c6c4c453c7591e29dcd448e90cecb16d.png)
![\textstyle h_\theta](https://i-blog.csdnimg.cn/blog_migrate/009c3a983c848514027a30027eb1fabb.png)
进一步而言,如果参数
![\textstyle (\theta_1, \theta_2,\ldots, \theta_k)](https://i-blog.csdnimg.cn/blog_migrate/a586e9145c767107c867ad93d0d5bd63.png)
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/38f5b287ee0f3666132c6156f9567349.png)
![\textstyle (\theta_1 - \psi, \theta_2 - \psi,\ldots,
\theta_k - \psi)](https://i-blog.csdnimg.cn/blog_migrate/aef4ba9c6ec4e1cab5b200da7a8f118e.png)
![\textstyle \psi](https://i-blog.csdnimg.cn/blog_migrate/62ff0027c9d1a2dec9f546c40ef90927.png)
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/129443ba5659dbb75ed5901851ddfa95.png)
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/89fd106bbcf2869747d01ea75c862492.png)
注释:上面已经说明解不唯一,那么就等于这个函数的最大似然函数不收敛-->>不存在局部最优解-->>Hessian矩阵是不存在的-->>那么最大似然函数就是无解的。。。
注意,当
![\textstyle \psi = \theta_1](https://i-blog.csdnimg.cn/blog_migrate/f57e2db5b0bea8b8c1400bc2047c0146.png)
![\textstyle \theta_1](https://i-blog.csdnimg.cn/blog_migrate/17ed687857dfbea08c29d712aae57c5f.png)
![\textstyle \theta_1 - \psi = \vec{0}](https://i-blog.csdnimg.cn/blog_migrate/09efea54e10eb88adde752811e050f99.png)
![\textstyle \theta_1](https://i-blog.csdnimg.cn/blog_migrate/03f02269a0dda8f5925db10cec03e819.png)
![\textstyle \theta_j](https://i-blog.csdnimg.cn/blog_migrate/ed9e1949bdd4ae12ca1f37839f53870d.png)
![\textstyle k\times(n+1)](https://i-blog.csdnimg.cn/blog_migrate/66b6a0dc2d553d4b42b507709239b510.png)
![\textstyle (\theta_1, \theta_2,\ldots, \theta_k)](https://i-blog.csdnimg.cn/blog_migrate/1adbf182639b43b48e29ab9015b089f7.png)
![\textstyle \theta_j \in \Re^{n+1}](https://i-blog.csdnimg.cn/blog_migrate/556194ff3250f03853a5e6d7a9fcc54f.png)
![\textstyle \theta_1 =
\vec{0}](https://i-blog.csdnimg.cn/blog_migrate/6b3480c581586414a6300689c1e38a97.png)
![\textstyle (k-1)\times(n+1)](https://i-blog.csdnimg.cn/blog_migrate/5c7cf55d607fe78f07ce028858f601c4.png)
在实际应用中,为了使算法实现更简单清楚,往往保留所有参数
![\textstyle (\theta_1, \theta_2,\ldots, \theta_n)](https://i-blog.csdnimg.cn/blog_migrate/dd7c775e53e9f841a82f0521e5a6d586.png)
权重衰减:
![](https://i-blog.csdnimg.cn/blog_migrate/b3cf2d67900a12965cbf0c1fcc47fa0e.png)
我们通过添加一个权重衰减项
![\textstyle \frac{\lambda}{2} \sum_{i=1}^k \sum_{j=0}^{n} \theta_{ij}^2](https://i-blog.csdnimg.cn/blog_migrate/5c39546153e8c7e8836e58453d195fee.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/f31a6d994b4704c84b432e4ec396f1b8.png)
有了这个权重衰减项以后 (
![\textstyle \lambda > 0](https://i-blog.csdnimg.cn/blog_migrate/9ec5a644d01ec49fac39cfe9644e2a7b.png)
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/07e29146b97745b9fa59021fbfb77b86.png)
为了使用优化算法,我们需要求得这个新函数
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/f832fba1b4038f5d4bcd300cf5175303.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/5d3fa9d31e60794f7e86e4116b70a42e.png)
通过最小化
![\textstyle J(\theta)](https://i-blog.csdnimg.cn/blog_migrate/2b325d9c7af8e5d66bae06e996a14d3a.png)
Softmax回归与Logistic 回归的关系:
当类别数
![\textstyle k = 2](https://i-blog.csdnimg.cn/blog_migrate/d3e20157749078f0355c2512c7f99443.png)
![\textstyle k = 2](https://i-blog.csdnimg.cn/blog_migrate/e9dab76fc8a0dc51941dcb533b145988.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/27698749700bc6da4bdb65d16439d7d0.png)
利用softmax回归参数冗余的特点,我们令
![\textstyle \psi = \theta_1](https://i-blog.csdnimg.cn/blog_migrate/4836dd3a90aeddd33bb66b93a586cac7.png)
![\textstyle \theta_1](https://i-blog.csdnimg.cn/blog_migrate/00c795e8f12efa093f7318239be789e8.png)
![\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}](https://i-blog.csdnimg.cn/blog_migrate/05570ec6c08a656dcac36653cfee3b80.png)
因此,用
![\textstyle \theta'](https://i-blog.csdnimg.cn/blog_migrate/79bfdceae36f18b696a12dcad611eae4.png)
![\textstyle \theta_2-\theta_1](https://i-blog.csdnimg.cn/blog_migrate/4212ff256f253a75b3ec0f6cc19b75e8.png)
![\textstyle \frac{1}{ 1 + e^{ (\theta')^T x^{(i)} } }](https://i-blog.csdnimg.cn/blog_migrate/382a8b2e88183b1cb772374897b3aa4a.png)
![\textstyle 1 - \frac{1}{ 1 + e^{ (\theta')^T x^{(i)} } }](https://i-blog.csdnimg.cn/blog_migrate/8100e0e0f2a6a7137a24c1352793ce23.png)
Softmax 回归 vs. k 个二元分类器:
注释:这里好理解了,唯一性用SoftMax,不唯一用K个二分类器。
如果你在开发一个音乐分类的应用,需要对k种类型的音乐进行识别,那么是选择使用 softmax 分类器呢,还是使用 logistic 回归算法建立 k 个独立的二元分类器呢?
这一选择取决于你的类别之间是否互斥,例如,如果你有四个类别的音乐,分别为:古典音乐、乡村音乐、摇滚乐和爵士乐,那么你可以假设每个训练样本只会被打上一个标签(即:一首歌只能属于这四种音乐类型的其中一种),此时你应该使用类别数 k = 4 的softmax回归。(如果在你的数据集中,有的歌曲不属于以上四类的其中任何一类,那么你可以添加一个“其他类”,并将类别数 k 设为5。)
如果你的四个类别如下:人声音乐、舞曲、影视原声、流行歌曲,那么这些类别之间并不是互斥的。例如:一首歌曲可以来源于影视原声,同时也包含人声 。这种情况下,使用4个二分类的 logistic 回归分类器更为合适。这样,对于每个新的音乐作品 ,我们的算法可以分别判断它是否属于各个类别。
现在我们来看一个计算视觉领域的例子,你的任务是将图像分到三个不同类别中。(i) 假设这三个类别分别是:室内场景、户外城区场景、户外荒野场景。你会使用sofmax回归还是 3个logistic 回归分类器呢? (ii) 现在假设这三个类别分别是室内场景、黑白图片、包含人物的图片,你又会选择 softmax 回归还是多个 logistic 回归分类器呢?
在第一个例子中,三个类别是互斥的,因此更适于选择softmax回归分类器 。而在第二个例子中,建立三个独立的 logistic回归分类器更加合适。
原文地址:http://deeplearning.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92