プリンキピア

第5章多層パーセプトロン

5.3.3. Backpropagation5.3.3 逆伝播

Backpropagation refers to the method of calculating the gradient of neural network parameters. In short, the method traverses the network in reverse order, from the output to the input layer, according to the chain rule from calculus. The algorithm stores any intermediate variables (partial derivatives) required while calculating the gradient with respect to some parameters. Assume that we have functions 𝖸=f(𝖷) and 𝖹=g(𝖸), in which the input and the output 𝖷,𝖸,𝖹 are tensors of arbitrary shapes. By using the chain rule, we can compute the derivative of 𝖹 with respect to 𝖷 via
英語のヒント
逆伝播とは、ニューラルネットワークのパラメータの勾配を計算する方法をいう。要するに、この方法は、微積分の連鎖律に従って、出力層から入力層へと逆の順にネットワークをたどる。このアルゴリズムは、あるパラメータについての勾配を計算するあいだに必要な中間変数(偏導関数)をすべて保存する。関数𝖸=f(𝖷)と𝖹=g(𝖸)があり、その入力と出力𝖷,𝖸,𝖹は任意の形状のテンソルであるとしよう。連鎖律を使えば、𝖷についての𝖹の導関数を次のように計算できる。
∂𝖹∂𝖷=prod(∂𝖹∂𝖸,∂𝖸∂𝖷).(5.3.7)
Here we use the prod operator to multiply its arguments after the necessary operations, such as transposition and swapping input positions, have been carried out. For vectors, this is straightforward: it is simply matrix–matrix multiplication. For higher dimensional tensors, we use the appropriate counterpart. The operator prod hides all the notational overhead.
英語のヒント
ここでは、転置や入力の位置の入れ替えといった必要な操作を済ませた後で引数を掛け合わせるために、prod演算子を使う。ベクトルについては、これは素直である。単に行列と行列の掛け算である。もっと高次元のテンソルについては、適切な対応物を使う。演算子prodは、記法上の面倒をすべて覆い隠してくれる。
Recall that the parameters of the simple network with one hidden layer, whose computational graph is in Fig. 5.3.1, are 𝐖(1) and 𝐖(2). The objective of backpropagation is to calculate the gradients ∂J/∂𝐖(1) and ∂J/∂𝐖(2). To accomplish this, we apply the chain rule and calculate, in turn, the gradient of each intermediate variable and parameter. The order of calculations are reversed relative to those performed in forward propagation, since we need to start with the outcome of the computational graph and work our way towards the parameters. The first step is to calculate the gradients of the objective function J=L+s with respect to the loss term L and the regularization term s:
英語のヒント
計算グラフが図5.3.1にある、隠れ層が一つの単純なネットワークのパラメータは、𝐖(1)と𝐖(2)であることを思い出してほしい。逆伝播の目的は、勾配∂J/∂𝐖(1)と∂J/∂𝐖(2)を計算することである。これを達成するため、連鎖律を当てはめ、各中間変数とパラメータの勾配を順に計算する。計算の順序は、順伝播で行ったものとは逆になる。計算グラフの結果から始めて、パラメータのほうへ進んでいく必要があるからである。最初の一歩は、損失項Lと正則化項sについての、目的関数J=L+sの勾配を計算することである。
∂J∂L=1and∂J∂s=1.(5.3.8)
Next, we compute the gradient of the objective function with respect to variable of the output layer 𝐨 according to the chain rule:
英語のヒント
次に、連鎖律に従って、出力層の変数𝐨についての目的関数の勾配を計算する。
∂J∂𝐨=prod(∂J∂L,∂L∂𝐨)=∂L∂𝐨∈ℝq.(5.3.9)
Next, we calculate the gradients of the regularization term with respect to both parameters:
英語のヒント
次に、両方のパラメータについての正則化項の勾配を計算する。
∂s∂𝐖(1)=λ𝐖(1)and∂s∂𝐖(2)=λ𝐖(2).(5.3.10)
Now we are able to calculate the gradient ∂J/∂𝐖(2)∈ℝq×h of the model parameters closest to the output layer. Using the chain rule yields:
英語のヒント
これで、出力層に最も近いモデルのパラメータの勾配∂J/∂𝐖(2)∈ℝq×hを計算できる。連鎖律を使うと、次が得られる。
∂J∂𝐖(2)=prod(∂J∂𝐨,∂𝐨∂𝐖(2))+prod(∂J∂s,∂s∂𝐖(2))=∂J∂𝐨𝐡⊤+λ𝐖(2).(5.3.11)
To obtain the gradient with respect to 𝐖(1) we need to continue backpropagation along the output layer to the hidden layer. The gradient with respect to the hidden layer output ∂J/∂𝐡∈ℝh is given by
英語のヒント
𝐖(1)についての勾配を得るには、出力層に沿って隠れ層まで逆伝播を続ける必要がある。隠れ層の出力についての勾配∂J/∂𝐡∈ℝhは、次で与えられる。
∂J∂𝐡=prod(∂J∂𝐨,∂𝐨∂𝐡)=𝐖(2)⊤∂J∂𝐨.(5.3.12)
Since the activation function ϕ applies elementwise, calculating the gradient ∂J/∂𝐳∈ℝh of the intermediate variable 𝐳 requires that we use the elementwise multiplication operator, which we denote by ⊙:
英語のヒント
活性化関数ϕは成分ごとに適用されるので、中間変数𝐳の勾配∂J/∂𝐳∈ℝhを計算するには、成分ごとの掛け算の演算子を使う必要がある。これを⊙で表す。
∂J∂𝐳=prod(∂J∂𝐡,∂𝐡∂𝐳)=∂J∂𝐡⊙ϕ′(𝐳).(5.3.13)
Finally, we can obtain the gradient ∂J/∂𝐖(1)∈ℝh×d of the model parameters closest to the input layer. According to the chain rule, we get
英語のヒント
最後に、入力層に最も近いモデルのパラメータの勾配∂J/∂𝐖(1)∈ℝh×dを得られる。連鎖律に従うと、次が得られる。
∂J∂𝐖(1)=prod(∂J∂𝐳,∂𝐳∂𝐖(1))+prod(∂J∂s,∂s∂𝐖(1))=∂J∂𝐳𝐱⊤+λ𝐖(1).(5.3.14)