プリンキピア

第12章最適化アルゴリズム

12.7.3. The Algorithm12.7.3 アルゴリズム

Let’s formalize the discussion from above. We use the variable 𝐬t to accumulate past gradient variance as follows.
英語のヒント
上の議論を形式化しよう。変数𝐬tを使って、過去の勾配の分散を次のようにためていく。
𝐠t=∂𝐰l(yt,f(𝐱t,𝐰)),𝐬t=𝐬t−1+𝐠t2,𝐰t=𝐰t−1−η𝐬t+ϵ⋅𝐠t.(12.7.5)
Here the operation are applied coordinate wise. That is, 𝐯2 has entries vi2. Likewise 1v has entries 1vi and 𝐮⋅𝐯 has entries uivi. As before η is the learning rate and ϵ is an additive constant that ensures that we do not divide by 0. Last, we initialize 𝐬0=𝟎.
英語のヒント
ここで演算は座標ごとに当てはめられる。つまり、𝐯2の成分はvi2である。同じように、1vの成分は1viであり、𝐮⋅𝐯の成分はuiviである。前と同じように、ηは学習率であり、ϵは0で割らないようにするための加法的な定数である。最後に、𝐬0=𝟎と初期化する。
Just like in the case of momentum we need to keep track of an auxiliary variable, in this case to allow for an individual learning rate per coordinate. This does not increase the cost of Adagrad significantly relative to SGD, simply since the main cost is typically to compute l(yt,f(𝐱t,𝐰)) and its derivative.
英語のヒント
モメンタムの場合と同じように、補助変数を追跡する必要がある。この場合は、座標ごとに個別の学習率を使えるようにするためである。主なコストはふつうl(yt,f(𝐱t,𝐰))とその導関数を計算することなので、これでSGDと比べてAdagradのコストが大きく増えることはない。
Note that accumulating squared gradients in 𝐬t means that 𝐬t grows essentially at linear rate (somewhat slower than linearly in practice, since the gradients initially diminish). This leads to an 𝒪(t−12) learning rate, albeit adjusted on a per coordinate basis. For convex problems this is perfectly adequate. In deep learning, though, we might want to decrease the learning rate rather more slowly. This led to a number of Adagrad variants that we will discuss in the subsequent chapters. For now let’s see how it behaves in a quadratic convex problem. We use the same problem as before:
英語のヒント
勾配の2乗を𝐬tにためることは、𝐬tが本質的に線形の速さで増えることを意味する点に注意してほしい(勾配は最初のうち小さくなっていくので、実際には線形より少し遅い)。これは、座標ごとに調整されるとはいえ、𝒪(t−12)の学習率につながる。凸の問題では、これでまったく十分である。しかし深層学習では、学習率をもっとゆっくり下げたいかもしれない。このことから、後の章で論じるAdagradの変種がいくつも生まれた。いまは、2次の凸の問題でそれがどうふるまうかを見よう。前と同じ問題を使う。
f(𝐱)=0.1x12+2x22.(12.7.6)
We are going to implement Adagrad using the same learning rate previously, i.e., η=0.4. As we can see, the iterative trajectory of the independent variable is smoother. However, due to the cumulative effect of 𝒔t, the learning rate continuously decays, so the independent variable does not move as much during later stages of iteration.
英語のヒント
前と同じ学習率、つまりη=0.4を使ってAdagradを実装する。見てのとおり、独立変数の反復の軌跡はもっとなめらかである。しかし、𝒔tの累積の効果のため、学習率は減衰し続けるので、反復の後の段階では独立変数はあまり動かない。
%matplotlib inline
import math
import torch
from d2l import torch as d2l

def adagrad_2d(x1, x2, s1, s2):
    eps = 1e-6
    g1, g2 = 0.2 * x1, 4 * x2
    s1 += g1 ** 2
    s2 += g2 ** 2
    x1 -= eta / math.sqrt(s1 + eps) * g1
    x2 -= eta / math.sqrt(s2 + eps) * g2
    return x1, x2, s1, s2

def f_2d(x1, x2):
    return 0.1 * x1 ** 2 + 2 * x2 ** 2

eta = 0.4
d2l.show_trace_2d(f_2d, d2l.train_2d(adagrad_2d))
epoch 20, x1: -2.382563, x2: -0.158591
As we increase the learning rate to 2 we see much better behavior. This already indicates that the decrease in learning rate might be rather aggressive, even in the noise-free case and we need to ensure that parameters converge appropriately.
英語のヒント
学習率を2に上げると、ずっとよいふるまいが見られる。これはすでに、ノイズのない場合でさえ学習率の下がり方がかなり急であるかもしれず、パラメータが適切に収束するようにする必要があることを示している。
eta = 2
d2l.show_trace_2d(f_2d, d2l.train_2d(adagrad_2d))
epoch 20, x1: -0.002295, x2: -0.000000