プリンキピア

第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 𝐱=[x1,x2,…,xd]⊤. That is, the objective function f:ℝd→ℝ maps vectors into scalars. Correspondingly its gradient is multivariate, too. It is a vector consisting of d partial derivatives:
英語のヒント
1変数の場合の直観がよくわかったので、𝐱=[x1,x2,…,xd]⊤の状況を考えよう。つまり、目的関数f:ℝd→ℝはベクトルをスカラーに写す。それに応じて、その勾配も多変数である。それは、次のような、d個の偏導関数からなるベクトルである。
∇f(𝐱)=[∂f(𝐱)∂x1,∂f(𝐱)∂x2,…,∂f(𝐱)∂xd]⊤.(12.3.5)
Each partial derivative element ∂f(𝐱)/∂xi in the gradient indicates the rate of change of f at 𝐱 with respect to the input xi. 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
英語のヒント
勾配の各偏導関数の要素∂f(𝐱)/∂xiは、入力xiについての、𝐱でのfの変化率を示す。前の1変数の場合と同じように、多変数関数に対応するテイラー近似を使って、何をすべきかの見当をつけられる。特に、次が成り立つ。
f(𝐱+𝝐)=f(𝐱)+𝝐⊤∇f(𝐱)+𝒪(∥𝝐∥2).(12.3.6)
In other words, up to second-order terms in 𝝐 the direction of steepest descent is given by the negative gradient −∇f(𝐱). Choosing a suitable learning rate η>0 yields the prototypical gradient descent algorithm:
英語のヒント
言い換えると、𝝐の2次の項までを除けば、最急降下の向きは負の勾配−∇f(𝐱)で与えられる。適切な学習率η>0を選ぶと、典型的な勾配降下法のアルゴリズムが得られる。
𝐱←𝐱−η∇f(𝐱).(12.3.7)
To see how the algorithm behaves in practice let’s construct an objective function f(𝐱)=x12+2x22 with a two-dimensional vector 𝐱=[x1,x2]⊤ as input and a scalar as output. The gradient is given by ∇f(𝐱)=[2x1,4x2]⊤. We will observe the trajectory of 𝐱 by gradient descent from the initial position [−5,−2].
英語のヒント
このアルゴリズムが実際にどうふるまうかを見るため、2次元のベクトル𝐱=[x1,x2]⊤を入力とし、スカラーを出力とする目的関数f(𝐱)=x12+2x22を作ろう。勾配は∇f(𝐱)=[2x1,4x2]⊤で与えられる。初期位置[−5,−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 η=0.1. We can see that after 20 steps the value of 𝐱 approaches its minimum at [0,0]. Progress is fairly well-behaved albeit rather slow.
英語のヒント
次に、学習率η=0.1のときの最適化変数𝐱の軌跡を観察する。20ステップの後、𝐱の値が[0,0]にある最小値に近づくのがわかる。進み方は、かなり遅いとはいえ、おおむね行儀がよい。
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