Closed6

計算グラフ: 行列同士の積

nabeyangnabeyang

計算グラフは組み合わせが複雑になっても使えるルールを最初に作る必要があります。分割不可能な計算グラフのルールは普通に連鎖律を使って計算したものを使います。

nabeyangnabeyang

行列同士の積

\bm{y} = \bm{x} \bm{W}

x.shape(N, D)W.shape(D, H)とする。

nabeyangnabeyang

行列の成分表記について

\bm{a} = \begin{pmatrix} a_{0, 0} & a_{0, 1} & a_{0, 2} \\ a_{1, 0} & a_{1, 1} & a_{1, 2} \end{pmatrix}

pythonで書くとこんな感じ

a = np.array([
  [ a[0,0], a[0,1], a[0,2]],
  [ a[1,0], a[1,1], a[1,2]],
])

行列同士の積の定義は成分表記で次の通り

(\bm{a}\bm{b})_{i, j} = \sum_{k} a_{i,k} b_{k, j}

\bm{y}の成分表記は次のようになる

y_{i,j} = \sum_{k} x_{i,k} W_{k,j}
nabeyangnabeyang
\begin{aligned} \frac{\partial y_{k,l}}{\partial x_{i, j}} &= \sum_{m} \frac{\partial x_{k,m}}{\partial x_{i, j}} W_{m,l}\\ &= \sum_{m} \delta_{i,k} \delta_{m, j} W_{m, j}\\ &= \delta_{i, k} W_{m, l} \end{aligned}
\begin{aligned} \left(\frac{\partial L}{\partial \bm{x}} \right)_{i,j} &= \frac{\partial L}{\partial x_{i, j}} \\ &= \sum_{k,l} \frac{\partial y_{k,l}}{\partial x_{i, j}} \frac{\partial L}{\partial y_{k,l}} \\ &= \sum_{k, l} \left(\delta_{i, k} W_{m, l} \right) \frac{\partial L}{\partial y_{k,l}}\\ &= \sum_{l} W_{j, l} \frac{\partial L}{\partial y_{i,l}}\\ &= \sum_{l} \left(\frac{\partial L}{\partial \bm{y}}\right)_{k, l}\left(W^{T}\right)_{l, j}\\ &= \left( \frac{\partial L}{\partial \bm{y}} W^{T}\right)_{i,j} \end{aligned}
nabeyangnabeyang
\begin{aligned} \frac{\partial y_{k,l}}{\partial W_{i, j}} &= \sum_{m} x_{k,m}\frac{\partial W_{m,l}}{\partial W_{i, j}}\\ &= \sum_{m} x_{k,m}\delta_{i,m} \delta_{j,l}\\ &= \delta_{j, l} x_{k,i} \end{aligned}
\begin{aligned} \left(\frac{\partial L}{\partial \bm{W}} \right)_{i,j} &= \frac{\partial L}{\partial W_{i, j}} \\ &= \sum_{k,l} \frac{\partial y_{k,l}}{\partial W_{i, j}} \frac{\partial L}{\partial y_{k,l}} \\ &= \sum_{k, l} \left(\delta_{j, l} x_{k, i} \right) \frac{\partial L}{\partial y_{k,l}}\\ &= \sum_{k} x_{k, i} \frac{\partial L}{\partial y_{k,j}}\\ &= \sum_{k} \left(x^{T}\right)_{i, k}\left(\frac{\partial L}{\partial \bm{W}}\right)_{k, j}\\ &= \left(x^{T} \frac{\partial L}{\partial \bm{y}} \right)_{i,j} \end{aligned}
nabeyangnabeyang

今回の計算で、行列の積に関する計算グラフのルールが得られました。

このスクラップは2021/03/09にクローズされました