softmax分类函数
这部分教程将介绍两部分:
- softmax函数
- 交叉熵损失函数
在先前的教程中,我们已经使用学习了如何使用Logistic函数来实现二分类问题。对于多分类问题,我们可以使用多项Logistic回归,该方法也被称之为softmax函数。接下来,我们来解释什么事softmax函数,以及怎么得到它。
我们先导入教程需要使用的软件包。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import colorConverter, ListedColormap
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
Softmax函数
在之前的教程中,我们已经知道了Logistic函数只能被使用在二分类问题中,但是它的多项式回归,即softmax函数,可以解决多分类问题。假设softmax函数ς
的输入数据是C
维度的向量z
,那么softmax函数的数据也是一个C
维度的向量y
,里面的值是0或者1。softmax函数其实就是一个归一化的指数函数,定义如下:
![](https://i-blog.csdnimg.cn/blog_migrate/e116e6f2d0abf43b887982aa3a9a2ca3.webp?x-image-process=image/format,png)
式子中的分母充当了正则项的作用,可以使得
![](https://i-blog.csdnimg.cn/blog_migrate/55d011a6065fe14cd151b454d667553b.webp?x-image-process=image/format,png)
作为神经网络的输出层,softmax函数中的值可以用C
个神经元来表示。
对于给定的输入z
,我们可以得到每个分类的概率t = c for c = 1 ... C
可以表示为:
![](https://i-blog.csdnimg.cn/blog_migrate/888dac11dbfd659161cf9852ecd0e835.webp?x-image-process=image/format,png)
其中,P(t=c|z)
表示,在给定输入z
时,该输入数据是c
分类的概率。
下图展示了在一个二分类(t = 1, t = 2)
中,输入向量是z = [z1, z2]
,那么输出概率P(t=1|z)
如下图所示。
# Define the softmax function
def softmax(z):
return np.exp(z) / np.sum(np.exp(z))
# Plot the softmax output for 2 dimensions for both classes
# Plot the output in function of the weights
# Define a vector of weights for which we want to plot the ooutput
nb_of_zs = 200
zs = np.linspace(-10, 10, num=nb_of_zs) # input
zs_1, zs_2 = np.meshgrid(zs, zs) # generate grid
y = np.zeros((nb_of_zs, nb_of_zs, 2)) # initialize output
# Fill the output matrix for each combination of input z's
for i in range(nb_of_zs):
for j in range(nb_of_zs):
y[i,j,:] = softmax(np.asarray([zs_1[i,j], zs_2[i,j]]))
# Plot the cost function surfaces for both classes
fig = plt.figure()
# Plot the cost function surface for t=1
ax = fig.gca(projection='3d')
surf = ax.plot_surface(zs_1, zs_2, y[:,:,0], linewidth=0, cmap=cm.coolwarm)
ax.view_init(elev=30, azim=70)
cbar = fig.colorbar(surf)
ax.set_xlabel('$z_1$', fontsize=15)
ax.set_ylabel('$z_2$', fontsize=15)
ax.set_zlabel('$y_1$', fontsize=15)
ax.set_title ('$P(t=1|\mathbf{z})$')
cbar.ax.set_ylabel('$P(t=1|\mathbf{z})$', fontsize=15)
plt.grid()
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/bf0b834387e5ed4bb6ad8fb871a69b28.webp?x-image-process=image/format,png)
softmax函数的导数
在神经网络中,使用softmax函数,我们需要知道softmax函数的导数。如果我们定义:
![](https://i-blog.csdnimg.cn/blog_migrate/6ab34ccb5dd73dfa89734ab8519b9291.webp?x-image-process=image/format,png)
那么可以得到:
![](https://i-blog.csdnimg.cn/blog_migrate/6a4b65ae306ade67d240260e9353abc9.webp?x-image-process=image/format,png)
因此,softmax函数的输出结果y
对于它的输入数据z
的导数∂yi/∂zj
可以定义为:
![](https://i-blog.csdnimg.cn/blog_migrate/a501f783510b34759802595b049b835b.webp?x-image-process=image/format,png)
注意,当i = j
时,softmax函数的倒数推导结果和Logistic函数一样。
softmax函数的交叉熵损失函数
在学习softmax函数的损失函数之前,我们先从学习它的最大似然函数开始。给定模型的参数组θ
,利用这个参数组,我们可以得到输入样本的正确预测,正如在Logistic损失函数推导中,我们可以仿照写出这个的最大似然估计:
![](https://i-blog.csdnimg.cn/blog_migrate/ade30c9e8fe01d0152d0a02c1a899397.webp?x-image-process=image/format,png)
根据联合概率,我们可以将似然函数改写成:P(t,z|θ)
,根据条件分布,我们最终可以得到如下公式:
![](https://i-blog.csdnimg.cn/blog_migrate/524506f1bd2715e6317c471ebb537a6a.webp?x-image-process=image/format,png)
因为我们不关心z
的概率,所以公式又可以改写为:L(θ|t,z)=P(t|z,θ)
。而且,P(t|z, θ)
可以被写成P(t|z)
,如果θ
会一个定值。因为,每一个ti
都是依赖于整个z
,而且只有其中一个t
将会被激活,所以我们可以得到下式:
![](https://i-blog.csdnimg.cn/blog_migrate/c120b5d90cbd90eb4123690336ae94f4.webp?x-image-process=image/format,png)
正如我们在Logistic函数中推导损失函数的导数一样,最大化似然函数就是最小化它的负对数释然函数:
![](https://i-blog.csdnimg.cn/blog_migrate/df71ac98eaf7552841321d6ef5afc9c4.webp?x-image-process=image/format,png)
其中,ξ
表示交叉熵误差函数。在二分类问题中,我们将t2
定义为t2=1−t1
。同理,在softmax函数中,我们也可以定义为:
![](https://i-blog.csdnimg.cn/blog_migrate/60dac92cf08f25ec689d33289976817e.webp?x-image-process=image/format,png)
在n
个样本的批处理中,交叉熵误差函数可以这样计算:
![](https://i-blog.csdnimg.cn/blog_migrate/1b47b3fce89ac56291081f1b74491ab8.webp?x-image-process=image/format,png)
其中,当且仅当tic
是1
,那么样本i
是属于类别c
,yic
是样本i
属于类别c
的概率。
softmax函数的交叉熵损失函数的推导
损失函数对于zi
的导数∂ξ/∂zi
求解如下:
![](https://i-blog.csdnimg.cn/blog_migrate/7454363729014007d9a6a1d1f36e66ed.webp?x-image-process=image/format,png)
上式已经求解了当i=j
和i≠j
的两种情况。
最终的结果为∂ξ/∂zi=yi−ti for all i ∈ C
,这个求导结果和Logistic函数的交叉熵损失函数求导是一样的,再次证明softmax函数是Logistic函数的一个扩展板。