プリンキピア

第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 ht as the hidden state, xt as input, and ot as output at time step t. 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 wh and wo 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がどう働くかを単純にしたモデルから始める。このモデルは、隠れ状態の具体的な中身や、それがどう更新されるかについての細部を無視する。ここでの数学的な表記は、スカラー、ベクトル、行列をはっきり区別しない。ここではただ直観を養おうとしているだけである。この単純にしたモデルでは、時間ステップtの隠れ状態をht、入力をxt、出力をotと表す。9.4.2節の議論で、入力と隠れ状態は、隠れ層の一つの重み変数を掛ける前に連結できたことを思い出そう。そこで、whとwoを使って、それぞれ隠れ層と出力層の重みを表す。その結果、各時間ステップの隠れ状態と出力は次のようになる。
ht=f(xt,ht−1,wh),ot=g(ht,wo),(9.7.1)
where f and g are transformations of the hidden layer and the output layer, respectively. Hence, we have a chain of values {…,(xt−1,ht−1,ot−1),(xt,ht,ot),…} that depend on each other via recurrent computation. The forward propagation is fairly straightforward. All we need is to loop through the (xt,ht,ot) triples one time step at a time. The discrepancy between output ot and the desired target yt is then evaluated by an objective function across all the T time steps as
英語のヒント
ここで、fとgは、それぞれ隠れ層と出力層の変換である。したがって、再帰的な計算によって互いに依存する値の連鎖{…,(xt−1,ht−1,ot−1),(xt,ht,ot),…}がある。順伝播はかなり素直である。必要なのは、(xt,ht,ot)の三つ組を一度に1時間ステップずつループでたどることだけである。出力otと望みの目標ytのずれは、すべてのT時間ステップにわたって、目的関数で次のように評価される。
L(x1,…,xT,y1,…,yT,wh,wo)=1T∑t=1Tl(yt,ot).(9.7.2)
For backpropagation, matters are a bit trickier, especially when we compute the gradients with regard to the parameters wh of the objective function L. To be specific, by the chain rule,
英語のヒント
逆伝播では、事情は少し厄介になる。特に、目的関数Lのパラメータwhについての勾配を計算するときがそうである。具体的には、連鎖律により次が成り立つ。
∂L∂wh=1T∑t=1T∂l(yt,ot)∂wh=1T∑t=1T∂l(yt,ot)∂ot∂g(ht,wo)∂ht∂ht∂wh.(9.7.3)
The first and the second factors of the product in (9.7.3) are easy to compute. The third factor ∂ht/∂wh is where things get tricky, since we need to recurrently compute the effect of the parameter wh on ht. According to the recurrent computation in (9.7.1), ht depends on both ht−1 and wh, where computation of ht−1 also depends on wh. Thus, evaluating the total derivate of ht with respect to wh using the chain rule yields
英語のヒント
(9.7.3)の積の1番目と2番目の因子は計算が簡単である。3番目の因子∂ht/∂whが厄介なところである。パラメータwhがhtに与える影響を再帰的に計算する必要があるからである。(9.7.1)の再帰的な計算によれば、htはht−1とwhの両方に依存し、ht−1の計算もまたwhに依存する。したがって、連鎖律を使ってwhについてのhtの全微分を評価すると、次のようになる。
∂ht∂wh=∂f(xt,ht−1,wh)∂wh+∂f(xt,ht−1,wh)∂ht−1∂ht−1∂wh.(9.7.4)
To derive the above gradient, assume that we have three sequences {at},{bt},{ct} satisfying a0=0 and at=bt+ctat−1 for t=1,2,…. Then for t≥1, it is easy to show
英語のヒント
上の勾配を導くため、t=1,2,…についてa0=0とat=bt+ctat−1を満たす三つの系列{at},{bt},{ct}があるとする。すると、t≥1について次を示すのは簡単である。
at=bt+∑i=1t−1(∏j=i+1tcj)bi.(9.7.5)
By substituting at, bt, and ct according to
英語のヒント
at、bt、ctを次のように置き換える。
at=∂ht∂wh,bt=∂f(xt,ht−1,wh)∂wh,ct=∂f(xt,ht−1,wh)∂ht−1,(9.7.6)
the gradient computation in (9.7.4) satisfies at=bt+ctat−1. Thus, per (9.7.5), we can remove the recurrent computation in (9.7.4) with
英語のヒント
すると、(9.7.4)の勾配の計算はat=bt+ctat−1を満たす。したがって、(9.7.5)により、(9.7.4)の再帰的な計算を次のように取り除ける。
∂ht∂wh=∂f(xt,ht−1,wh)∂wh+∑i=1t−1(∏j=i+1t∂f(xj,hj−1,wh)∂hj−1)∂f(xi,hi−1,wh)∂wh.(9.7.7)
While we can use the chain rule to compute ∂ht/∂wh recursively, this chain can get very long whenever t is large. Let’s discuss a number of strategies for dealing with this problem.
英語のヒント
連鎖律を使って∂ht/∂whを再帰的に計算することはできるが、tが大きいときにはいつも、この連鎖はとても長くなりうる。この問題に対処するための戦略をいくつか論じよう。

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 ∂ht−τ/∂wh. 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)の和をτステップで打ち切ることもできる。これは、ここまで論じてきたことである。これにより、和を∂ht−τ/∂whで終わらせるだけで、本当の勾配の近似が得られる。実際には、これはかなりうまく働く。これは、ふつう打ち切り型の時間をさかのぼる逆伝播と呼ばれるものである(Jaeger, 2002)。この帰結の一つは、モデルが長期的な帰結よりも主に短期的な影響に焦点を当てることである。これは実は望ましい。推定を、もっと単純で安定したモデルの方へ偏らせるからである。

9.7.1.3. Randomized Truncation9.7.1.3 ランダムな打ち切り

Last, we can replace ∂ht/∂wh by a random variable which is correct in expectation but truncates the sequence. This is achieved by using a sequence of ξt with predefined 0≤πt≤1, where P(ξt=0)=1−πt and P(ξt=πt−1)=πt, thus E[ξt]=1. We use this to replace the gradient ∂ht/∂wh in (9.7.4) with
英語のヒント
最後に、∂ht/∂whを、期待値では正しいが系列を打ち切る確率変数で置き換えることもできる。これは、あらかじめ決めた0≤πt≤1をもつξtの系列を使うことで実現される。ここで、P(ξt=0)=1−πtかつP(ξt=πt−1)=πtであり、したがってE[ξt]=1である。これを使って、(9.7.4)の勾配∂ht/∂whを次で置き換える。
zt=∂f(xt,ht−1,wh)∂wh+ξt∂f(xt,ht−1,wh)∂ht−1∂ht−1∂wh.(9.7.8)
It follows from the definition of ξt that E[zt]=∂ht/∂wh. Whenever ξt=0 the recurrent computation terminates at that time step t. 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).
英語のヒント
ξtの定義から、E[zt]=∂ht/∂whが成り立つ。ξt=0のときはいつも、再帰的な計算はその時間ステップtで終わる。これにより、長さの異なる系列の重みつきの和が得られる。そこでは、長い系列はまれだが、それに見合って大きく重みづけされる。この考えは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.
英語のヒント
残念ながら、理論的には魅力的だが、ランダムな打ち切りは通常の打ち切りよりそれほどうまく働かない。これはおそらくいくつかの要因による。第一に、過去へ何ステップか逆伝播した後の観測の影響は、実際には依存関係をとらえるのに十分である。第二に、分散が増えることが、ステップが多いほど勾配がより正確になるという事実を打ち消してしまう。第三に、実際に欲しいのは、相互作用の範囲が短いだけのモデルである。そのため、通常の打ち切り型の時間をさかのぼる逆伝播には、わずかな正則化の効果があり、それは望ましいものでありうる。