プリンキピア

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

12.3.3. Adaptive Methods12.3.3 適応的な手法

As we could see in Section 12.3.1.1, getting the learning rate η “just right” is tricky. If we pick it too small, we make little progress. If we pick it too large, the solution oscillates and in the worst case it might even diverge. What if we could determine η automatically or get rid of having to select a learning rate at all? Second-order methods that look not only at the value and gradient of the objective function but also at its curvature can help in this case. While these methods cannot be applied to deep learning directly due to the computational cost, they provide useful intuition into how to design advanced optimization algorithms that mimic many of the desirable properties of the algorithms outlined below.
英語のヒント
12.3.1.1節で見たように、学習率ηを「ちょうどよく」するのは難しい。小さすぎるとほとんど前進しない。大きすぎると解が振動し、最悪の場合には発散さえするかもしれない。ηを自動的に決められたら、あるいは学習率を選ばなければならないこと自体をなくせたらどうだろうか。目的関数の値と勾配だけでなく、その曲率も見る2次の手法が、この場合に役立つ。こうした手法は、計算のコストのために深層学習に直接は当てはめられないが、以下で概説するアルゴリズムの望ましい性質の多くをまねる高度な最適化アルゴリズムをどう設計するかについて、役に立つ直観を与えてくれる。

12.3.3.1. Newton’s Method12.3.3.1 ニュートン法

Reviewing the Taylor expansion of some function f:ℝd→ℝ there is no need to stop after the first term. In fact, we can write it as
英語のヒント
ある関数f:ℝd→ℝのテイラー展開を見直すと、最初の項の後で止める必要はない。実際、次のように書ける。
f(𝐱+𝝐)=f(𝐱)+𝝐⊤∇f(𝐱)+12𝝐⊤∇2f(𝐱)𝝐+𝒪(∥𝝐∥3).(12.3.8)
To avoid cumbersome notation we define 𝐇=def∇2f(𝐱) to be the Hessian of f, which is a d×d matrix. For small d and simple problems 𝐇 is easy to compute. For deep neural networks, on the other hand, 𝐇 may be prohibitively large, due to the cost of storing 𝒪(d2) entries. Furthermore it may be too expensive to compute via backpropagation. For now let’s ignore such considerations and look at what algorithm we would get.
英語のヒント
面倒な記法を避けるため、𝐇=def∇2f(𝐱)をfのヘッセ行列と定義する。これはd×dの行列である。小さなdと単純な問題では、𝐇は簡単に計算できる。一方、深いニューラルネットワークでは、𝒪(d2)個の成分を格納するコストのため、𝐇は手に負えないほど大きくなるかもしれない。さらに、逆伝播で計算するには高くつきすぎるかもしれない。いまはそうした事情を無視し、どんなアルゴリズムが得られるかを見よう。
After all, the minimum of f satisfies ∇f=0. Following calculus rules in Section 2.4.3, by taking derivatives of (12.3.8) with regard to 𝝐 and ignoring higher-order terms we arrive at
英語のヒント
結局のところ、fの最小値は∇f=0を満たす。2.4.3節の微積分の規則に従い、(12.3.8)を𝝐について微分し、高次の項を無視すると、次にたどり着く。
∇f(𝐱)+𝐇𝝐=0 and hence 𝝐=−𝐇−1∇f(𝐱).(12.3.9)
That is, we need to invert the Hessian 𝐇 as part of the optimization problem.
英語のヒント
つまり、最適化問題の一部として、ヘッセ行列𝐇の逆行列を求める必要がある。
As a simple example, for f(x)=12x2 we have ∇f(x)=x and 𝐇=1. Hence for any x we obtain ϵ=−x. In other words, a single step is sufficient to converge perfectly without the need for any adjustment! Alas, we got a bit lucky here: the Taylor expansion was exact since f(x+ϵ)=12x2+ϵx+12ϵ2.
英語のヒント
単純な例として、f(x)=12x2では∇f(x)=xかつ𝐇=1である。したがって、どのxについてもϵ=−xが得られる。言い換えると、まったく調整しなくても、1ステップで完全に収束するのに十分である。残念ながら、ここでは少し運がよかった。f(x+ϵ)=12x2+ϵx+12ϵ2なので、テイラー展開が厳密だったのである。
Let’s see what happens in other problems. Given a convex hyperbolic cosine function f(x)=cosh⁡(cx) for some constant c, we can see that the global minimum at x=0 is reached after a few iterations.
英語のヒント
ほかの問題で何が起こるかを見よう。ある定数cについての凸の双曲線余弦関数f(x)=cosh⁡(cx)が与えられたとき、数回の反復の後にx=0での大域的な最小値に達するのがわかる。
c = torch.tensor(0.5)

def f(x):  # Objective function
    return torch.cosh(c * x)

def f_grad(x):  # Gradient of the objective function
    return c * torch.sinh(c * x)

def f_hess(x):  # Hessian of the objective function
    return c**2 * torch.cosh(c * x)

def newton(eta=1):
    x = 10.0
    results = [x]
    for i in range(10):
        x -= eta * f_grad(x) / f_hess(x)
        results.append(float(x))
    print('epoch 10, x:', x)
    return results

show_trace(newton(), f)
epoch 10, x: tensor(0.)
Now let’s consider a nonconvex function, such as f(x)=xcos⁡(cx) for some constant c. After all, note that in Newton’s method we end up dividing by the Hessian. This means that if the second derivative is negative we may walk into the direction of increasing the value of f. That is a fatal flaw of the algorithm. Let’s see what happens in practice.
英語のヒント
では、ある定数cについてf(x)=xcos⁡(cx)のような非凸の関数を考えよう。結局のところ、ニュートン法ではヘッセ行列で割ることになる点に注意してほしい。これは、2階導関数が負なら、fの値が増える向きへ進んでしまうかもしれないことを意味する。それはこのアルゴリズムの致命的な欠陥である。実際に何が起こるかを見よう。
c = torch.tensor(0.15 * np.pi)

def f(x):  # Objective function
    return x * torch.cos(c * x)

def f_grad(x):  # Gradient of the objective function
    return torch.cos(c * x) - c * x * torch.sin(c * x)

def f_hess(x):  # Hessian of the objective function
    return - 2 * c * torch.sin(c * x) - x * c**2 * torch.cos(c * x)

show_trace(newton(), f)
epoch 10, x: tensor(26.8341)
This went spectacularly wrong. How can we fix it? One way would be to “fix” the Hessian by taking its absolute value instead. Another strategy is to bring back the learning rate. This seems to defeat the purpose, but not quite. Having second-order information allows us to be cautious whenever the curvature is large and to take longer steps whenever the objective function is flatter. Let’s see how this works with a slightly smaller learning rate, say η=0.5. As we can see, we have quite an efficient algorithm.
英語のヒント
これは見事に失敗した。どう直せばよいだろうか。一つのやり方は、代わりにヘッセ行列の絶対値をとって「直す」ことだろう。別の戦略は、学習率を戻すことである。これは目的に反するように見えるが、そうでもない。2次の情報があれば、曲率が大きいときには慎重になり、目的関数が平らなときには長いステップをとれる。学習率を少し小さく、たとえばη=0.5にして、これがどう働くかを見よう。見てのとおり、かなり効率のよいアルゴリズムになっている。
show_trace(newton(0.5), f)
epoch 10, x: tensor(7.2699)

12.3.3.2. Convergence Analysis12.3.3.2 収束の解析

We only analyze the convergence rate of Newton’s method for some convex and three times differentiable objective function f, where the second derivative is nonzero, i.e., f″>0. The multivariate proof is a straightforward extension of the one-dimensional argument below and omitted since it does not help us much in terms of intuition.
英語のヒント
ここでは、2階導関数がゼロでない、つまりf″>0である、凸で3回微分可能な目的関数fについて、ニュートン法の収束の速さだけを解析する。多変数の証明は、下の1次元の議論を素直に拡張したものであり、直観の面ではあまり助けにならないので省く。
Denote by x(k) the value of x at the kth iteration and let e(k)=defx(k)−x∗ be the distance from optimality at the kth iteration. By Taylor expansion we have that the condition f′(x∗)=0 can be written as
英語のヒント
kthの反復でのxの値をx(k)で表し、kthの反復での最適からの距離をe(k)=defx(k)−x∗とする。テイラー展開により、条件f′(x∗)=0は次のように書ける。
0=f′(x(k)−e(k))=f′(x(k))−e(k)f″(x(k))+12(e(k))2f‴(ξ(k)),(12.3.10)
which holds for some ξ(k)∈[x(k)−e(k),x(k)]. Dividing the above expansion by f″(x(k)) yields
英語のヒント
これは、あるξ(k)∈[x(k)−e(k),x(k)]について成り立つ。上の展開をf″(x(k))で割ると、次が得られる。
e(k)−f′(x(k))f″(x(k))=12(e(k))2f‴(ξ(k))f″(x(k)).(12.3.11)
Recall that we have the update x(k+1)=x(k)−f′(x(k))/f″(x(k)). Plugging in this update equation and taking the absolute value of both sides, we have
英語のヒント
更新がx(k+1)=x(k)−f′(x(k))/f″(x(k))であることを思い出そう。この更新式を代入し、両辺の絶対値をとると、次が得られる。
|e(k+1)|=12(e(k))2|f‴(ξ(k))|f″(x(k)).(12.3.12)
Consequently, whenever we are in a region of bounded |f‴(ξ(k))|/(2f″(x(k)))≤c, we have a quadratically decreasing error
英語のヒント
その結果、|f‴(ξ(k))|/(2f″(x(k)))≤cと有界な領域にいるときはいつでも、誤差は次のように2次的に減少する。
|e(k+1)|≤c(e(k))2.(12.3.13)
As an aside, optimization researchers call this linear convergence, whereas a condition such as |e(k+1)|≤α|e(k)| would be called a constant rate of convergence. Note that this analysis comes with a number of caveats. First, we do not really have much of a guarantee when we will reach the region of rapid convergence. Instead, we only know that once we reach it, convergence will be very quick. Second, this analysis requires that f is well-behaved up to higher-order derivatives. It comes down to ensuring that f does not have any “surprising” properties in terms of how it might change its values.
英語のヒント
余談だが、最適化の研究者はこれを線形収束と呼び、|e(k+1)|≤α|e(k)|のような条件は一定の速さの収束と呼ばれる。この解析にはいくつかの注意点があることに注意してほしい。第一に、速い収束の領域にいつ到達するかについては、あまり保証がない。代わりにわかっているのは、いったんそこに到達すれば収束がとても速いということだけである。第二に、この解析では、fが高階の導関数まで行儀がよいことが要る。つまり、値がどう変わりうるかについて、fが「意外な」性質をまったくもたないようにすることに帰着する。

12.3.3.3. Preconditioning12.3.3.3 前条件づけ

Quite unsurprisingly computing and storing the full Hessian is very expensive. It is thus desirable to find alternatives. One way to improve matters is preconditioning. It avoids computing the Hessian in its entirety but only computes the diagonal entries. This leads to update algorithms of the form
英語のヒント
当然ながら、ヘッセ行列全体を計算して格納するのはとても高くつく。したがって、代わりのものを見つけるのが望ましい。事態を改善する一つの方法は前条件づけである。前条件づけでは、ヘッセ行列全体を計算するのを避け、対角成分だけを計算する。これは、次の形の更新アルゴリズムにつながる。
𝐱←𝐱−ηdiag(𝐇)−1∇f(𝐱).(12.3.14)
While this is not quite as good as the full Newton’s method, it is still much better than not using it. To see why this might be a good idea consider a situation where one variable denotes height in millimeters and the other one denotes height in kilometers. Assuming that for both the natural scale is in meters, we have a terrible mismatch in parametrizations. Fortunately, using preconditioning removes this. Effectively preconditioning with gradient descent amounts to selecting a different learning rate for each variable (coordinate of vector 𝐱). As we will see later, preconditioning drives some of the innovation in stochastic gradient descent optimization algorithms.
英語のヒント
これは完全なニュートン法ほどよくはないが、使わないよりはずっとよい。これがなぜよい考えでありうるかを見るため、一方の変数が高さをミリメートルで表し、もう一方の変数が高さをキロメートルで表す状況を考えよう。どちらも自然な尺度はメートルだとすると、パラメータ化にひどい食い違いがある。幸い、前条件づけを使えばこれが取り除かれる。実質的に、勾配降下法で前条件づけをすることは、変数(ベクトル𝐱の座標)ごとに違う学習率を選ぶことにあたる。後で見るように、前条件づけは、確率的勾配降下法の最適化アルゴリズムでの革新の一部を推し進めている。