プリンキピア

第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を論じた。それには、勾配を平均することで分散の量が減るというよい副作用もあった。ミニバッチ確率的勾配降下法は次のように計算できる。
𝐠t,t−1=∂𝐰1|ℬt|∑i∈ℬtf(𝐱i,𝐰t−1)=1|ℬt|∑i∈ℬt𝐡i,t−1.(12.6.1)
To keep the notation simple, here we used 𝐡i,t−1=∂𝐰f(𝐱i,𝐰t−1) as the stochastic gradient descent for sample i using the weights updated at time t−1. 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”:
英語のヒント
記法を簡単にするため、ここでは、時刻t−1に更新された重みを使った標本iについての確率的勾配降下法として𝐡i,t−1=∂𝐰f(𝐱i,𝐰t−1)を使った。ミニバッチで勾配を平均すること以上に、分散を減らす効果の恩恵を受けられたらよいだろう。これを達成する一つの選択肢は、勾配の計算を「漏れのある平均」に置き換えることである。
𝐯t=β𝐯t−1+𝐠t,t−1(12.6.2)
for some β∈(0,1). 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 𝐯t recursively into
英語のヒント
ここでβ∈(0,1)とする。これは実質的に、瞬間的な勾配を、過去の複数の勾配で平均した勾配に置き換える。𝐯は速度と呼ばれる。それは、目的関数の地形を転がり落ちる重いボールが過去の力を積分するのと同じように、過去の勾配をためていく。何が起きているかをもっと詳しく見るため、𝐯tを再帰的に展開すると次のようになる。
𝐯t=β2𝐯t−2+β𝐠t−1,t−2+𝐠t,t−1=…,=∑τ=0t−1βτ𝐠t−τ,t−τ−1.(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.
英語のヒント
予想されるとおり、その効き目のため、モメンタムは深層学習の最適化やそれ以外でよく研究されてきた題材である。掘り下げた解析と対話的なアニメーションは、たとえばGoh (2017)のすばらしい解説記事を参照。それはPolyak (1964)によって提案された。Nesterov (2018)には、凸最適化の文脈での詳しい理論的な議論がある。深層学習でのモメンタムは、ずっと前から役に立つことが知られている。詳しくは、たとえばSutskever et al. (2013)の議論を参照。

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 f(𝐱)=x12+2x22, i.e., a moderately distorted ellipsoid objective. We distort this function further by stretching it out in the x1 direction via
英語のヒント
モメンタム法の幾何学的な性質をもっとよく理解するため、勾配降下法をもう一度見るが、今度はずっと扱いにくい目的関数を使う。12.3節ではf(𝐱)=x12+2x22、つまりほどほどにゆがんだ楕円体の目的関数を使ったことを思い出そう。この関数を、次のようにx1の向きに引き伸ばして、さらにゆがめる。
f(𝐱)=0.1x12+2x22.(12.6.4)
As before f has its minimum at (0,0). This function is very flat in the direction of x1. Let’s see what happens when we perform gradient descent as before on this new function. We pick a learning rate of 0.4.
英語のヒント
前と同じように、fは(0,0)で最小値をとる。この関数はx1の向きにとても平らである。この新しい関数に前と同じように勾配降下法を行うと何が起こるかを見よう。学習率は0.4とする。
%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.000073
By construction, the gradient in the x2 direction is much higher and changes much more rapidly than in the horizontal x1 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 x2 direction but we are saddled with slow convergence in the x1 direction. Conversely, with a large learning rate we progress rapidly in the x1 direction but diverge in x2. The example below illustrates what happens even after a slight increase in learning rate from 0.4 to 0.6. Convergence in the x1 direction improves but the overall solution quality is much worse.
英語のヒント
作り方からして、x2の向きの勾配は、横のx1の向きよりずっと大きく、ずっと速く変わる。したがって、二つの望ましくない選択のあいだで身動きがとれない。小さな学習率を選べば、解がx2の向きに発散しないようにできるが、x1の向きの遅い収束を背負い込む。逆に、大きな学習率では、x1の向きには速く進むが、x2の向きには発散する。下の例は、学習率を0.4から0.6へわずかに上げただけでも何が起こるかを示している。x1の向きの収束はよくなるが、解全体の質はずっと悪くなる。
eta = 0.6
d2l.show_trace_2d(f_2d, d2l.train_2d(gd_2d))
epoch 20, x1: -0.387814, x2: -1673.365109

12.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 x1 direction this will aggregate well-aligned gradients, thus increasing the distance we cover with every step. Conversely, in the x2 direction where gradients oscillate, an aggregate gradient will reduce step size due to oscillations that cancel each other out. Using 𝐯t instead of the gradient 𝐠t yields the following update equations:
英語のヒント
モメンタム法を使うと、上で説明した勾配降下法の問題を解決できる。上の最適化の軌跡を見ると、過去の勾配を平均するとうまくいくのではないかと直観できるかもしれない。結局のところ、x1の向きでは、これはよくそろった勾配を集めるので、1ステップごとに進む距離が伸びる。逆に、勾配が振動するx2の向きでは、集めた勾配は、振動が互いに打ち消し合うので、ステップの大きさを小さくする。勾配𝐠tの代わりに𝐯tを使うと、次の更新式が得られる。
𝐯t←β𝐯t−1+𝐠t,t−1,𝐱t←𝐱t−1−ηt𝐯t.(12.6.5)
Note that for β=0 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.
英語のヒント
β=0のときは通常の勾配降下法に戻ることに注意してほしい。数学的な性質にもっと深く踏み込む前に、このアルゴリズムが実際にどうふるまうかを手短に見てみよう。
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.002553
As 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 β=0.25 leads to a trajectory that barely converges at all. Nonetheless, it is a lot better than without momentum (when the solution diverges).
英語のヒント
見てのとおり、前に使ったのと同じ学習率でも、モメンタムはやはりよく収束する。モメンタムのパラメータを下げると何が起こるかを見よう。それを半分のβ=0.25にすると、ほとんど収束しない軌跡になる。それでも、モメンタムなし(解が発散するとき)よりはずっとよい。
eta, beta = 0.6, 0.25
d2l.show_trace_2d(f_2d, d2l.train_2d(momentum_2d))
epoch 20, x1: -0.126340, x2: -0.186632
Note 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 𝐠t,t−1 with 𝐠t. Last, for convenience we initialize 𝐯0=0 at time t=0. Let’s look at what leaky averaging actually does to the updates.
英語のヒント
モメンタムは、確率的勾配降下法、特にミニバッチ確率的勾配降下法と組み合わせられることに注意してほしい。変わるのは、その場合に勾配𝐠t,t−1を𝐠tに置き換えることだけである。最後に、便宜上、時刻t=0で𝐯0=0と初期化する。漏れのある平均が更新に実際に何をするかを見てみよう。

12.6.1.4. Effective Sample Weight12.6.1.4 実質的な標本の重み

Recall that 𝐯t=∑τ=0t−1βτ𝐠t−τ,t−τ−1. In the limit the terms add up to ∑τ=0∞βτ=11−β. In other words, rather than taking a step of size η in gradient descent or stochastic gradient descent we take a step of size η1−β 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.
英語のヒント
𝐯t=∑τ=0t−1βτ𝐠t−τ,t−τ−1であることを思い出そう。極限では、項の和は∑τ=0∞βτ=11−βになる。言い換えると、勾配降下法や確率的勾配降下法で大きさηのステップをとる代わりに、大きさη1−βのステップをとり、同時に、もっとずっと行儀がよいかもしれない降下の向きを扱う。これらは一つで二つの利点である。βのさまざまな選び方で重みづけがどうふるまうかを示すため、下の図を考えよう。
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();