%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
sns.set_context('notebook')
解析的ではなく,反復プロセスによって解の見積もりを更新する方法によって数学的問題を解くアルゴリズムを紹介.
「ゼロから作るディープラーニング」第4章も参照.
計算機上で連続的な計算を実行する際の基本的な困難さは、有限長のビットパターンで無限に多くの実数を表現する必要があることである.これは,ほとんどすべての実数について,計算機内の数値を表すときに近似誤差が発生することを意味する.多くの場合,これはちょうど丸め誤差である.丸め誤差の累積を最小限に抑えるように設計されていないと,実際に多くの操作で混乱し,理論上で動作するアルゴリズムが失敗する可能性がある.
xi がある定数 c に等しいとする.
c = 10
x = np.full((1, 5), c)
x
/Users/ryo-t/.pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/numpy/core/numeric.py:301: FutureWarning: in the future, full((1, 5), 10) will return an array of dtype('int64') format(shape, fill_value, array(fill_value).dtype), FutureWarning)
array([[ 10., 10., 10., 10., 10.]])
np.exp(x)
array([[ 22026.46579481, 22026.46579481, 22026.46579481, 22026.46579481, 22026.46579481]])
# softmax(x)
np.exp(x) / np.exp(x).sum()
array([[ 0.2, 0.2, 0.2, 0.2, 0.2]])
🙆
c がデカイと exp(c) がオーバーフローし,softmax は ∞/∞ で undefined.
c = 10e2
x = np.full((1, 5), c)
x
array([[ 1000., 1000., 1000., 1000., 1000.]])
np.exp(x)
/Users/ryo-t/.pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/ipykernel/__main__.py:1: RuntimeWarning: overflow encountered in exp if __name__ == '__main__':
array([[ inf, inf, inf, inf, inf]])
# softmax(x)
np.exp(x) / np.exp(x).sum()
/Users/ryo-t/.pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/ipykernel/__main__.py:2: RuntimeWarning: overflow encountered in exp from ipykernel import kernelapp as app /Users/ryo-t/.pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/ipykernel/__main__.py:2: RuntimeWarning: invalid value encountered in true_divide from ipykernel import kernelapp as app
array([[ nan, nan, nan, nan, nan]])
🙀
c が小さいと exp(c) がアンダーフローし,softmax は 0/0 で undefined.
c = -10e2
x = np.full((1, 5), c)
x
array([[-1000., -1000., -1000., -1000., -1000.]])
np.exp(x)
array([[ 0., 0., 0., 0., 0.]])
# softmax(x)
np.exp(x) / np.exp(x).sum()
/Users/ryo-t/.pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/ipykernel/__main__.py:2: RuntimeWarning: invalid value encountered in true_divide from ipykernel import kernelapp as app
array([[ nan, nan, nan, nan, nan]])
🙀
c = 10e2
x = np.full((1, 5), c)
x
array([[ 1000., 1000., 1000., 1000., 1000.]])
z = x - x.max()
z
array([[ 0., 0., 0., 0., 0.]])
# softmax(z)
np.exp(z) / np.exp(z).sum()
array([[ 0.2, 0.2, 0.2, 0.2, 0.2]])
🙆
c = -10e2
x = np.full((1, 5), c)
x
array([[-1000., -1000., -1000., -1000., -1000.]])
z = x - x.max()
z
array([[ 0., 0., 0., 0., 0.]])
# softmax(z)
np.exp(z) / np.exp(z).sum()
array([[ 0.2, 0.2, 0.2, 0.2, 0.2]])
🙆
x = np.array([1, 2, 3, 4, 5])
z = x - x.max()
z
array([-4, -3, -2, -1, 0])
np.exp(x) / np.exp(x).sum(), np.exp(z) / np.exp(z).sum()
(array([ 0.01165623, 0.03168492, 0.08612854, 0.23412166, 0.63640865]), array([ 0.01165623, 0.03168492, 0.08612854, 0.23412166, 0.63640865]))
🙆
普通はこういうトリックが必要だが,この本で説明されているさまざまなアルゴリズムの実装に関わるすべての数値的な考慮事項は明示的に記述されていない.Theano など低レベルライブラリの開発者は,deep learning アルゴリズムを実装する際に数値問題を念頭に置くべき.でもこの本のほとんどの読者は,低レベルライブラリに頼ってればOK牧場.
conditioning: 入力のわずかな変化に対して,関数がどれほど急速に変化するかを指す.入力の丸め誤差が出力に大きな変化をもたらす可能性があるため,入力がわずかに動いたときに急激に変化する関数は科学的計算には問題がある.
関数 f(x)=A−1x を考える.A∈Rn×n が固有値を持つとき,condition number は
a = np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]])
a
array([[ 1, 0, -1], [ 0, 1, 0], [ 1, 0, 1]])
np.linalg.cond(a)
1.4142135623730951
a = np.diag([1, 10, 100])
a
array([[ 1, 0, 0], [ 0, 10, 0], [ 0, 0, 100]])
np.linalg.cond(a)
100.0
この値がデカイと,逆行列は入力の誤差を増幅する.実際には,誤差は逆行列自体の数値誤差によってさらに複合化される.
ほとんどの deep learning アルゴリズムには,ある種の最適化が必要.最適化とは,x を変更することによって,ある関数 f(x) を最小化または最大化するタスクのこと.f(x) を最小化するという点では,ほとんどの最適化の問題に言及する.
最小化または最大化したい関数は目的関数 (objective function) または最適化基準 (criterion) と呼ばれる.最小化するときには,コスト関数 (cost function),損失関数 (loss function),またはエラー関数 (error function) とも呼ばれる. 本書では、これらの用語を同じ意味で使用していますが、一部の機械学習の出版物では、これらの用語のいくつかに特別な意味が割り当てられています。
読者はすでに微積分に精通していると仮定し,微積分の概念が最適化にどのように関係しているかを簡単に解説する.
def numerical_diff(f, x):
h = 1e-4
return (f(x+h) - f(x-h)) / (2*h)
def function_1(x):
return 0.01*x**2 + 0.1*x
def tangent_line(f, x):
d = numerical_diff(f, x)
y = f(x) - d*x
return lambda t: d*t + y
x = np.arange(0.0, 20.0, 0.1)
y = function_1(x)
plt.xlabel("x")
plt.ylabel("f(x)")
tf = tangent_line(function_1, 5)
y2 = tf(x)
plt.plot(x, y)
plt.plot(x, y2)
[<matplotlib.lines.Line2D at 0x11ba486a0>]
def function_2(x):
if x.ndim == 1:
return np.sum(x**2)
else:
return np.sum(x**2, axis=1)
def function_tmp1(x0):
return x0**2 + 0**2 # x_1^2 は定数なので何でもいいし結果も変わらない
numerical_diff(function_tmp1, 3.0)
6.000000000012662
def function_tmp1(x1):
return 0**2 + x1**2 # x_0^2 は定数なので何でもいいし結果も変わらない
numerical_diff(function_tmp1, 4.0)
7.999999999999119
x0 と x1 の偏微分をまとめて計算したいとする.(x0,x1) の両方の偏微分をまとめて,(∂f∂x0,∂f∂x1) として計算することを考える.(∂f∂x0,∂f∂x1) のように,すべての変数の偏微分をベクトルとしてまとめたものを勾配 (gradient) と呼ぶ.
def numerical_gradient(f, x):
h = 1e-4 # 0.0001
grad = np.zeros_like(x) # x と同じ形状の零ベクトル
for idx in range(x.size):
tmp_val = x[idx]
# f(x+h) の計算
x[idx] = float(tmp_val) + h
fxh1 = f(x)
# f(x-h) の計算
x[idx] = tmp_val - h
fxh2 = f(x)
grad[idx] = (fxh1 - fxh2) / (2*h)
x[idx] = tmp_val # 値を元に戻す
return grad
点 (3,4) での勾配:
numerical_gradient(function_2, np.array([3.0, 4.0]))
array([ 6., 8.])
勾配をベクトルだと思っていろんな点で求めて図示すると:
def _numerical_gradient_no_batch(f, x):
h = 1e-4 # 0.0001
grad = np.zeros_like(x) # x と同じ形状の零ベクトル
for idx in range(x.size):
tmp_val = x[idx]
# f(x+h) の計算
x[idx] = float(tmp_val) + h
fxh1 = f(x)
# f(x-h) の計算
x[idx] = tmp_val - h
fxh2 = f(x)
grad[idx] = (fxh1 - fxh2) / (2*h)
x[idx] = tmp_val # 値を元に戻す
return grad
def numerical_gradient(f, X):
if X.ndim == 1:
return _numerical_gradient_no_batch(f, X)
else:
grad = np.zeros_like(X)
for idx, x in enumerate(X):
grad[idx] = _numerical_gradient_no_batch(f, x)
return grad
x0 = np.arange(-2, 2.5, 0.25)
x1 = np.arange(-2, 2.5, 0.25)
X, Y = np.meshgrid(x0, x1)
X = X.flatten()
Y = Y.flatten()
grad = numerical_gradient(function_2, np.array([X, Y]))
plt.figure()
plt.quiver(X, Y, -grad[0], -grad[1], angles="xy", color="#666666")
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.xlabel('x0')
plt.ylabel('x1')
<matplotlib.text.Text at 0x11baed860>
各地点において関数の値を最も減らす方向を示すのが勾配.
機械学習の問題の多くは,学習の際に最適なパラメータを探索する.最適なパラメータとは,損失関数が最小値を取るときのパラメータの値.勾配をうまく利用して関数の最小値を探すのが勾配法.
ただし,各地点において関数の値を最も減らす方向を示すのが勾配であるため,勾配が指す先が本当に関数の最小値なのかどうかは保証されない.
より一般的に書くと
η (ϵ) は学習率 (learning rate) と呼ばれる.ニューラルネットワークの学習においては,学習率の値を変更しながら,正しく学習できているかどうか,確認作業を行うのが一般的.
def gradient_descent(f, init_x, lr=0.01, step_num=100):
x = init_x
x_history = []
for i in range(step_num):
x_history.append(x.copy())
grad = numerical_gradient(f, x)
x -= lr * grad
return x, np.array(x_history)
初期値 (−3.0,4.0),学習率 η=0.1:
init_x = np.array([-3.0, 4.0])
lr = 0.1
step_num = 20
x, x_history = gradient_descent(function_2, init_x, lr=lr, step_num=step_num)
plt.plot([-5, 5], [0,0], '--b')
plt.plot([0,0], [-5, 5], '--b')
plt.plot(x_history[:,0], x_history[:,1], 'o')
plt.xlim(-3.5, 3.5)
plt.ylim(-4.5, 4.5)
plt.xlabel("X0")
plt.ylabel("X1")
<matplotlib.text.Text at 0x11c55b128>
🙆
初期値 (−3.0,4.0),学習率 η=0.01:
init_x = np.array([-3.0, 4.0])
lr = 0.01
step_num = 20
x, x_history = gradient_descent(function_2, init_x, lr=lr, step_num=step_num)
plt.plot([-5, 5], [0,0], '--b')
plt.plot([0,0], [-5, 5], '--b')
plt.plot(x_history[:,0], x_history[:,1], 'o')
plt.xlim(-3.5, 3.5)
plt.ylim(-4.5, 4.5)
plt.xlabel("X0")
plt.ylabel("X1")
<matplotlib.text.Text at 0x11c6d4438>
小さすぎた 🙀
初期値 (−3.0,4.0),学習率 η=1.0:
init_x = np.array([-3.0, 4.0])
lr = 1.0
step_num = 20
x, x_history = gradient_descent(function_2, init_x, lr=lr, step_num=step_num)
plt.plot([-5, 5], [0,0], '--b')
plt.plot([0,0], [-5, 5], '--b')
plt.plot(x_history[:,0], x_history[:,1], 'o')
plt.xlim(-3.5, 3.5)
plt.ylim(-4.5, 4.5)
plt.xlabel("X0")
plt.ylabel("X1")
<matplotlib.text.Text at 0x11c81f080>
デカすぎた 🙀
line search では幾つかの ϵ に対して f(x−ϵ∇xf(x)) を計算して,目的関数が最小となるような ϵ を選ぶ.
A = np.eye(2)
b = np.array([1, 1]).reshape((2, 1))
def f(x):
if x.shape[1] == 1:
return np.linalg.norm(A.dot(x) - b)**2 / 2
return np.linalg.norm(A.dot(x) - b, axis=0)**2 / 2
step_size = 0.01
tolerance = 0.01
x = np.array([[2.0], [2.0]])
history = []
grad = A.T.dot(A).dot(x) - A.T.dot(b)
while np.linalg.norm(grad) > tolerance:
history.append(x.copy())
x -= step_size * grad
grad = A.T.dot(A).dot(x) - A.T.dot(b)
len(history)
493
history[-1]
(array([[ 1.00712059], [ 1.00712059]]), 493)
x = [1, 1] で f(x) が最小なのでOK牧場.