神经网络学习笔记 - 损失函数的定义和微分证明
神经网络学习笔记 - 损失函数的定义和微分证明
损失函数 Loss function (cross entropy loss)
损失函数,反向传播和梯度计算构成了循环神经网络的训练过程。
激活函数softmax和损失函数会一起使用。
激活函数会根据输入的参数(一个矢量,表示每个分类的可能性),计算每个分类的概率(0, 1)。
损失函数根据softmax的计算结果\(\hat{y}\)和期望结果\(y\),根据交叉熵方法(cross entropy loss) 可得到损失\(L\)。
cross entropy loss函数
\[L_t(y_t, \hat{y_t}) = - y_t \log \hat{y_t} \\
L(y, \hat{y}) = - \sum_{t} y_t \log \hat{y_t} \\
\frac{ \partial L_t } { \partial z_t } = \hat{y_t} - y_t \\
\text{where} \\
z_t = s_tV \\
\hat{y_t} = softmax(z_t) \\
y_t \text{ : for training data x, the expected result y at time t. which are from training data}
\]
证明
\[\begin{align}
\frac{ \partial L_t } { \partial z_t }
& = \frac{ \partial \left ( - \sum_{k} y_k \log \hat{y_k} \right ) } { \partial z_t } \\
& = - \sum_{k} y_k \frac{ \partial \log \hat{y_k} } { \partial z_t } \\
& = - \sum_{k} y_k \frac {1} {\hat{y_k}} \cdot \frac{ \partial \hat{y_k} } { \partial z_t } \\
& = - \left ( y_t \frac {1} {\hat{y_t}} \cdot \frac{ \partial \hat{y_t} } { \partial z_t } \right ) - \left ( \sum_{k \ne t} y_k \frac {1} {\hat{y_k}} \cdot \frac{ \partial \hat{y_k} } { \partial z_t } \right ) \\
& \because \text{softmax differentiation formula } \\
& = - \left ( y_t \frac {1} {\hat{y_t}} \cdot ( 1 - \hat{y_t} ) \hat{y_t} \right ) - \left ( \sum_{k \ne t} y_k \frac {1} {\hat{y_k}} \cdot (-\hat{y_t} \hat{y_k}) \right ) \\
& = - \left ( y_t \cdot ( 1 - \hat{y_t} ) \right ) - \left ( \sum_{k \ne t} y_k \cdot (-\hat{y_t}) \right ) \\
& = - y_t + y_t \hat{y_t} + \left ( \sum_{k \ne t} y_k \hat{y_t} \right ) \\
& = - y_t + \hat{y_t} \left ( \sum_{k} y_k \right ) \\
& \because \sum_{k} y_k = 1 \\
& = \hat{y_t} - y_t
\end{align}
\]
参照
非常感谢阅读!如有不足之处,请留下您的评价和问题。
请“推荐”本文!
请“推荐”本文!