-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path4xOX.py
134 lines (97 loc) · 3.72 KB
/
4xOX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#techgym7-6-A-4
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# 出力層
class OutputLayer:
#初期設定
def __init__(self, n_upper, n):
self.w = wb_width * np.random.randn(n_upper, n) # 重み(行列)
self.b = wb_width * np.random.randn(n) # バイアス(ベクトル)
# 順伝播
def forward(self, x):
self.x = x
u = np.dot(x, self.w) + self.b
self.y = u # 恒等関数
# 逆伝播
def backward(self, t):
delta = self.y - t
self.grad_w = np.dot(self.x.T, delta)
self.grad_b = np.sum(delta, axis=0)
self.grad_x = np.dot(delta, self.w.T)
# 重みとバイアスの更新
def update(self, eta):
self.w -= eta * self.grad_w
self.b -= eta * self.grad_b
# 中間層
class MiddleLayer:
#初期設定
def __init__(self, n_upper, n):
self.w = wb_width * np.random.randn(n_upper, n) # 重み(行列)
self.b = wb_width * np.random.randn(n) # バイアス(ベクトル)
# 順伝播
def forward(self, x):
self.x = x
u = np.dot(x, self.w) + self.b
self.y = 1/(1+np.exp(-u)) # シグモイド関数
# 逆伝播
def backward(self, grad_y):
delta = grad_y * (1-self.y)*self.y # シグモイド関数の微分
self.grad_w = np.dot(self.x.T, delta)
self.grad_b = np.sum(delta, axis=0)
self.grad_x = np.dot(delta, self.w.T)
def update(self, eta): # 重みとバイアスの更新
self.w -= eta * self.grad_w
self.b -= eta * self.grad_b
#入力と正解の用意
input_data = np.arange(0, np.pi*2, 0.1) # 入力
correct_data = # 正解
input_data = (input_data-np.pi)/np.pi # 入力を-1.0-1.0の範囲に収める
n_data = # データ数
#各設定値
n_in = 1 # 入力層のニューロン数
n_mid = 3 # 中間層のニューロン数
n_out = 1 # 出力層のニューロン数
wb_width = 0.01 # 重みとバイアスの広がり具合
eta = # 学習係数
epoch =
interval = 200 # 経過の表示間隔
#############################################################
# 各層の初期化
middle_layer =
output_layer =
# 学習
for i in range(epoch):
# インデックスをシャッフル
index_random =
np.random.shuffle(index_random)
# 結果の表示用
total_error = 0
plot_x = []
plot_y = []
for idx in index_random:
# 入力と正解をランダムに選ぶ
x = input_data[idx:idx+1]
t = correct_data[idx:idx+1] # 正解
# 順伝播
middle_layer.forward(x.reshape(1, 1)) # 入力を行列に変換
output_layer.forward(middle_layer.y)
# 逆伝播
output_layer.backward(t.reshape(1, 1)) # 正解を行列に変換
middle_layer.backward(output_layer.grad_x)
# 重みとバイアスの更新
if i%interval == 0:
# 行列をベクトルに戻す
y = output_layer.y.reshape(-1)
# 誤差の計算 : 二乗和誤差
total_error +=
# 出力の記録
plot_x.
plot_y.append(y)
if i%interval == 0:
# 出力のグラフ表示
plt.plot(input_data, correct_data, linestyle="dashed")
plt.scatter(plot_x, plot_y, marker="+")
plt.show()
# エポック数と誤差の表示
print("Epoch:" + str(i) + "/" + str(epoch), "Error:" + str(total_error/n_data))