第12章最適化アルゴリズム
12.6.1. Basics12.6.1 基礎
In this section, we will explore more effective optimization algorithms, especially for certain types of optimization problems that are common in practice.
英語のヒント
この節では、もっと効果的な最適化アルゴリズムを、特に実際によく見られる種類の最適化問題について探る。
12.6.1.1. Leaky Averages12.6.1.1 漏れのある平均
The previous section saw us discussing minibatch SGD as a means for accelerating computation. It also had the nice side-effect that averaging gradients reduced the amount of variance. The minibatch stochastic gradient descent can be calculated by:
英語のヒント
前の節では、計算を速める手段としてミニバッチSGDを論じた。それには、勾配を平均することで分散の量が減るというよい副作用もあった。ミニバッチ確率的勾配降下法は次のように計算できる。
(12.6.1)
To keep the notation simple, here we used as the stochastic gradient descent for sample using the weights updated at time . It would be nice if we could benefit from the effect of variance reduction even beyond averaging gradients on a minibatch. One option to accomplish this task is to replace the gradient computation by a “leaky average”:
英語のヒント
記法を簡単にするため、ここでは、時刻に更新された重みを使った標本についての確率的勾配降下法としてを使った。ミニバッチで勾配を平均すること以上に、分散を減らす効果の恩恵を受けられたらよいだろう。これを達成する一つの選択肢は、勾配の計算を「漏れのある平均」に置き換えることである。
(12.6.2)
for some . This effectively replaces the instantaneous gradient by one that is been averaged over multiple past gradients. is called velocity. It accumulates past gradients similar to how a heavy ball rolling down the objective function landscape integrates over past forces. To see what is happening in more detail let’s expand recursively into
英語のヒント
ここでとする。これは実質的に、瞬間的な勾配を、過去の複数の勾配で平均した勾配に置き換える。は速度と呼ばれる。それは、目的関数の地形を転がり落ちる重いボールが過去の力を積分するのと同じように、過去の勾配をためていく。何が起きているかをもっと詳しく見るため、を再帰的に展開すると次のようになる。
(12.6.3)
Large amounts to a long-range average, whereas small amounts to only a slight correction relative to a gradient method. The new gradient replacement no longer points into the direction of steepest descent on a particular instance any longer but rather in the direction of a weighted average of past gradients. This allows us to realize most of the benefits of averaging over a batch without the cost of actually computing the gradients on it. We will revisit this averaging procedure in more detail later.
英語のヒント
が大きければ長い範囲の平均になり、が小さければ勾配法に対するわずかな補正にしかならない。新しい勾配の代わりは、もはや特定の事例での最急降下の向きを指すのではなく、過去の勾配の重みつき平均の向きを指す。これにより、バッチについて実際に勾配を計算するコストをかけずに、バッチで平均することの利点のほとんどを実現できる。この平均の手続きは、後でもっと詳しく取り上げ直す。
The above reasoning formed the basis for what is now known as accelerated gradient methods, such as gradients with momentum. They enjoy the additional benefit of being much more effective in cases where the optimization problem is ill-conditioned (i.e., where there are some directions where progress is much slower than in others, resembling a narrow canyon). Furthermore, they allow us to average over subsequent gradients to obtain more stable directions of descent. Indeed, the aspect of acceleration even for noise-free convex problems is one of the key reasons why momentum works and why it works so well.
英語のヒント
上の推論は、いまでは加速勾配法として知られるもの、たとえばモメンタムつきの勾配の基礎になった。それらには、最適化問題の条件が悪い場合(つまり、狭い峡谷のように、ほかの向きよりずっと進みが遅い向きがある場合)に、はるかに効果的だという利点もある。さらに、それらは後に続く勾配を平均して、もっと安定した降下の向きを得られるようにする。実際、ノイズのない凸の問題でさえ得られる加速という面が、モメンタムがうまくいく、しかもとてもうまくいく主な理由の一つである。
As one would expect, due to its efficacy momentum is a well-studied subject in optimization for deep learning and beyond. See e.g., the beautiful expository article by Goh (2017) for an in-depth analysis and interactive animation. It was proposed by Polyak (1964). Nesterov (2018) has a detailed theoretical discussion in the context of convex optimization. Momentum in deep learning has been known to be beneficial for a long time. See e.g., the discussion by Sutskever et al. (2013) for details.
英語のヒント
12.6.1.2. An Ill-conditioned Problem12.6.1.2 条件の悪い問題
To get a better understanding of the geometric properties of the momentum method we revisit gradient descent, albeit with a significantly less pleasant objective function. Recall that in Section 12.3 we used , i.e., a moderately distorted ellipsoid objective. We distort this function further by stretching it out in the direction via
英語のヒント
モメンタム法の幾何学的な性質をもっとよく理解するため、勾配降下法をもう一度見るが、今度はずっと扱いにくい目的関数を使う。12.3節では、つまりほどほどにゆがんだ楕円体の目的関数を使ったことを思い出そう。この関数を、次のようにの向きに引き伸ばして、さらにゆがめる。
(12.6.4)
As before has its minimum at . This function is very flat in the direction of . Let’s see what happens when we perform gradient descent as before on this new function. We pick a learning rate of .
英語のヒント
前と同じように、はで最小値をとる。この関数はの向きにとても平らである。この新しい関数に前と同じように勾配降下法を行うと何が起こるかを見よう。学習率はとする。
%matplotlib inline
import torch
from d2l import torch as d2l
eta = 0.4
def f_2d(x1, x2):
return 0.1 * x1 ** 2 + 2 * x2 ** 2
def gd_2d(x1, x2, s1, s2):
return (x1 - eta * 0.2 * x1, x2 - eta * 4 * x2, 0, 0)
d2l.show_trace_2d(f_2d, d2l.train_2d(gd_2d))epoch 20, x1: -0.943467, x2: -0.000073By construction, the gradient in the direction is much higher and changes much more rapidly than in the horizontal direction. Thus we are stuck between two undesirable choices: if we pick a small learning rate we ensure that the solution does not diverge in the direction but we are saddled with slow convergence in the direction. Conversely, with a large learning rate we progress rapidly in the direction but diverge in . The example below illustrates what happens even after a slight increase in learning rate from to . Convergence in the direction improves but the overall solution quality is much worse.
英語のヒント
作り方からして、の向きの勾配は、横のの向きよりずっと大きく、ずっと速く変わる。したがって、二つの望ましくない選択のあいだで身動きがとれない。小さな学習率を選べば、解がの向きに発散しないようにできるが、の向きの遅い収束を背負い込む。逆に、大きな学習率では、の向きには速く進むが、の向きには発散する。下の例は、学習率をからへわずかに上げただけでも何が起こるかを示している。の向きの収束はよくなるが、解全体の質はずっと悪くなる。
eta = 0.6
d2l.show_trace_2d(f_2d, d2l.train_2d(gd_2d))epoch 20, x1: -0.387814, x2: -1673.36510912.6.1.3. The Momentum Method12.6.1.3 モメンタム法
The momentum method allows us to solve the gradient descent problem described above. Looking at the optimization trace above we might intuit that averaging gradients over the past would work well. After all, in the direction this will aggregate well-aligned gradients, thus increasing the distance we cover with every step. Conversely, in the direction where gradients oscillate, an aggregate gradient will reduce step size due to oscillations that cancel each other out. Using instead of the gradient yields the following update equations:
英語のヒント
モメンタム法を使うと、上で説明した勾配降下法の問題を解決できる。上の最適化の軌跡を見ると、過去の勾配を平均するとうまくいくのではないかと直観できるかもしれない。結局のところ、の向きでは、これはよくそろった勾配を集めるので、1ステップごとに進む距離が伸びる。逆に、勾配が振動するの向きでは、集めた勾配は、振動が互いに打ち消し合うので、ステップの大きさを小さくする。勾配の代わりにを使うと、次の更新式が得られる。
(12.6.5)
Note that for we recover regular gradient descent. Before delving deeper into the mathematical properties let’s have a quick look at how the algorithm behaves in practice.
英語のヒント
のときは通常の勾配降下法に戻ることに注意してほしい。数学的な性質にもっと深く踏み込む前に、このアルゴリズムが実際にどうふるまうかを手短に見てみよう。
def momentum_2d(x1, x2, v1, v2):
v1 = beta * v1 + 0.2 * x1
v2 = beta * v2 + 4 * x2
return x1 - eta * v1, x2 - eta * v2, v1, v2
eta, beta = 0.6, 0.5
d2l.show_trace_2d(f_2d, d2l.train_2d(momentum_2d))epoch 20, x1: 0.007188, x2: 0.002553As we can see, even with the same learning rate that we used before, momentum still converges well. Let’s see what happens when we decrease the momentum parameter. Halving it to leads to a trajectory that barely converges at all. Nonetheless, it is a lot better than without momentum (when the solution diverges).
英語のヒント
見てのとおり、前に使ったのと同じ学習率でも、モメンタムはやはりよく収束する。モメンタムのパラメータを下げると何が起こるかを見よう。それを半分のにすると、ほとんど収束しない軌跡になる。それでも、モメンタムなし(解が発散するとき)よりはずっとよい。
eta, beta = 0.6, 0.25
d2l.show_trace_2d(f_2d, d2l.train_2d(momentum_2d))epoch 20, x1: -0.126340, x2: -0.186632Note that we can combine momentum with stochastic gradient descent and in particular, minibatch stochastic gradient descent. The only change is that in that case we replace the gradients with . Last, for convenience we initialize at time . Let’s look at what leaky averaging actually does to the updates.
英語のヒント
モメンタムは、確率的勾配降下法、特にミニバッチ確率的勾配降下法と組み合わせられることに注意してほしい。変わるのは、その場合に勾配をに置き換えることだけである。最後に、便宜上、時刻でと初期化する。漏れのある平均が更新に実際に何をするかを見てみよう。
12.6.1.4. Effective Sample Weight12.6.1.4 実質的な標本の重み
Recall that . In the limit the terms add up to . In other words, rather than taking a step of size in gradient descent or stochastic gradient descent we take a step of size while at the same time, dealing with a potentially much better behaved descent direction. These are two benefits in one. To illustrate how weighting behaves for different choices of consider the diagram below.
英語のヒント
であることを思い出そう。極限では、項の和はになる。言い換えると、勾配降下法や確率的勾配降下法で大きさのステップをとる代わりに、大きさのステップをとり、同時に、もっとずっと行儀がよいかもしれない降下の向きを扱う。これらは一つで二つの利点である。のさまざまな選び方で重みづけがどうふるまうかを示すため、下の図を考えよう。
d2l.set_figsize()
betas = [0.95, 0.9, 0.6, 0]
for beta in betas:
x = torch.arange(40).detach().numpy()
d2l.plt.plot(x, beta ** x, label=f'beta = {beta:.2f}')
d2l.plt.xlabel('time')
d2l.plt.legend();