end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

ニューラルネットワークにおける信号伝達の行列による計算

GitHub - oreilly-japan/deep-learning-from-scratch: 『ゼロから作る Deep Learning』(O'Reilly Japan, 2016)

「ゼロから作るDeep Learning ① (Pythonで学ぶディープラーニングの理論と実装)」p.60 の写経です。

ニューラルネットワーク

1 x 1 x 2 a (1) 2 a (1) 1 a (1) 3 b (1) 1 w (1) 11 w (1) 12 y y 1 2 w (1) 12 第1層の重み 次層の2番目ニューロン 前層の1番目ニューロン z (1) 2 z (1) 1 z (1) 3 h () h () h () σ ()

信号伝達の行列式

 \large{ \textbf{A}^{(1)} = \textbf{XW}^{(1)} + \textbf{B}^{(1)} }

ただし、A、X、W、Bは以下の通り

 \large{ \textbf{A}^{(1)} =
\begin{pmatrix}
  a^{(1)}_1 & a^{(1)}_2 & a^{(1)}_3
\end{pmatrix}
} \large{ \textbf{X}^{(1)} =
\begin{pmatrix}
  x_1 & x_2
\end{pmatrix}
}
 \large{ \textbf{B}^{(1)} =
\begin{pmatrix}
  b^{(1)}_1 & b^{(1)}_2 & b^{(1)}_3
\end{pmatrix}
} \large{ \textbf{W}^{(1)} =
\begin{pmatrix}
  w^{(1)}_{11} & w^{(1)}_{21} & w^{(1)}_{31} \\\
  w^{(1)}_{12} & w^{(1)}_{22} & w^{(1)}_{32}
\end{pmatrix}
}

活性化関数

活性化関数(  \large{ h() } )には、様々ありますが、 sigmoid関数とReLU関数の式とグラフは、以下の通り

sigmoid関数

 \Large{  h() = \frac{1}{ 1 + e^{-x}}  }

<sodipodi:namedview id="namedview7" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:showpageshadow="2" inkscape:pageopacity="0.0" inkscape:pagecheckerboard="0" inkscape:deskcolor="#d1d1d1" inkscape:document-units="mm" showgrid="true" inkscape:zoom="2.8699528" inkscape:cx="91.987576" inkscape:cy="137.63293" inkscape:window-width="2560" inkscape:window-height="1351" inkscape:window-x="-9" inkscape:window-y="-9" inkscape:window-maximized="1" inkscape:current-layer="layer1"> <inkscape:grid type="xygrid" id="grid686" originx="0.66838312" originy="-0.44218888" /> </sodipodi:namedview> <inkscape:path-effect effect="bspline" id="path-effect2497" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> <inkscape:path-effect effect="bspline" id="path-effect2493" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> <inkscape:path-effect effect="bspline" id="path-effect1596" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> 0.5 1.0 O x h ()

ReLU関数 (ランプ関数)

 \large{  h() =
\begin{Bmatrix}
  x &amp; (x > 0) \\
  0 &amp; (x ≦ 0)
\end{Bmatrix}
}

<sodipodi:namedview id="namedview7" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:showpageshadow="2" inkscape:pageopacity="0.0" inkscape:pagecheckerboard="0" inkscape:deskcolor="#d1d1d1" inkscape:document-units="mm" showgrid="true" inkscape:zoom="2.0293631" inkscape:cx="20.696149" inkscape:cy="58.639088" inkscape:window-width="2560" inkscape:window-height="1351" inkscape:window-x="-9" inkscape:window-y="-9" inkscape:window-maximized="1" inkscape:current-layer="layer1"> <inkscape:grid type="xygrid" id="grid686" originx="0.41218199" originy="-0.44218864" /> </sodipodi:namedview> <inkscape:path-effect effect="bspline" id="path-effect2497" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> <inkscape:path-effect effect="bspline" id="path-effect2493" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> <inkscape:path-effect effect="bspline" id="path-effect1596" is_visible="true" lpeversion="1" weight="33.333333" steps="2" helper_size="0" apply_no_weight="true" apply_with_weight="true" only_selected="false" /> 1.0 O x h () 1.0

ソフトマックス関数

再度に、出力層に使用する softmac関数である  \large{ σ() } は、以下の通り

ソフトマックス関数の特徴

出力値である 各 \large{y_i}は、0~1をとり、 その合計が、1となることから、確率に利用できます。

基本形

 \Large{
y_k = \frac { exp(a_k) }{ \sum^n_{i=1} exp(a_i)}
}

オーバーフロー対応型

基本形では、桁あふれの問題があるようですのが、 これを対策すると、以下のようになります。

 \large{ C' }は任意定数ですが、一般的には最大値を指定するようです。

 \Large{
y_k = \frac { exp(a_k) }{ \sum^n_{i=1} exp(a_i)}
= \frac { C exp(a_k) }{ C \sum^n_{i=1} exp(a_i)} \\\
= \frac { C exp(a_k + log C) }{ C \sum^n_{i=1} exp(a_i + logC )}
= \frac { C exp(a_k + C') }{ C \sum^n_{i=1} exp(a_i + C' )}
}