corssEntropy(softmax(\theta | X,

2021-02-05  本文已影响0人  东东是个鬼

限于本人水平,如有谬误敬请指出。

交叉熵合并 softmax 函数:

\begin{align} \\ &\mathrm{F}(\theta)&&=&&crossEntropy(softmax(\theta\;|\; \vec{\mathrm{x}}, \vec{f_c(y)})) \\ \\ &&&=&& \vec{f_{c}(y)}^{T} \cdot log \left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) \end{align}

\theta 是一个 m\times c 的矩阵,其中 m 是特征的数量, c 是分类的数量。

假设 \mathrm{\vec{x}}m\times 1 的列向量。

\cdot 运算和 numpy 的广播机制一致。

\vec{f_{c}(y)} 函数会建立一个长度为 m\times 1 的全零向量,并将第 y 个索引位置的元素置一。(m 和 y 均从 0 开始计数,\vec{f_c(y)}^{T} 将会建立一个与softmax(\mathrm{\vec{x}}^{T}\theta) 输出1\times m相同形式的向量)

\vec{\hat{y}} 是一个形状与 \vec{f_{c}(y)} 形状相同的向量。

这里所有的向量初始状态默认为列向量。

注意:注意矩阵运算中的结果的形状应该以 \theta 的形状为准,因为求的梯度为 \theta 的梯度。

\mathrm{F}`(\theta) 求解:

部分求导

part 0 求导
\begin{align} \frac { \partial\left( f_{c}(y)^{T} \cdot log \left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) \right) } {\partial(\frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)}} &&=&& \vec{f_{c}(y)}^{T} \cdot \frac {1} {\frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)}} \\ \\ &&=&& \vec{f_{c}(y)}^{T} \cdot \frac {\sum exp(\vec{\mathrm{x}}^T\theta)} {exp(\vec{\mathrm{x}}^T\theta)} \end{align}

part 1 求导

\begin{align} \frac { \partial\left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) } {\partial(exp(\vec{\mathrm{x}}^T\theta)} &&=&& \frac {{\sum exp(\vec{\mathrm{x}}^T\theta) - exp(\vec{\mathrm{x}}^T\theta)}} {(\sum exp(\vec{\mathrm{x}}^T\theta))^{2}} \end{align}

part 2 求导

\begin{align} \frac {\partial(exp(\vec{\mathrm{x}}^T\theta))} {\partial(\theta)} &&=&& \mathrm{\vec{x}}\cdot exp(\mathrm{\vec{x}^{T}\theta}) \end{align}

合并部分导数

依据链式求导法则,合并 part 0, part 1, part 2:(a 为左边,b 为右边)

\begin{align} \frac {\partial(\mathrm{F}(\theta))} {\partial(\theta)}_{shape=m\times c} &&=&& \frac { \partial\left( \vec{f_{c}(y)}^{T} \cdot log \left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) \right) } {\partial(\frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)}} \cdot \frac { \partial\left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) } {\partial(exp(\vec{\mathrm{x}}^T\theta)} \cdot \frac {\partial(exp(\vec{\mathrm{x}}^T\theta))} {\partial(\theta)} &&(a) \\\\ &&=&& \vec{f_{c}(y)}^{T} \cdot \frac {\sum exp(\vec{\mathrm{x}}^T\theta)} {exp(\vec{\mathrm{x}}^T\theta)} \cdot \frac {{\sum exp(\vec{\mathrm{x}}^T\theta) - exp(\vec{\mathrm{x}}^T\theta)}} {(\sum exp(\vec{\mathrm{x}}^T\theta))^{2}} \cdot \mathrm{\vec{x}}\cdot exp(\mathrm{\vec{x}^{T}\theta}) &&(b) \end{align}

分别合并 (a), (b):

\begin{align} \frac {\partial(\mathrm{F}(\theta))} {\partial(\theta)}_{shape=m\times c} &&=&& \frac { \partial\left( \vec{f_{c}(y)}^{T} \cdot log \left( \frac {exp(\vec{\mathrm{x}}^T\theta)} {\sum exp(\vec{\mathrm{x}}^T\theta)} \right) \right) } {\partial(\theta)} &&(a) \\\\ && = && \mathrm{\vec{x}}\cdot \vec{f_{c}(y)}^{T} \cdot \frac {{\sum exp(\vec{\mathrm{x}}^T\theta) - exp(\vec{\mathrm{x}}^T\theta)}} {\sum exp(\vec{\mathrm{x}}^T\theta)}&&(b)\\\\ &&=&&\mathrm{\vec{x}}\cdot \vec{f_{c}(y)}^{T}\cdot (1 - softmax(\mathrm{\vec{x}}^{T}\theta))&&(b) \\ \\ &&=&&\mathrm{\vec{x}}\cdot \vec{f_{c}(y)}^{T}\cdot (1 - \vec{\hat{y}}^{T})&&(b) \end{align}

所有公式和内容均为本人手打,创作权归本人所有,禁止转载。

上一篇 下一篇

猜你喜欢

热点阅读