アダム(Adam)

Adamとは

Adamの読み方は”アダム”もしくは”アーダム”です。こちらのパラメータの最適化手法の数式については、「0からDeepLearning」には紹介されていません。その代わりに、Adamがどんなものかをわかりやすく説明しています。
ザックリと紹介すると、モーメンタム(Momentum)エーダグラッド(AdaGrad)を融合させたもの。という内容です。
Momentumのお椀を転がるような動きと、AdaGradのy軸の最小値が少ない回数で決まる動きを掛け合わせたものとなっています。

Adamのサンプルプログラム

「0からDeepLearning」に紹介されているAdamのプログラムがこちらです。
確かにコードもMomentumやAdaGradで見たことがある記述が並びます。パラメータが増えてややこしくなっている感じです。

class Adam:
    def __init__(self, lr=0.001, beta1=0.9, beta2=0.999):
        self.lr = lr
        self.beta1 = beta1
        self.beta2 = beta2
        self.iter = 0
        self.m = None
        self.v = None
        
    def update(self, params, grads):
        if self.m is None:
            self.m, self.v = {}, {}
            for key, val in params.items():
                self.m[key] = np.zeros_like(val)
                self.v[key] = np.zeros_like(val)
        
        self.iter += 1
        lr_t  = self.lr * np.sqrt(1.0 - self.beta2**self.iter) / (1.0 - self.beta1**self.iter)         
        
        for key in params.keys():
            self.m[key] += (1 - self.beta1) * (grads[key] - self.m[key])
            self.v[key] += (1 - self.beta2) * (grads[key]**2 - self.v[key])
            params[key] -= lr_t * self.m[key] / (np.sqrt(self.v[key]) + 1e-7)

Adamのグラフ

最後にグラフがどのように表示されるのかをご紹介します。
このグラフは、以下のプログラムを実行した結果です。
モーメンタム(Momentum)を見たあとに、Adamを見たらこのパラメータ最適化法が一番良い!と思ったことでしょう・・・。
しかし、y軸が少ない回数で決まるエーダグラッド(AdaGrad)を知ってからは・・・AdaGradの方が効率が良い気がします。
Adamは、この2つを組み合わせた新しい最適化手法ということで紹介されていましたがAdaGradだと、どういう問題があったのだろう?と推測したときに、なんとなく想像しました。
AdaGradほど早くy軸を確定させずに、もっと緩やかに判定したい場合があったのではないだろうか?と・・・
例えば、判断の途中でも結果を出したい場合。いつ時点の判断はコレでした!を導き出したい場合は、Adamが良い気がします(超感覚値w)

import sys, os
sys.path.append(os.pardir)
import matplotlib.pyplot as plt
from collections import OrderedDict
from common.optimizer import *
def f(x, y):
    return x ** 2 / 20.0 + y ** 2
def df(x, y):
    return x / 10.0, 2.0 * y
init_pos = (-7.0, 2.0)
params = {}
params['x'], params['y'] = init_pos[0], init_pos[1]
grads = {}
grads['x'], grads['y'] = 0, 0
optimizers = OrderedDict()
optimizers["Adam"] = Adam(lr=0.3)
idx = 1
for key in optimizers:
    optimizer = optimizers[key]
    x_history = []
    y_history = []
    params['x'], params['y'] = init_pos[0], init_pos[1]
    for i in range(10000):
        x_history.append(params['x'])
        y_history.append(params['y'])
        grads['x'], grads['y'] = df(params['x'], params['y'])
        optimizer.update(params, grads)
    x = np.arange(-10, 10, 0.01)
    y = np.arange(-5, 5, 0.01)
    X, Y = np.meshgrid(x, y)
    Z = f(X, Y)
    # for simple contour line
    mask = Z > 7
    Z[mask] = 0
    # plot
    plt.subplot(1, 1, idx)
    idx += 1
    plt.plot(x_history, y_history, 'o-', color="red")
    plt.contour(X, Y, Z)
    plt.ylim(-10, 10)
    plt.xlim(-10, 10)
    plt.plot(0, 0, '+')
    # colorbar()
    # spring()
    plt.title(key)
    plt.xlabel("x")
    plt.ylabel("y")
plt.show()
引用元

オライリーJapan 斎藤康毅 著 「セロから作るDeep Learning」

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny