プリンキピア

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

12.4.1. Stochastic Gradient Updates12.4.1 確率的な勾配の更新

In deep learning, the objective function is usually the average of the loss functions for each example in the training dataset. Given a training dataset of n examples, we assume that fi(𝐱) is the loss function with respect to the training example of index i, where 𝐱 is the parameter vector. Then we arrive at the objective function
英語のヒント
深層学習では、目的関数はふつう、訓練データセットの各事例についての損失関数の平均である。n個の事例からなる訓練データセットが与えられたとき、fi(𝐱)を添字iの訓練事例についての損失関数とする。ここで𝐱はパラメータのベクトルである。すると、次の目的関数にたどり着く。
f(𝐱)=1n∑i=1nfi(𝐱).(12.4.1)
The gradient of the objective function at 𝐱 is computed as
英語のヒント
𝐱での目的関数の勾配は次のように計算される。
∇f(𝐱)=1n∑i=1n∇fi(𝐱).(12.4.2)
If gradient descent is used, the computational cost for each independent variable iteration is 𝒪(n), which grows linearly with n. Therefore, when the training dataset is larger, the cost of gradient descent for each iteration will be higher.
英語のヒント
勾配降下法を使うと、独立変数の反復ごとの計算コストは𝒪(n)であり、nに比例して増える。したがって、訓練データセットが大きいほど、反復ごとの勾配降下法のコストは高くなる。
Stochastic gradient descent (SGD) reduces computational cost at each iteration. At each iteration of stochastic gradient descent, we uniformly sample an index i∈{1,…,n} for data examples at random, and compute the gradient ∇fi(𝐱) to update 𝐱:
英語のヒント
確率的勾配降下法(SGD)は、各反復での計算コストを減らす。確率的勾配降下法の各反復では、データ事例の添字i∈{1,…,n}を一様にランダムに抽出し、勾配∇fi(𝐱)を計算して𝐱を更新する。
𝐱←𝐱−η∇fi(𝐱),(12.4.3)
where η is the learning rate. We can see that the computational cost for each iteration drops from 𝒪(n) of the gradient descent to the constant 𝒪(1). Moreover, we want to emphasize that the stochastic gradient ∇fi(𝐱) is an unbiased estimate of the full gradient ∇f(𝐱) because
英語のヒント
ここでηは学習率である。反復ごとの計算コストが、勾配降下法の𝒪(n)から定数の𝒪(1)に下がることがわかる。さらに、確率的勾配∇fi(𝐱)は完全な勾配∇f(𝐱)の不偏推定量であることを強調しておきたい。次が成り立つからである。
𝔼i∇fi(𝐱)=1n∑i=1n∇fi(𝐱)=∇f(𝐱).(12.4.4)
This means that, on average, the stochastic gradient is a good estimate of the gradient.
英語のヒント
これは、平均すれば確率的勾配が勾配のよい推定量であることを意味する。
Now, we will compare it with gradient descent by adding random noise with a mean of 0 and a variance of 1 to the gradient to simulate a stochastic gradient descent.
英語のヒント
では、確率的勾配降下法をシミュレートするため、勾配に平均0、分散1のランダムなノイズを加えて、勾配降下法と比べよう。
def f(x1, x2):  # Objective function
    return x1 ** 2 + 2 * x2 ** 2

def f_grad(x1, x2):  # Gradient of the objective function
    return 2 * x1, 4 * x2

def sgd(x1, x2, s1, s2, f_grad):
    g1, g2 = f_grad(x1, x2)
    # Simulate noisy gradient
    g1 += torch.normal(0.0, 1, (1,)).item()
    g2 += torch.normal(0.0, 1, (1,)).item()
    eta_t = eta * lr()
    return (x1 - eta_t * g1, x2 - eta_t * g2, 0, 0)

def constant_lr():
    return 1

eta = 0.1
lr = constant_lr  # Constant learning rate
d2l.show_trace_2d(f, d2l.train_2d(sgd, steps=50, f_grad=f_grad))
epoch 50, x1: 0.225517, x2: -0.076646
As we can see, the trajectory of the variables in the stochastic gradient descent is much more noisy than the one we observed in gradient descent in Section 12.3. This is due to the stochastic nature of the gradient. That is, even when we arrive near the minimum, we are still subject to the uncertainty injected by the instantaneous gradient via η∇fi(𝐱). Even after 50 steps the quality is still not so good. Even worse, it will not improve after additional steps (we encourage you to experiment with a larger number of steps to confirm this). This leaves us with the only alternative: change the learning rate η. However, if we pick this too small, we will not make any meaningful progress initially. On the other hand, if we pick it too large, we will not get a good solution, as seen above. The only way to resolve these conflicting goals is to reduce the learning rate dynamically as optimization progresses.
英語のヒント
見てのとおり、確率的勾配降下法での変数の軌跡は、12.3節の勾配降下法で観察したものよりずっとノイズが多い。これは勾配の確率的な性質による。つまり、最小値の近くに着いたときでさえ、η∇fi(𝐱)を通じて瞬間的な勾配が注入する不確かさに、まださらされている。50ステップの後でさえ、質はまだそれほどよくない。さらに悪いことに、ステップを追加しても改善しない(これを確かめるため、もっと多くのステップ数で実験することを勧める)。そうなると、残る選択肢は一つしかない。学習率ηを変えることである。しかし、これを小さすぎるように選ぶと、最初のうちは意味のある前進がまったくできない。一方、大きすぎるように選ぶと、上で見たように、よい解が得られない。これらの相反する目標を解決する唯一の方法は、最適化が進むにつれて学習率を動的に下げることである。
This is also the reason for adding a learning rate function lr into the sgd step function. In the example above any functionality for learning rate scheduling lies dormant as we set the associated lr function to be constant.
英語のヒント
これは、sgdのステップ関数に学習率の関数lrを加えた理由でもある。上の例では、対応するlr関数を定数に設定したので、学習率のスケジューリングの機能はどれも眠ったままである。