第9章リカレントニューラルネットワーク
9.7.1. Analysis of Gradients in RNNs9.7.1 RNNにおける勾配の分析
We start with a simplified model of how an RNN works. This model ignores details about the specifics of the hidden state and how it is updated. The mathematical notation here does not explicitly distinguish scalars, vectors, and matrices. We are just trying to develop some intuition. In this simplified model, we denote as the hidden state, as input, and as output at time step . Recall our discussions in Section 9.4.2 that the input and the hidden state can be concatenated before being multiplied by one weight variable in the hidden layer. Thus, we use and to indicate the weights of the hidden layer and the output layer, respectively. As a result, the hidden states and outputs at each time step are
英語のヒント
RNNがどう働くかを単純にしたモデルから始める。このモデルは、隠れ状態の具体的な中身や、それがどう更新されるかについての細部を無視する。ここでの数学的な表記は、スカラー、ベクトル、行列をはっきり区別しない。ここではただ直観を養おうとしているだけである。この単純にしたモデルでは、時間ステップの隠れ状態を、入力を、出力をと表す。9.4.2節の議論で、入力と隠れ状態は、隠れ層の一つの重み変数を掛ける前に連結できたことを思い出そう。そこで、とを使って、それぞれ隠れ層と出力層の重みを表す。その結果、各時間ステップの隠れ状態と出力は次のようになる。
(9.7.1)
where and are transformations of the hidden layer and the output layer, respectively. Hence, we have a chain of values that depend on each other via recurrent computation. The forward propagation is fairly straightforward. All we need is to loop through the triples one time step at a time. The discrepancy between output and the desired target is then evaluated by an objective function across all the time steps as
英語のヒント
ここで、とは、それぞれ隠れ層と出力層の変換である。したがって、再帰的な計算によって互いに依存する値の連鎖がある。順伝播はかなり素直である。必要なのは、の三つ組を一度に1時間ステップずつループでたどることだけである。出力と望みの目標のずれは、すべての時間ステップにわたって、目的関数で次のように評価される。
(9.7.2)
For backpropagation, matters are a bit trickier, especially when we compute the gradients with regard to the parameters of the objective function . To be specific, by the chain rule,
英語のヒント
逆伝播では、事情は少し厄介になる。特に、目的関数のパラメータについての勾配を計算するときがそうである。具体的には、連鎖律により次が成り立つ。
(9.7.3)
The first and the second factors of the product in (9.7.3) are easy to compute. The third factor is where things get tricky, since we need to recurrently compute the effect of the parameter on . According to the recurrent computation in (9.7.1), depends on both and , where computation of also depends on . Thus, evaluating the total derivate of with respect to using the chain rule yields
英語のヒント
(9.7.4)
To derive the above gradient, assume that we have three sequences satisfying and for . Then for , it is easy to show
英語のヒント
上の勾配を導くため、についてとを満たす三つの系列があるとする。すると、について次を示すのは簡単である。
(9.7.5)
By substituting , , and according to
英語のヒント
、、を次のように置き換える。
(9.7.6)
the gradient computation in (9.7.4) satisfies . Thus, per (9.7.5), we can remove the recurrent computation in (9.7.4) with
英語のヒント
(9.7.7)
While we can use the chain rule to compute recursively, this chain can get very long whenever is large. Let’s discuss a number of strategies for dealing with this problem.
英語のヒント
連鎖律を使ってを再帰的に計算することはできるが、が大きいときにはいつも、この連鎖はとても長くなりうる。この問題に対処するための戦略をいくつか論じよう。
9.7.1.1. Full Computation9.7.1.1 完全な計算
One idea might be to compute the full sum in (9.7.7). However, this is very slow and gradients can blow up, since subtle changes in the initial conditions can potentially affect the outcome a lot. That is, we could see things similar to the butterfly effect, where minimal changes in the initial conditions lead to disproportionate changes in the outcome. This is generally undesirable. After all, we are looking for robust estimators that generalize well. Hence this strategy is almost never used in practice.
英語のヒント
一つの考えは、(9.7.7)の和を完全に計算することかもしれない。しかし、これはとても遅く、初期条件のわずかな変化が結果に大きく影響しうるので、勾配が爆発しうる。つまり、初期条件のごく小さな変化が結果に不釣り合いに大きな変化をもたらす、バタフライ効果に似たことが見られるかもしれない。これは一般に望ましくない。結局のところ、私たちが求めているのはよく汎化する頑健な推定器である。そのため、この戦略は実際にはほとんど使われない。
9.7.1.2. Truncating Time Steps9.7.1.2 時間ステップを打ち切る
Alternatively, we can truncate the sum in (9.7.7) after steps. This is what we have been discussing so far. This leads to an approximation of the true gradient, simply by terminating the sum at . In practice this works quite well. It is what is commonly referred to as truncated backpropgation through time (Jaeger, 2002). One of the consequences of this is that the model focuses primarily on short-term influence rather than long-term consequences. This is actually desirable, since it biases the estimate towards simpler and more stable models.
英語のヒント
あるいは、(9.7.7)の和をステップで打ち切ることもできる。これは、ここまで論じてきたことである。これにより、和をで終わらせるだけで、本当の勾配の近似が得られる。実際には、これはかなりうまく働く。これは、ふつう打ち切り型の時間をさかのぼる逆伝播と呼ばれるものである(Jaeger, 2002)。この帰結の一つは、モデルが長期的な帰結よりも主に短期的な影響に焦点を当てることである。これは実は望ましい。推定を、もっと単純で安定したモデルの方へ偏らせるからである。
9.7.1.3. Randomized Truncation9.7.1.3 ランダムな打ち切り
Last, we can replace by a random variable which is correct in expectation but truncates the sequence. This is achieved by using a sequence of with predefined , where and , thus . We use this to replace the gradient in (9.7.4) with
英語のヒント
最後に、を、期待値では正しいが系列を打ち切る確率変数で置き換えることもできる。これは、あらかじめ決めたをもつの系列を使うことで実現される。ここで、かつであり、したがってである。これを使って、(9.7.4)の勾配を次で置き換える。
(9.7.8)
It follows from the definition of that . Whenever the recurrent computation terminates at that time step . This leads to a weighted sum of sequences of varying lengths, where long sequences are rare but appropriately overweighted. This idea was proposed by Tallec and Ollivier (2017).
英語のヒント
の定義から、が成り立つ。のときはいつも、再帰的な計算はその時間ステップで終わる。これにより、長さの異なる系列の重みつきの和が得られる。そこでは、長い系列はまれだが、それに見合って大きく重みづけされる。この考えはTallec and Ollivier (2017)が提案した。
9.7.1.4. Comparing Strategies9.7.1.4 戦略を比べる
Fig. 9.7.1図9.7.1
Comparing strategies for computing gradients in RNNs. From top to bottom: randomized truncation, regular truncation, and full computation.
英語のヒント
RNNで勾配を計算する戦略の比較。上から下へ:ランダムな打ち切り、通常の打ち切り、完全な計算。
Fig. 9.7.1 illustrates the three strategies when analyzing the first few characters of The Time Machine using backpropagation through time for RNNs:
英語のヒント
図9.7.1は、RNNの時間をさかのぼる逆伝播を使って『タイム・マシン』の最初の数文字を分析するときの、三つの戦略を示している。
- The first row is the randomized truncation that partitions the text into segments of varying lengths.
英語のヒント
1行目は、テキストを長さの異なる区間に分割するランダムな打ち切りである。 - The second row is the regular truncation that breaks the text into subsequences of the same length. This is what we have been doing in RNN experiments.
英語のヒント
2行目は、テキストを同じ長さの部分系列に分ける通常の打ち切りである。これは、RNNの実験でこれまで行ってきたことである。 - The third row is the full backpropagation through time that leads to a computationally infeasible expression.
英語のヒント
3行目は、計算上実行できない式につながる、完全な時間をさかのぼる逆伝播である。
Unfortunately, while appealing in theory, randomized truncation does not work much better than regular truncation, most likely due to a number of factors. First, the effect of an observation after a number of backpropagation steps into the past is quite sufficient to capture dependencies in practice. Second, the increased variance counteracts the fact that the gradient is more accurate with more steps. Third, we actually want models that have only a short range of interactions. Hence, regularly truncated backpropagation through time has a slight regularizing effect that can be desirable.
英語のヒント
残念ながら、理論的には魅力的だが、ランダムな打ち切りは通常の打ち切りよりそれほどうまく働かない。これはおそらくいくつかの要因による。第一に、過去へ何ステップか逆伝播した後の観測の影響は、実際には依存関係をとらえるのに十分である。第二に、分散が増えることが、ステップが多いほど勾配がより正確になるという事実を打ち消してしまう。第三に、実際に欲しいのは、相互作用の範囲が短いだけのモデルである。そのため、通常の打ち切り型の時間をさかのぼる逆伝播には、わずかな正則化の効果があり、それは望ましいものでありうる。