プリンキピア

第9章リカレントニューラルネットワーク

9.7.2. Backpropagation Through Time in Detail9.7.2 時間をさかのぼる逆伝播の詳細

After discussing the general principle, let’s discuss backpropagation through time in detail. In contrast to the analysis in Section 9.7.1, in the following we will show how to compute the gradients of the objective function with respect to all the decomposed model parameters. To keep things simple, we consider an RNN without bias parameters, whose activation function in the hidden layer uses the identity mapping (ϕ(x)=x). For time step t, let the single example input and the target be 𝐱t∈ℝd and yt, respectively. The hidden state 𝐡t∈ℝh and the output 𝐨t∈ℝq are computed as
英語のヒント
一般的な原理を論じたので、時間をさかのぼる逆伝播を詳しく論じよう。9.7.1節の分析とは対照的に、以下では、分解したすべてのモデルパラメータについての目的関数の勾配を計算する方法を示す。簡単にするため、バイアスパラメータのないRNNで、隠れ層の活性化関数が恒等写像(ϕ(x)=x)を使うものを考える。時間ステップtについて、一つの事例の入力と目標をそれぞれ𝐱t∈ℝdとytとする。隠れ状態𝐡t∈ℝhと出力𝐨t∈ℝqは次のように計算される。
𝐡t=𝐖hx𝐱t+𝐖hh𝐡t−1,𝐨t=𝐖qh𝐡t,(9.7.9)
where 𝐖hx∈ℝh×d, 𝐖hh∈ℝh×h, and 𝐖qh∈ℝq×h are the weight parameters. Denote by l(𝐨t,yt) the loss at time step t. Our objective function, the loss over T time steps from the beginning of the sequence is thus
英語のヒント
ここで、𝐖hx∈ℝh×d、𝐖hh∈ℝh×h、𝐖qh∈ℝq×hは重みパラメータである。時間ステップtの損失をl(𝐨t,yt)と表す。したがって、目的関数、つまり系列の初めからT時間ステップにわたる損失は次のようになる。
L=1T∑t=1Tl(𝐨t,yt).(9.7.10)
In order to visualize the dependencies among model variables and parameters during computation of the RNN, we can draw a computational graph for the model, as shown in Fig. 9.7.2. For example, the computation of the hidden states of time step 3, 𝐡3, depends on the model parameters 𝐖hx and 𝐖hh, the hidden state of the previous time step 𝐡2, and the input of the current time step 𝐱3.
英語のヒント
RNNの計算中のモデルの変数とパラメータのあいだの依存関係を可視化するため、図9.7.2に示すように、モデルの計算グラフを描ける。たとえば、時間ステップ3の隠れ状態𝐡3の計算は、モデルパラメータ𝐖hxと𝐖hh、前の時間ステップの隠れ状態𝐡2、現在の時間ステップの入力𝐱3に依存する。
Fig. 9.7.2図9.7.2
Computational graph showing dependencies for an RNN model with three time steps. Boxes represent variables (not shaded) or parameters (shaded) and circles represent operators.
英語のヒント
三つの時間ステップをもつRNNのモデルの依存関係を示す計算グラフ。四角は変数(網かけなし)またはパラメータ(網かけあり)を表し、丸は演算子を表す。
As just mentioned, the model parameters in Fig. 9.7.2 are 𝐖hx, 𝐖hh, and 𝐖qh. Generally, training this model requires gradient computation with respect to these parameters ∂L/∂𝐖hx, ∂L/∂𝐖hh, and ∂L/∂𝐖qh. According to the dependencies in Fig. 9.7.2, we can traverse in the opposite direction of the arrows to calculate and store the gradients in turn. To flexibly express the multiplication of matrices, vectors, and scalars of different shapes in the chain rule, we continue to use the prod operator as described in Section 5.3.
英語のヒント
今述べたように、図9.7.2のモデルパラメータは𝐖hx、𝐖hh、𝐖qhである。一般に、このモデルを訓練するには、これらのパラメータについての勾配∂L/∂𝐖hx、∂L/∂𝐖hh、∂L/∂𝐖qhを計算する必要がある。図9.7.2の依存関係に従って、矢印と逆の方向にたどり、勾配を順に計算して保存できる。連鎖律の中で、形状の異なる行列、ベクトル、スカラーの掛け算を柔軟に表すため、5.3節で説明したように、引き続きprod演算子を使う。
First of all, differentiating the objective function with respect to the model output at any time step t is fairly straightforward:
英語のヒント
まず、任意の時間ステップtでのモデルの出力について目的関数を微分するのは、かなり素直である。
∂L∂𝐨t=∂l(𝐨t,yt)T⋅∂𝐨t∈ℝq.(9.7.11)
Now we can calculate the gradient of the objective with respect to the parameter 𝐖qh in the output layer: ∂L/∂𝐖qh∈ℝq×h. Based on Fig. 9.7.2, the objective L depends on 𝐖qh via 𝐨1,…,𝐨T. Using the chain rule yields
英語のヒント
これで、出力層のパラメータ𝐖qhについての目的関数の勾配∂L/∂𝐖qh∈ℝq×hを計算できる。図9.7.2にもとづけば、目的関数Lは𝐨1,…,𝐨Tを通じて𝐖qhに依存する。連鎖律を使うと次が得られる。
∂L∂𝐖qh=∑t=1Tprod(∂L∂𝐨t,∂𝐨t∂𝐖qh)=∑t=1T∂L∂𝐨t𝐡t⊤,(9.7.12)
where ∂L/∂𝐨t is given by (9.7.11).
英語のヒント
ここで、∂L/∂𝐨tは(9.7.11)で与えられる。
Next, as shown in Fig. 9.7.2, at the final time step T, the objective function L depends on the hidden state 𝐡T only via 𝐨T. Therefore, we can easily find the gradient ∂L/∂𝐡T∈ℝh using the chain rule:
英語のヒント
次に、図9.7.2に示すように、最後の時間ステップTでは、目的関数Lは𝐨Tを通じてのみ隠れ状態𝐡Tに依存する。したがって、連鎖律を使って勾配∂L/∂𝐡T∈ℝhを簡単に求められる。
∂L∂𝐡T=prod(∂L∂𝐨T,∂𝐨T∂𝐡T)=𝐖qh⊤∂L∂𝐨T.(9.7.13)
It gets trickier for any time step t<T, where the objective function L depends on 𝐡t via 𝐡t+1 and 𝐨t. According to the chain rule, the gradient of the hidden state ∂L/∂𝐡t∈ℝh at any time step t<T can be recurrently computed as:
英語のヒント
任意の時間ステップt<Tではもっと厄介になる。そこでは目的関数Lは𝐡t+1と𝐨tを通じて𝐡tに依存する。連鎖律により、任意の時間ステップt<Tでの隠れ状態の勾配∂L/∂𝐡t∈ℝhは、再帰的に次のように計算できる。
∂L∂𝐡t=prod(∂L∂𝐡t+1,∂𝐡t+1∂𝐡t)+prod(∂L∂𝐨t,∂𝐨t∂𝐡t)=𝐖hh⊤∂L∂𝐡t+1+𝐖qh⊤∂L∂𝐨t.(9.7.14)
For analysis, expanding the recurrent computation for any time step 1≤t≤T gives
英語のヒント
分析のため、任意の時間ステップ1≤t≤Tについて再帰的な計算を展開すると、次が得られる。
∂L∂𝐡t=∑i=tT(𝐖hh⊤)T−i𝐖qh⊤∂L∂𝐨T+t−i.(9.7.15)
We can see from (9.7.15) that this simple linear example already exhibits some key problems of long sequence models: it involves potentially very large powers of 𝐖hh⊤. In it, eigenvalues smaller than 1 vanish and eigenvalues larger than 1 diverge. This is numerically unstable, which manifests itself in the form of vanishing and exploding gradients. One way to address this is to truncate the time steps at a computationally convenient size as discussed in Section 9.7.1. In practice, this truncation can also be effected by detaching the gradient after a given number of time steps. Later on, we will see how more sophisticated sequence models such as long short-term memory can alleviate this further.
英語のヒント
(9.7.15)から、この単純な線形の例でさえ、長い系列のモデルの鍵となる問題のいくつかをすでに示していることがわかる。それは、𝐖hh⊤のとても大きなべき乗を含みうる。その中で、1より小さい固有値は消え、1より大きい固有値は発散する。これは数値的に不安定であり、勾配の消失と爆発という形で現れる。これに対処する一つの方法は、9.7.1節で論じたように、計算上都合のよい大きさで時間ステップを打ち切ることである。実際には、この打ち切りは、決まった数の時間ステップの後で勾配を切り離すことでも実現できる。後で、長短期記憶のようなもっと洗練された系列モデルが、これをさらにどう和らげられるかを見る。
Finally, Fig. 9.7.2 shows that the objective function L depends on model parameters 𝐖hx and 𝐖hh in the hidden layer via hidden states 𝐡1,…,𝐡T. To compute gradients with respect to such parameters ∂L/∂𝐖hx∈ℝh×d and ∂L/∂𝐖hh∈ℝh×h, we apply the chain rule giving
英語のヒント
最後に、図9.7.2は、目的関数Lが隠れ状態𝐡1,…,𝐡Tを通じて隠れ層のモデルパラメータ𝐖hxと𝐖hhに依存することを示している。こうしたパラメータについての勾配∂L/∂𝐖hx∈ℝh×dと∂L/∂𝐖hh∈ℝh×hを計算するため、連鎖律を当てはめると次が得られる。
∂L∂𝐖hx=∑t=1Tprod(∂L∂𝐡t,∂𝐡t∂𝐖hx)=∑t=1T∂L∂𝐡t𝐱t⊤,∂L∂𝐖hh=∑t=1Tprod(∂L∂𝐡t,∂𝐡t∂𝐖hh)=∑t=1T∂L∂𝐡t𝐡t−1⊤,(9.7.16)
where ∂L/∂𝐡t which is recurrently computed by (9.7.13) and (9.7.14) is the key quantity that affects the numerical stability.
英語のヒント
ここで、(9.7.13)と(9.7.14)によって再帰的に計算される∂L/∂𝐡tは、数値的な安定性に影響する鍵となる量である。
Since backpropagation through time is the application of backpropagation in RNNs, as we have explained in Section 5.3, training RNNs alternates forward propagation with backpropagation through time. Moreover, backpropagation through time computes and stores the above gradients in turn. Specifically, stored intermediate values are reused to avoid duplicate calculations, such as storing ∂L/∂𝐡t to be used in computation of both ∂L/∂𝐖hx and ∂L/∂𝐖hh.
英語のヒント
時間をさかのぼる逆伝播はRNNにおける逆伝播の応用なので、5.3節で説明したように、RNNの訓練では順伝播と時間をさかのぼる逆伝播を交互に行う。さらに、時間をさかのぼる逆伝播は、上の勾配を順に計算して保存する。具体的には、保存した中間の値を再利用して重複する計算を避ける。たとえば、∂L/∂𝐡tを保存しておき、∂L/∂𝐖hxと∂L/∂𝐖hhの両方の計算に使う。