「.NET 開発基盤部会 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
(x1) --------- w1 --------> (y) ↑ (x2) --------- w2 -----------┘ 入力信号 固有の重み 出力信号
┌ │0 ( x1 w1 + x2 w2 <= θ ) y = < │1 ( x1 w1 + x2 w2 > θ ) └
┌ │0 ( -θ + x1 w1 + x2 w2 <= 0 ) y = < │1 ( -θ + x1 w1 + x2 w2 > 0 ) └ ┌ │0 ( b + x1 w1 + x2 w2 <= 0 ) y = < │1 ( b + x1 w1 + x2 w2 > 0 ) └
-θ= bはバイアスと呼ばれ、w, θ同様に、「重み」と呼ぶことがある。
((1))--------- b -----------┐ ↓ (x1) --------- w1 --------> (y) ↑ (x2) --------- w2 -----------┘ 入力信号 固有の重み 出力信号
a = b + x1 w1 + x2 w2
┌ │0 ( a <= 0 ) y = h(a) = < │1 ( a > 0 ) └
((1))--------- b -----------┐ ↓ h() (x1) --------- w1 --------> ((a) ---> (y)) ↑ (x2) --------- w2 -----------┘ 入力信号 固有の重み 出力信号
機械学習では下のパラメタ(w1, w2, θ(-b))を決める作業をコンピュータに行わせる。
下記のANDゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。
x1 x2 y ──────┼─── 0 0 │ 0 ──────┼─── 1 0 │ 0 ──────┼─── 0 1 │ 0 ──────┼─── 1 1 │ 1
│ 0 ① │ │ ─0──0───── │
下記のNANDゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。
x1 x2 y ──────┼─── 0 0 │ 1 ──────┼─── 1 0 │ 1 ──────┼─── 0 1 │ 1 ──────┼─── 1 1 │ 0
(ANDゲートのパラメタの符号を反転すればOK)
│ ① 0 │ │ ─①──①───── │
下記のNANDゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。
x1 x2 y ──────┼─── 0 0 │ 0 ──────┼─── 1 0 │ 1 ──────┼─── 0 1 │ 1 ──────┼─── 1 1 │ 1
│ ① ① │ │ ─0──①───── │
下記のXORゲートを満たす1層のパーセプトロンを作ることはできない。
x1 x2 y ──────┼─── 0 0 │ 0 ──────┼─── 1 0 │ 1 ──────┼─── 0 1 │ 1 ──────┼─── 1 1 │ 0
理由は、以下の縦軸x1、横軸x2のプロットは、
非線形領域のため、閾値の直線を引けないため。
│ ① 0 │ │ ─0──①───── │
しかし、前述のAND、NADN、ORのゲートを
組みわせた多層のパーセプトロンであればXORゲートを作ることができる。
例えば、
を使用すると、
第0層 第1層 第2層 x1 x2 s1 s2 y ──────┼──────┼─── 0 0 │ 1 0 │ 0 ──────┼──────┼─── 1 0 │ 1 1 │ 1 ──────┼──────┼─── 0 1 │ 1 1 │ 1 ──────┼──────┼─── 1 1 │ 0 1 │ 0
と多層(2層)パーセプトロンとしてXORゲートを作ることができる。
ここから、VSCodeでPythonファイルに書いた。
"""This is a test program.""" def and_method(x_1, x_2): """This is a test program.""" w_1, w_2, theta = 0.5, 0.5, 0.7 tmp = x_1 * w_1 + x_2 * w_2 if tmp <= theta: return 0 else: return 1 print(and_method(0, 0)) print(and_method(1, 0)) print(and_method(0, 1)) print(and_method(1, 1))
w, θを以下のようにする。
w_1, w_2, theta = -0.5, -0.5, -0.7
w, θを以下のようにする。
w_1, w_2, theta = 0.5, 0.5, 0.3
"""This is a test program.""" import numpy as np def and_method(x_1, x_2): """This is a test program.""" x = np.array([x_1, x_2]) w = np.array([0.5, 0.5]) b = -0.7 tmp = b + np.sum(x*w) if tmp <= 0: return 0 else: return 1 print(and_method(0, 0)) print(and_method(1, 0)) print(and_method(0, 1)) print(and_method(1, 1))
w, bを以下のようにする。
w = np.array([-0.5, -0.5]) b = 0.7
w, bを以下のようにする。
w = np.array([0.5, 0.5]) b = -0.3
多層パーセプトロンとして以下のように実装できる。
"""This is a test program.""" import numpy as np def and_method(x_1, x_2): """This is a test program.""" x = np.array([x_1, x_2]) w = np.array([0.5, 0.5]) b = -0.7 tmp = b + np.sum(x*w) if tmp <= 0: return 0 else: return 1 def nand_method(x_1, x_2): """This is a test program.""" x = np.array([x_1, x_2]) w = np.array([-0.5, -0.5]) b = 0.7 tmp = b + np.sum(x*w) if tmp <= 0: return 0 else: return 1 def or_method(x_1, x_2): """This is a test program.""" x = np.array([x_1, x_2]) w = np.array([0.5, 0.5]) b = -0.3 tmp = b + np.sum(x*w) if tmp <= 0: return 0 else: return 1 def xor_method(x_1, x_2): """This is a test program.""" s_1 = nand_method(x_1, x_2) s_2 = or_method(x_1, x_2) return and_method(s_1, s_2) print(xor_method(0, 0)) print(xor_method(1, 0)) print(xor_method(0, 1)) print(xor_method(1, 1))