博客: blog.shinelee.me | 博客园 | CSDN
交叉熵损失与均方误差损失
常规分类网络最后的softmax层如下图所示,传统机器学习方法以此类比,
一共有 K K K类,令网络的输出为 [ y ^ 1 , … , y ^ K ] [\hat{y}_1,\dots, \hat{y}_K] [y^1,…,y^K],对应每个类别的概率,令label为 [ y 1 , … , y K ] [y_1, \dots, y_K] [y1,…,yK]。对某个属于 p p p类的样本,其label中 y p = 1 y_p=1 yp=1, y 1 , … , y p − 1 , y p + 1 , … , y K y_1, \dots, y_{p-1}, y_{p+1}, \dots, y_K y1,…,yp−1,yp+1,…,yK均为0。
对这个样本,交叉熵(cross entropy)损失为
L = − ( y 1 log y ^ 1 + ⋯ + y K log y ^ K ) = − y p log y ^ p = − log y ^ p \begin{aligned}L &= - (y_1 \log \hat{y}_1 + \dots + y_K \log \hat{y}_K) \\&= -y_p \log \hat{y}_p \\ &= - \log \hat{y}_p\end{aligned} L=−(y1logy^1+⋯+yKlogy^K)=−yplogy^p=−logy^p
**均方误差损失(mean squared error,MSE)**为
L = ( y 1 − y ^ 1 ) 2 + ⋯ + ( y K − y ^ K ) 2 = ( 1 − y ^ p ) 2 + ( y ^ 1 2 + ⋯ + y ^ p − 1 2 + y ^ p + 1 2 + ⋯ + y ^ K 2 ) \begin{aligned}L &= (y_1 - \hat{y}_1)^2 + \dots + (y_K - \hat{y}_K)^2 \\&= (1 - \hat{y}_p)^2 + (\hat{y}_1^2 + \dots + \hat{y}_{p-1}^2 + \hat{y}_{p+1}^2 + \dots + \hat{y}_K^2)\end{aligned}