第12章最適化アルゴリズム
12.3.1. One-Dimensional Gradient Descent12.3.1 1次元の勾配降下法
Gradient descent in one dimension is an excellent example to explain why the gradient descent algorithm may reduce the value of the objective function. Consider some continuously differentiable real-valued function . Using a Taylor expansion we obtain
英語のヒント
1次元の勾配降下法は、勾配降下法のアルゴリズムが目的関数の値を減らしうる理由を説明するのにうってつけの例である。連続的に微分可能な実数値の関数を考えよう。テイラー展開を使うと、次が得られる。
(12.3.1)
That is, in first-order approximation is given by the function value and the first derivative at . It is not unreasonable to assume that for small moving in the direction of the negative gradient will decrease . To keep things simple we pick a fixed step size and choose . Plugging this into the Taylor expansion above we get
英語のヒント
つまり、1次の近似では、は、での関数の値と1階導関数で与えられる。小さなについて、負の勾配の向きに動けばが減ると仮定するのは不合理ではない。話を簡単にするため、固定したステップサイズを選び、とする。これを上のテイラー展開に代入すると、次が得られる。
(12.3.2)
If the derivative does not vanish we make progress since . Moreover, we can always choose small enough for the higher-order terms to become irrelevant. Hence we arrive at
英語のヒント
導関数が消えない、つまりなら、なので前進できる。さらに、は、高次の項が問題にならなくなるほど小さく、いつでも選べる。したがって、次にたどり着く。
(12.3.3)
This means that, if we use
英語のヒント
これは、次の式
(12.3.4)
to iterate , the value of function might decline. Therefore, in gradient descent we first choose an initial value and a constant and then use them to continuously iterate until the stop condition is reached, for example, when the magnitude of the gradient is small enough or the number of iterations has reached a certain value.
英語のヒント
を使ってを反復すれば、関数の値が下がるかもしれないということである。したがって、勾配降下法では、まず初期値と定数を選び、それらを使って、停止条件に達するまでを繰り返し反復する。停止条件は、たとえば勾配の大きさが十分小さくなったときや、反復の回数がある値に達したときである。
For simplicity we choose the objective function to illustrate how to implement gradient descent. Although we know that is the solution to minimize , we still use this simple function to observe how changes.
英語のヒント
簡単のため、勾配降下法をどう実装するかを示す目的関数としてを選ぶ。がを最小化する解だとわかっているが、それでもこの単純な関数を使って、がどう変わるかを観察する。
%matplotlib inline
import numpy as np
import torch
from d2l import torch as d2l
def f(x): # Objective function
return x ** 2
def f_grad(x): # Gradient (derivative) of the objective function
return 2 * xNext, we use as the initial value and assume . Using gradient descent to iterate for 10 times we can see that, eventually, the value of approaches the optimal solution.
英語のヒント
次に、初期値をとし、とする。勾配降下法でを10回反復すると、最終的にの値が最適解に近づくのがわかる。
def gd(eta, f_grad):
x = 10.0
results = [x]
for i in range(10):
x -= eta * f_grad(x)
results.append(float(x))
print(f'epoch 10, x: {x:f}')
return results
results = gd(0.2, f_grad)epoch 10, x: 0.060466The progress of optimizing over can be plotted as follows.
英語のヒント
についての最適化の進み具合は、次のようにプロットできる。
def show_trace(results, f):
n = max(abs(min(results)), abs(max(results)))
f_line = torch.arange(-n, n, 0.01)
d2l.set_figsize()
d2l.plot([f_line, results], [[f(x) for x in f_line], [
f(x) for x in results]], 'x', 'f(x)', fmts=['-', '-o'])
show_trace(results, f)12.3.1.1. Learning Rate12.3.1.1 学習率
The learning rate can be set by the algorithm designer. If we use a learning rate that is too small, it will cause to update very slowly, requiring more iterations to get a better solution. To show what happens in such a case, consider the progress in the same optimization problem for . As we can see, even after 10 steps we are still very far from the optimal solution.
英語のヒント
学習率は、アルゴリズムの設計者が設定できる。小さすぎる学習率を使うと、の更新がとても遅くなり、よりよい解を得るのにもっと多くの反復が必要になる。そのような場合に何が起こるかを示すため、同じ最適化問題でとしたときの進み具合を考えよう。見てのとおり、10ステップの後でさえ、まだ最適解からとても遠い。
show_trace(gd(0.05, f_grad), f)epoch 10, x: 3.486784Conversely, if we use an excessively high learning rate, might be too large for the first-order Taylor expansion formula. That is, the term in (12.3.2) might become significant. In this case, we cannot guarantee that the iteration of will be able to lower the value of . For example, when we set the learning rate to , overshoots the optimal solution and gradually diverges.
英語のヒント
逆に、学習率が高すぎると、は1次のテイラー展開の式には大きすぎるかもしれない。つまり、(12.3.2)の項が無視できなくなるかもしれない。この場合、の反復での値を下げられるとは保証できない。たとえば、学習率をに設定すると、は最適解を行き過ぎ、だんだん発散する。
show_trace(gd(1.1, f_grad), f)epoch 10, x: 61.91736412.3.1.2. Local Minima12.3.1.2 局所的な最小値
To illustrate what happens for nonconvex functions consider the case of for some constant . This function has infinitely many local minima. Depending on our choice of the learning rate and depending on how well conditioned the problem is, we may end up with one of many solutions. The example below illustrates how an (unrealistically) high learning rate will lead to a poor local minimum.
英語のヒント
非凸の関数で何が起こるかを示すため、ある定数についての場合を考えよう。この関数には局所的な最小値が無限にある。学習率の選び方と、問題の条件がどれだけよいかによって、多くの解の一つに行き着くかもしれない。下の例は、(非現実的なほど)高い学習率が、どのようにして悪い局所的な最小値につながるかを示している。
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)
show_trace(gd(2, f_grad), f)epoch 10, x: -1.528166