第12章最適化アルゴリズム
12.3.2. Multivariate Gradient Descent12.3.2 多変数の勾配降下法
Now that we have a better intuition of the univariate case, let’s consider the situation where . That is, the objective function maps vectors into scalars. Correspondingly its gradient is multivariate, too. It is a vector consisting of partial derivatives:
英語のヒント
1変数の場合の直観がよくわかったので、の状況を考えよう。つまり、目的関数はベクトルをスカラーに写す。それに応じて、その勾配も多変数である。それは、次のような、個の偏導関数からなるベクトルである。
(12.3.5)
Each partial derivative element in the gradient indicates the rate of change of at with respect to the input . As before in the univariate case we can use the corresponding Taylor approximation for multivariate functions to get some idea of what we should do. In particular, we have that
英語のヒント
勾配の各偏導関数の要素は、入力についての、でのの変化率を示す。前の1変数の場合と同じように、多変数関数に対応するテイラー近似を使って、何をすべきかの見当をつけられる。特に、次が成り立つ。
(12.3.6)
In other words, up to second-order terms in the direction of steepest descent is given by the negative gradient . Choosing a suitable learning rate yields the prototypical gradient descent algorithm:
英語のヒント
言い換えると、の2次の項までを除けば、最急降下の向きは負の勾配で与えられる。適切な学習率を選ぶと、典型的な勾配降下法のアルゴリズムが得られる。
(12.3.7)
To see how the algorithm behaves in practice let’s construct an objective function with a two-dimensional vector as input and a scalar as output. The gradient is given by . We will observe the trajectory of by gradient descent from the initial position .
英語のヒント
このアルゴリズムが実際にどうふるまうかを見るため、2次元のベクトルを入力とし、スカラーを出力とする目的関数を作ろう。勾配はで与えられる。初期位置から、勾配降下法によるの軌跡を観察する。
To begin with, we need two more helper functions. The first uses an update function and applies it 20 times to the initial value. The second helper visualizes the trajectory of .
英語のヒント
まず、補助関数があと二つ要る。一つ目は、更新関数を使い、それを初期値に20回当てはめる。二つ目の補助関数は、の軌跡を可視化する。
def train_2d(trainer, steps=20, f_grad=None): #@save
"""Optimize a 2D objective function with a customized trainer."""
# `s1` and `s2` are internal state variables that will be used in Momentum, adagrad, RMSProp
x1, x2, s1, s2 = -5, -2, 0, 0
results = [(x1, x2)]
for i in range(steps):
if f_grad:
x1, x2, s1, s2 = trainer(x1, x2, s1, s2, f_grad)
else:
x1, x2, s1, s2 = trainer(x1, x2, s1, s2)
results.append((x1, x2))
print(f'epoch {i + 1}, x1: {float(x1):f}, x2: {float(x2):f}')
return results
def show_trace_2d(f, results): #@save
"""Show the trace of 2D variables during optimization."""
d2l.set_figsize()
d2l.plt.plot(*zip(*results), '-o', color='#ff7f0e')
x1, x2 = torch.meshgrid(torch.arange(-5.5, 1.0, 0.1),
torch.arange(-3.0, 1.0, 0.1), indexing='ij')
d2l.plt.contour(x1, x2, f(x1, x2), colors='#1f77b4')
d2l.plt.xlabel('x1')
d2l.plt.ylabel('x2')Next, we observe the trajectory of the optimization variable for learning rate . We can see that after 20 steps the value of approaches its minimum at . Progress is fairly well-behaved albeit rather slow.
英語のヒント
次に、学習率のときの最適化変数の軌跡を観察する。20ステップの後、の値がにある最小値に近づくのがわかる。進み方は、かなり遅いとはいえ、おおむね行儀がよい。
def f_2d(x1, x2): # Objective function
return x1 ** 2 + 2 * x2 ** 2
def f_2d_grad(x1, x2): # Gradient of the objective function
return (2 * x1, 4 * x2)
def gd_2d(x1, x2, s1, s2, f_grad):
g1, g2 = f_grad(x1, x2)
return (x1 - eta * g1, x2 - eta * g2, 0, 0)
eta = 0.1
show_trace_2d(f_2d, train_2d(gd_2d, f_grad=f_2d_grad))epoch 20, x1: -0.057646, x2: -0.000073