10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Softmax Cross Entropyの誤差逆伝播法

Posted at

#Softmax Cross Entropyとは
出力層でよく使われる活性化関数Softmaxと損失関数Cross Entropyとの合成関数です。以下ではバッチ版で議論していきますが、バッチなしのときは$m = 1$を代入すればOKです。

#Softmax Cross Entropyの導関数の導出
活性化関数Softmaxの入力を$\boldsymbol{X}$、出力を$\boldsymbol{Y}$とします。教師データを$\boldsymbol{T}$とします。損失関数Cross Entropyの値を$L$とします。ここで、$\boldsymbol{X,Y,T}$は$m \times n$行列で、$L$はスカラーです。このときSoftmaxとCross Entropyの定義から

y_{ij} = \frac{\exp(x_{ij})}{\sum_{k=1}^n \exp(x_{ik})}\\

L = - \frac{1}{m} \sum_{i, j} t_{ij} \log y_{ij}

よって

L = - \frac{1}{m} \sum_{i, j} t_{ij} x_{ij}
 + \frac{1}{m} \sum_{i, j} t_{ij} \log(\sum_{k=1}^n \exp(x_{ik}))

$これをx_{pq}$で偏微分すると

\frac{\partial}{\partial x_{pq}}\left(
- \frac{1}{m} \sum_{i, j} t_{ij} x_{ij}
\right) = - \frac{1}{m} t_{pq}\\

\begin{align}
\frac{\partial}{\partial x_{pq}}\left(
\frac{1}{m} \sum_{i, j} t_{ij} \log(\sum_{k=1}^n \exp(x_{ik}))
\right) &= 
\frac{\partial}{\partial x_{pq}}\left(
\frac{1}{m}\sum_{j=1}^n t_{pj} \log(\sum_{k=1}^n \exp(x_{pk}))
\right) \\
&= \frac{\partial}{\partial x_{pq}}\left(
\frac{1}{m} \log(\sum_{k=1}^n \exp(x_{pk})) \sum_{j=1}^n t_{pj}
\right) \\
&= \frac{1}{m} \frac{\exp(x_{pq})}{\sum_{k=1}^n \exp(x_{pk})} \sum_{j=1}^n t_{pj} \\
&= \frac{1}{m} y_{pq} \sum_{j=1}^n t_{pj}
\end{align}

one-hot表現のときは$\sum_{j=1}^n t_{pj} = 1$であることを用いると

\frac{\partial L}{\partial x_{pq}} = \frac{1}{m} (y_{pq} - t_{pq})

したがって

\frac{\partial L}{\partial \boldsymbol{X}} = \frac{1}{m} (\boldsymbol{Y} - \boldsymbol{T})

ただし$\frac{\partial L}{\partial \boldsymbol{X}}$は分母レイアウト記法。

以上から教師データはone-hot表現である必要があることがわかります。

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?