「.NET 開発基盤部会 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
学習フェーズでは、信号は逆方向に伝播する。
人間が(暗黙的な学習によって、)これらを判別することはできる。
従って、人間の脳も、一部は、このようなデータ駆動で動いているのかもしれない。
入力画像データから、本質的なデータを抽出できるように設計された変換器を指す。
ベクトル化された画像データを機械学習のSVMやKNNなどの識別器で学習させる。
特徴量についても機会が学習する。
ニューラルネットワークの出力と正解となる教師データの各要素の差の二乗の総和の2分の一。
2 E = 1/2 Σ (yk-tk) k
"""This is a test program.""" import numpy as np def mean_squared_error(yk, tk): """損失関数(2乗和誤差)""" return 0.5 * np.sum((yk - tk)**2) tk = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0]) yk = np.array([0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]) print(mean_squared_error(yk, tk)) yk = np.array([0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]) print(mean_squared_error(yk, tk))
0.0975
0.5975
E = - Σ tk log yk k
"""This is a test program.""" import numpy as np def mean_squared_error(yk, tk): """損失関数(交差エントロピー誤差)""" delta = 1e-7 # log(0)はマイナス∞になるのを微小な値を足して防止。 return -np.sum(tk * np.log(yk + delta)) tk = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0]) yk = np.array([0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]) print(mean_squared_error(yk, tk)) yk = np.array([0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]) print(mean_squared_error(yk, tk))
0.510825457099
2.30258409299
E = -1/N ΣΣ tnk log ynk n k
"""This is a test program.""" import numpy as np def mean_squared_error(ynk, tnk): """損失関数(交差エントロピー誤差)""" print("tnk:" + str(tnk)) print("ynk:" + str(ynk)) batch_size = ynk.shape[0] print("batch_size:" + str(batch_size)) delta = 1e-7 # log(0)はマイナス∞になるのを微小な値を足して防止。 return - 1 / batch_size * (np.sum(tnk * np.log(ynk + delta))) TNK = np.array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 0], \ [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]]) YNK = np.array([[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0], \ [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]]) print("mean_squared_error:" + str(mean_squared_error(YNK, TNK))) YNK = np.array([[0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0], \ [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]]) print("mean_squared_error:" + str(mean_squared_error(YNK, TNK)))
"""This is a test program.""" import numpy as np def mean_squared_error(ynk, tnk): """損失関数(交差エントロピー誤差)""" print("tnk:" + str(tnk)) print("ynk:" + str(ynk)) batch_size = ynk.shape[0] print("batch_size:" + str(batch_size)) delta = 1e-7 # log(0)はマイナス∞になるのを微小な値を足して防止。 ynk = ynk + delta print("arange:" + str(ynk[np.arange(batch_size), tnk])) return - 1 / batch_size * (np.sum(np.log(ynk[np.arange(batch_size), tnk]))) TNK = np.array([2, 2]) YNK = np.array([[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0], \ [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]]) print("mean_squared_error:" + str(mean_squared_error(YNK, TNK))) YNK = np.array([[0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0], \ [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]]) print("mean_squared_error:" + str(mean_squared_error(YNK, TNK)))
勾配法では、(パラメタの微分を計算して得た)勾配の情報を使って進む方向を決める。
ある瞬間の変化の量(=グラフの接線の傾き的な)。
df(x) yの増量 f(x+h) - f(x) f(x+h) - f(x) ── = ─── = lim ────── = lim ────── dx xの増量 h→0 (x+h) - (x) h→0 h
式の展開によって微分を求める(誤差がない)。
2 y = x
df(x) yの増量 (x+h)^2 - x^2 (x^2+2hx+h^2) - (x^2) ── = ─── = lim ────── = lim ────────── dx xの増量 h→0 (x+h) - (x) h→0 h
dy 2hx+h^2 ── = lim ──── = lim 2x+h = 2x dx h→0 h h→0
計算によって微分を求める(誤差がある)。
"""This is a test program.""" import numpy as np import matplotlib.pyplot as plt def numerical_diff(f, x): """数値微分""" h = 1e-4 # 微小な値hとして1の-4乗を用いる return (f(x + h) - f(x - h)) / (2 * h) # 前方差分から中心差分にして誤差減 def function_1(x): """f(x)=0.01x^2+0.1x""" return 0.01 * x ** 2 + 0.1 * x X=5 print("X=" + str(X) + " : " + str(numerical_diff(function_1, X))) X=10 print("X=" + str(X) + " : " + str(numerical_diff(function_1, X))) X = np.arange(0.0, 20.0, 0.1) Y = function_1(X) plt.xlabel("x") plt.ylabel("f(x)") plt.plot(X, Y) plt.show()
#ref(): File not found: "graf.png" at page "ニューラルネットワーク(学習)"
X=5 : 0.1999999999990898 X=10 : 0.2999999999986347
#ref(): File not found: "gradient_1d.png" at page "ニューラルネットワーク(学習)"
2 2 f(x0, x1) = x0 + x1
"""This is a test program.""" import numpy as np def function_2(x): """f(x0, x1) = x0^2 + x1^2""" return x[0] ** 2 + x[1] ** 2 # or return np.sum(x ** 2)
df(x0, x1) ───── = 2 * x0 dx0 df(x0, x1) ───── = 2 * x1 dx1
numerical_diff"""This is a test program.""" import numpy as np def numerical_diff(f, x): """数値微分""" h = 1e-4 # 微小な値hとして1の-4乗を用いる return (f(x + h) - f(x - h)) / (2 * h) # 前方差分から中心差分にして誤差減 def function_tmp1(x0): """x0=3, x1=4 の場合の x0 に対する偏微分用""" return x0 ** 2 + 4.0 ** 2 def function_tmp2(x1): """x0=3, x1=4 の場合の x1 に対する偏微分用""" return 3.0 ** 2.0 + x1 ** 2 print(numerical_diff(function_tmp1, 3.0)) print(numerical_diff(function_tmp2, 4.0))
6.00000000000378 (2 * x0 = 2 * 3 = 6) 7.999999999999119 (2 * x1 = 2 * 4 = 8)
勾配の示す方向は、各場所で関数の値を最も減らす方向。
df(x0, x1) df(x0, x1) ───── , ───── dx0 dx1
"""This is a test program.""" import numpy as np def numerical_gradient(f, x01): """偏微分""" h = 1e-4 # 微小な値hとして1の-4乗を用いる grad = np.zeros_like(x01) # x01と同じ形状で要素が0。 for idx in range(x01.size): tmp_val = x01[idx] # 前方差分から中心差分にして誤差減 # f(x + h) fxh1 = f(tmp_val + h) # f(x - h) fxh2 = f(tmp_val - h) # (f(x + h) - f(x - h)) / 2 * h grad[idx] = (fxh1 - fxh2) / (2 * h) return grad def function_2(x): return np.sum(x**2) print(numerical_gradient(function_2, np.array([3.0, 4.0]))) print(numerical_gradient(function_2, np.array([0.0, 2.0]))) print(numerical_gradient(function_2, np.array([3.0, 0.0])))
[ 6. 8.] [ 0. 4.] [ 6. 0.]
#ref(): File not found: "gradient_2d.png" at page "ニューラルネットワーク(学習)"