第9章リカレントニューラルネットワーク
9.5.3. Gradient Clipping9.5.3 勾配クリッピング
While you are already used to thinking of neural networks as “deep” in the sense that many layers separate the input and output even within a single time step, the length of the sequence introduces a new notion of depth. In addition to the passing through the network in the input-to-output direction, inputs at the first time step must pass through a chain of layers along the time steps in order to influence the output of the model at the final time step. Taking the backwards view, in each iteration, we backpropagate gradients through time, resulting in a chain of matrix-products of length . As mentioned in Section 5.4, this can result in numerical instability, causing the gradients either to explode or vanish, depending on the properties of the weight matrices.
英語のヒント
一つの時間ステップの中でさえ多くの層が入力と出力を隔てているという意味で、ニューラルネットワークを「深い」と考えることにはすでに慣れているだろうが、系列の長さは深さという新しい概念をもたらす。入力から出力への方向にネットワークを通ることに加えて、最初の時間ステップの入力は、最後の時間ステップでのモデルの出力に影響するためには、時間ステップに沿って層の連鎖を通らなければならない。逆向きの見方をとれば、各反復で、時間をさかのぼって勾配を逆伝播し、長さの行列の積の連鎖が生じる。5.4節で述べたように、これは数値的な不安定さを招き、重み行列の性質によっては、勾配が爆発するか消失するかのどちらかになりうる。
Dealing with vanishing and exploding gradients is a fundamental problem when designing RNNs and has inspired some of the biggest advances in modern neural network architectures. In the next chapter, we will talk about specialized architectures that were designed in hopes of mitigating the vanishing gradient problem. However, even modern RNNs often suffer from exploding gradients. One inelegant but ubiquitous solution is to simply clip the gradients forcing the resulting “clipped” gradients to take smaller values.
英語のヒント
勾配の消失と爆発に対処することは、RNNを設計するときの根本的な問題であり、現代のニューラルネットワークの構造における最大級の進歩のいくつかを生み出してきた。次の章では、勾配消失の問題を和らげることを期待して設計された特別な構造について話す。しかし、現代のRNNでさえ、しばしば勾配の爆発に悩まされる。洗練されてはいないがどこでも使われている解決策の一つは、単に勾配をクリップし、その結果の「クリップされた」勾配がより小さな値をとるよう強いることである。
Generally speaking, when optimizing some objective by gradient descent, we iteratively update the parameter of interest, say a vector , but pushing it in the direction of the negative gradient (in stochastic gradient descent, we calculate this gradient on a randomly sampled minibatch). For example, with learning rate , each update takes the form . Let’s further assume that the objective function is sufficiently smooth. Formally, we say that the objective is Lipschitz continuous with constant , meaning that for any and , we have
英語のヒント
一般に、勾配降下法で何らかの目的関数を最適化するときには、関心のあるパラメータ、たとえばベクトルを反復的に更新し、それを負の勾配の方向へ押しやる(確率的勾配降下法では、この勾配をランダムにサンプリングしたミニバッチで計算する)。たとえば、学習率では、各更新はの形をとる。さらに、目的関数が十分になめらかだと仮定しよう。正式には、目的関数は定数でリプシッツ連続であると言う。これは、任意のとについて次が成り立つことを意味する。
(9.5.1)
As you can see, when we update the parameter vector by subtracting , the change in the value of the objective depends on the learning rate, the norm of the gradient and as follows:
英語のヒント
見てのとおり、パラメータベクトルからを引いて更新するとき、目的関数の値の変化は、学習率、勾配のノルム、に次のように依存する。
(9.5.2)
In other words, the objective cannot change by more than . Having a small value for this upper bound might be viewed as good or bad. On the downside, we are limiting the speed at which we can reduce the value of the objective. On the bright side, this limits by just how much we can go wrong in any one gradient step.
英語のヒント
言い換えれば、目的関数はを超えて変化することはありえない。この上界の値が小さいことは、よいとも悪いともみなせる。悪い面では、目的関数の値を減らせる速さを制限している。よい面では、一回の勾配ステップでどれだけ道を誤りうるかを制限している。
When we say that gradients explode, we mean that becomes excessively large. In this worst case, we might do so much damage in a single gradient step that we could undo all of the progress made over the course of thousands of training iterations. When gradients can be so large, neural network training often diverges, failing to reduce the value of the objective. At other times, training eventually converges but is unstable owing to massive spikes in the loss.
英語のヒント
勾配が爆発すると言うとき、それはが過度に大きくなることを意味する。この最悪の場合には、一回の勾配ステップで大きな損害を与え、何千回もの訓練の反復で積み上げた進歩をすべて台無しにしかねない。勾配がこれほど大きくなりうると、ニューラルネットワークの訓練はしばしば発散し、目的関数の値を減らせなくなる。また別のときには、訓練は最終的には収束するが、損失の大きなスパイクのせいで不安定になる。
One way to limit the size of is to shrink the learning rate to tiny values. This has the advantage that we do not bias the updates. But what if we only rarely get large gradients? This drastic move slows down our progress at all steps, just to deal with the rare exploding gradient events. A popular alternative is to adopt a gradient clipping heuristic projecting the gradients onto a ball of some given radius as follows:
英語のヒント
の大きさを制限する一つの方法は、学習率をごく小さな値に縮めることである。これには、更新に偏りを生じさせないという利点がある。しかし、大きな勾配がまれにしか得られないとしたらどうだろう。この思い切った手は、まれな勾配爆発の出来事に対処するためだけに、すべてのステップで進歩を遅くしてしまう。人気のある代わりの方法は、勾配を、与えられた半径の球へと次のように射影する、勾配クリッピングの経験則を採用することである。
(9.5.3)
This ensures that the gradient norm never exceeds and that the updated gradient is entirely aligned with the original direction of . It also has the desirable side-effect of limiting the influence any given minibatch (and within it any given sample) can exert on the parameter vector. This bestows a certain degree of robustness to the model. To be clear, it is a hack. Gradient clipping means that we are not always following the true gradient and it is hard to reason analytically about the possible side effects. However, it is a very useful hack, and is widely adopted in RNN implementations in most deep learning frameworks.
英語のヒント
これにより、勾配のノルムがを決して超えず、更新された勾配がの元の方向と完全にそろうことが保証される。また、任意のミニバッチ(さらにその中の任意の標本)がパラメータベクトルに及ぼしうる影響を制限するという、望ましい副作用もある。これはモデルにある程度の頑健さを与える。はっきり言えば、これはその場しのぎの工夫である。勾配クリッピングは、いつも本当の勾配に従っているわけではないということであり、起こりうる副作用について解析的に推論するのは難しい。しかし、これはとても役に立つ工夫であり、ほとんどの深層学習のフレームワークのRNNの実装で広く採用されている。
Below we define a method to clip gradients, which is invoked by the
fit_epoch method of the d2l.Trainer class (see Section 3.4). Note that when computing the gradient norm, we are concatenating all model parameters, treating them as a single giant parameter vector.英語のヒント
以下では、勾配をクリップするメソッドを定義する。これは
d2l.Trainerクラスのfit_epochメソッドから呼び出される(3.4節を参照)。勾配のノルムを計算するときには、すべてのモデルパラメータを連結し、それらを一つの巨大なパラメータベクトルとして扱っていることに注意してほしい。@d2l.add_to_class(d2l.Trainer) #@save
def clip_gradients(self, grad_clip_val, model):
params = [p for p in model.parameters() if p.requires_grad]
norm = torch.sqrt(sum(torch.sum((p.grad ** 2)) for p in params))
if norm > grad_clip_val:
for param in params:
param.grad[:] *= grad_clip_val / norm