第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 (). For time step , let the single example input and the target be and , respectively. The hidden state and the output are computed as
英語のヒント
一般的な原理を論じたので、時間をさかのぼる逆伝播を詳しく論じよう。9.7.1節の分析とは対照的に、以下では、分解したすべてのモデルパラメータについての目的関数の勾配を計算する方法を示す。簡単にするため、バイアスパラメータのないRNNで、隠れ層の活性化関数が恒等写像()を使うものを考える。時間ステップについて、一つの事例の入力と目標をそれぞれととする。隠れ状態と出力は次のように計算される。
(9.7.9)
where , , and are the weight parameters. Denote by the loss at time step . Our objective function, the loss over time steps from the beginning of the sequence is thus
英語のヒント
ここで、、、は重みパラメータである。時間ステップの損失をと表す。したがって、目的関数、つまり系列の初めから時間ステップにわたる損失は次のようになる。
(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, , depends on the model parameters and , the hidden state of the previous time step , and the input of the current time step .
英語のヒント
RNNの計算中のモデルの変数とパラメータのあいだの依存関係を可視化するため、図9.7.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 , , and . Generally, training this model requires gradient computation with respect to these parameters , , and . 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 operator as described in Section 5.3.
英語のヒント
First of all, differentiating the objective function with respect to the model output at any time step is fairly straightforward:
英語のヒント
まず、任意の時間ステップでのモデルの出力について目的関数を微分するのは、かなり素直である。
(9.7.11)
Now we can calculate the gradient of the objective with respect to the parameter in the output layer: . Based on Fig. 9.7.2, the objective depends on via . Using the chain rule yields
英語のヒント
これで、出力層のパラメータについての目的関数の勾配を計算できる。図9.7.2にもとづけば、目的関数はを通じてに依存する。連鎖律を使うと次が得られる。
(9.7.12)
Next, as shown in Fig. 9.7.2, at the final time step , the objective function depends on the hidden state only via . Therefore, we can easily find the gradient using the chain rule:
英語のヒント
次に、図9.7.2に示すように、最後の時間ステップでは、目的関数はを通じてのみ隠れ状態に依存する。したがって、連鎖律を使って勾配を簡単に求められる。
(9.7.13)
It gets trickier for any time step , where the objective function depends on via and . According to the chain rule, the gradient of the hidden state at any time step can be recurrently computed as:
英語のヒント
任意の時間ステップではもっと厄介になる。そこでは目的関数はとを通じてに依存する。連鎖律により、任意の時間ステップでの隠れ状態の勾配は、再帰的に次のように計算できる。
(9.7.14)
For analysis, expanding the recurrent computation for any time step gives
英語のヒント
分析のため、任意の時間ステップについて再帰的な計算を展開すると、次が得られる。
(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 . 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.
英語のヒント
Finally, Fig. 9.7.2 shows that the objective function depends on model parameters and in the hidden layer via hidden states . To compute gradients with respect to such parameters and , we apply the chain rule giving
英語のヒント
最後に、図9.7.2は、目的関数が隠れ状態を通じて隠れ層のモデルパラメータとに依存することを示している。こうしたパラメータについての勾配とを計算するため、連鎖律を当てはめると次が得られる。
(9.7.16)
where which is recurrently computed by (9.7.13) and (9.7.14) is the key quantity that affects the numerical stability.
英語のヒント
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 to be used in computation of both and .
英語のヒント
時間をさかのぼる逆伝播はRNNにおける逆伝播の応用なので、5.3節で説明したように、RNNの訓練では順伝播と時間をさかのぼる逆伝播を交互に行う。さらに、時間をさかのぼる逆伝播は、上の勾配を順に計算して保存する。具体的には、保存した中間の値を再利用して重複する計算を避ける。たとえば、を保存しておき、との両方の計算に使う。