<目次>
(1) ディープラーニングのパーセプトロンとは?Pythonで実装したサンプルもご紹介
(1-1) パーセプトロンとは?
(1-2) 処理の流れ
(1-3) サンプルプログラム
(1-4) 補足:Excelでの検算
(1) ディープラーニングのパーセプトロンとは?Pythonで実装したサンプルもご紹介
(1-1) パーセプトロンとは?
(1-2) 処理の流れ
(図121)
(1-3) サンプルプログラム
(サンプルプログラム)
import numpy as np # グローバル変数 # d:データの次元 d = 2 # N:データ数(パターン別。発火有り/無しそれぞれN個) N = 10 # 発火するニューロンの平均値 mean = 5 # パラメータwの初期化 # ⇒「np.zeros(n)」で0を要素とするn要素の配列を作成 w = np.zeros(d) # dw1,dw2,...dwn,dthetaの定義 # ⇒ w1,w2,...wn,thetaの初期値は適当な値からスタート可。 dw = np.zeros(d) # ランダムデータの生成(ただし、1パターンにデータ固定) def create_random_data(d,N,mean): rng = np.random.RandomState(123) x1 = rng.randn(N,d) + np.array([0,0]) x2 = rng.randn(N,d) + np.array([mean,mean]) x = np.concatenate((x1,x2), axis=0) return x # ステップ関数 def step(x): return 1 * (x >= 0) # 正解値「t」の導出関数 # ⇒i:データの番号。0~N-1番目までは発火しない(0)データ。以降は発火する(1)データ。 def t(i): if i < N: return 0 else: return 1 def main(): # ■変数設定 ------------------------------------------------------------- # x1,x2,tの設定 # ⇒ゲートの種類に応じて、誤り訂正学習法で使える「データ数回(今回は20))」のパターンを設定 x = create_random_data(d,N,mean) # パラメータthetaの初期化 theta,dtheta = 0,0 # ■繰り返し学習処理 ------------------------------------------------------------- # dw1,dw2,...dwn,dthetaが「データ数回(今回は20)」連続で0になるまで繰り返し # ⇒ゲートの「データ数回(今回は20))」個のパターン全部でデルタ=0なら収束と判断 # delta_zero_flag: 現在のデータ(x1,x2,...xn)に対してdw1,dw2,...dwn,dthetaが0か?の管理フラグ delta_zero_flag = False # i1: ループ回数、i2: 現在のデータ数(何番目のデータで計算中か) i1,i2,end_counter = 0,0,0 # end_counter: デルタ(dw1,dw2,...dwn,dtheta)=ゼロがデータの個数(len(x))回続くまで while end_counter < len(x): # if: 初回は1番目のデータ(i2=0) if i1 == 0: i2 = 0 # elif: 2回目以降でlen(x)-1の場合は、セットの最後のデータのため、0リセット elif i2 != 0 and i2 == len(x)-1: i2 = 0 # else: 次のデータを使うためi2加算 else: i2 = i2 + 1 # ■w1,w2,...wn,thetaの設定 ------------------------------------------------ # w1,w2,...wn,thetaの初期値は適当な値からスタート可。 # 初回以外はデルタ値(dw1,dw2,...dwn,dtheta)を加算して更新 for j1 in range(d): w[j1] = w[j1] + dw[j1] theta = theta + dtheta # ■yの計算(モデルの出力)-------------------------------------------------- # 関数「y = f(w*x - theta)」の表現 y = step(np.dot(w, x[i2]) - theta) # ■dw1,dw2,dthetaの計算 -------------------------------------------------- # モデルの出力(y)と正解の出力(t)の差を用いて計算する。 for k1 in range(d): dw[k1] = (t(i2)-y)*x[i2][k1] dtheta = (-1)*(t(i2)-y) # ■ループ脱出判定 --------------------------------------------------------- # ゲートの「データ数回(今回は20)」パターン(x1,x2,...xn,tの組み合わせ)全てで # 修正分(dw1,dw2,...xn,dtheta)が0になった場合に脱出する # ------------------------------------------------------------------------ # (1)最初にFlagをTrueにしておく delta_zero_flag = True # (2)dw1,dw2,..dwnと確認し、1つでも0でないならフラグをFalseにする(処理を継続する) for l1 in range(d): if dw[l1] != 0: delta_zero_flag = False # (3)dthetaを確認し、0ならフラグをFalseにする if dtheta != 0: delta_zero_flag = False # (4)上記で1セット分(dw1,dw2,...,dwn,dtheta)なので、 # この時点でdelta_zero_flag == Trueなら1回分のカウントする if delta_zero_flag == True: end_counter = end_counter + 1 # (5)ただし、最後のセットで、end_counter < [データ数]なら # 全部が0でない(収束していない)として、end_counterをリセット if i2 == len(x)-1 and end_counter < len(x): end_counter = 0 print("k= ",i1,"x1 =",'{:.2f}'.format(x[i2][0]),"x2 =",'{:.2f}'.format(x[i2][1]),"t =",t(i2),"w1 =",'{:.2f}'.format(w[0]),"w2 =",'{:.2f}'.format(w[1]),"theta =",theta,"y =",'{:.2f}'.format(y),"dw1 =",'{:.2f}'.format(dw[0]),"dw2 =",'{:.2f}'.format(dw[1]),"dtheta =",dtheta,"end=",i2) # ループ変数を更新 i1 = i1 + 1 if __name__ == "__main__": main()
(実行結果例)
k= 0 x1 = -1.09 x2 = 1.00 t = 0 w1 = 0.00 w2 = 0.00 theta = 0 y = 1.00 dw1 = 1.09 dw2 = -1.00 dtheta = 1 end= 0 k= 1 x1 = 0.28 x2 = -1.51 t = 0 w1 = 1.09 w2 = -1.00 theta = 1 y = 1.00 dw1 = -0.28 dw2 = 1.51 dtheta = 1 end= 1 k= 2 x1 = -0.58 x2 = 1.65 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = -0.00 dw2 = 0.00 dtheta = 0 end= 2 k= 3 x1 = -2.43 x2 = -0.43 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 3 k= 4 x1 = 1.27 x2 = -0.87 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 4 k= 5 x1 = -0.68 x2 = -0.09 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 5 k= 6 x1 = 1.49 x2 = -0.64 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 6 k= 7 x1 = -0.44 x2 = -0.43 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 7 k= 8 x1 = 2.21 x2 = 2.19 t = 0 w1 = 0.80 w2 = 0.51 theta = 2 y = 1.00 dw1 = -2.21 dw2 = -2.19 dtheta = 1 end= 8 k= 9 x1 = 1.00 x2 = 0.39 t = 0 w1 = -1.40 w2 = -1.68 theta = 3 y = 0.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 9 k= 10 x1 = 5.74 x2 = 6.49 t = 1 w1 = -1.40 w2 = -1.68 theta = 3 y = 0.00 dw1 = 5.74 dw2 = 6.49 dtheta = -1 end= 10 k= 11 x1 = 4.06 x2 = 6.18 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 11 k= 12 x1 = 3.75 x2 = 4.36 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 12 k= 13 x1 = 5.91 x2 = 3.57 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 13 k= 14 x1 = 4.86 x2 = 4.14 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 14 k= 15 x1 = 4.74 x2 = 2.20 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 15 k= 16 x1 = 3.23 x2 = 4.30 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 16 k= 17 x1 = 5.93 x2 = 4.83 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 17 k= 18 x1 = 5.00 x2 = 5.69 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 18 k= 19 x1 = 4.12 x2 = 5.28 t = 1 w1 = 4.33 w2 = 4.81 theta = 2 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 19 ~中略~ k= 181 x1 = 0.28 x2 = -1.51 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 1 k= 182 x1 = -0.58 x2 = 1.65 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = -0.00 dw2 = 0.00 dtheta = 0 end= 2 k= 183 x1 = -2.43 x2 = -0.43 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 3 k= 184 x1 = 1.27 x2 = -0.87 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 4 k= 185 x1 = -0.68 x2 = -0.09 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 5 k= 186 x1 = 1.49 x2 = -0.64 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 6 k= 187 x1 = -0.44 x2 = -0.43 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 7 k= 188 x1 = 2.21 x2 = 2.19 t = 0 w1 = 3.79 w2 = 4.35 theta = 10 y = 1.00 dw1 = -2.21 dw2 = -2.19 dtheta = 1 end= 8 k= 189 x1 = 1.00 x2 = 0.39 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 9 k= 190 x1 = 5.74 x2 = 6.49 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 10 k= 191 x1 = 4.06 x2 = 6.18 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 11 k= 192 x1 = 3.75 x2 = 4.36 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 12 k= 193 x1 = 5.91 x2 = 3.57 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 13 k= 194 x1 = 4.86 x2 = 4.14 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 14 k= 195 x1 = 4.74 x2 = 2.20 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 15 k= 196 x1 = 3.23 x2 = 4.30 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 16 k= 197 x1 = 5.93 x2 = 4.83 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 17 k= 198 x1 = 5.00 x2 = 5.69 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 18 k= 199 x1 = 4.12 x2 = 5.28 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 19 k= 200 x1 = -1.09 x2 = 1.00 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = -0.00 dw2 = 0.00 dtheta = 0 end= 0 k= 201 x1 = 0.28 x2 = -1.51 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 1 k= 202 x1 = -0.58 x2 = 1.65 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = -0.00 dw2 = 0.00 dtheta = 0 end= 2 k= 203 x1 = -2.43 x2 = -0.43 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 3 k= 204 x1 = 1.27 x2 = -0.87 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 4 k= 205 x1 = -0.68 x2 = -0.09 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 5 k= 206 x1 = 1.49 x2 = -0.64 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = -0.00 dtheta = 0 end= 6 k= 207 x1 = -0.44 x2 = -0.43 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = -0.00 dw2 = -0.00 dtheta = 0 end= 7 k= 208 x1 = 2.21 x2 = 2.19 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 8 k= 209 x1 = 1.00 x2 = 0.39 t = 0 w1 = 1.58 w2 = 2.17 theta = 11 y = 0.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 9 k= 210 x1 = 5.74 x2 = 6.49 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 10 k= 211 x1 = 4.06 x2 = 6.18 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 11 k= 212 x1 = 3.75 x2 = 4.36 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 12 k= 213 x1 = 5.91 x2 = 3.57 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 13 k= 214 x1 = 4.86 x2 = 4.14 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 14 k= 215 x1 = 4.74 x2 = 2.20 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 15 k= 216 x1 = 3.23 x2 = 4.30 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 16 k= 217 x1 = 5.93 x2 = 4.83 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 17 k= 218 x1 = 5.00 x2 = 5.69 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 18 k= 219 x1 = 4.12 x2 = 5.28 t = 1 w1 = 1.58 w2 = 2.17 theta = 11 y = 1.00 dw1 = 0.00 dw2 = 0.00 dtheta = 0 end= 19
(図131)