-
Notifications
You must be signed in to change notification settings - Fork 10
/
1002.py
227 lines (174 loc) Β· 6.07 KB
/
1002.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 1002.pyκ° 1001.py μ λ€λ₯Έμ
#
# - λͺ©ν ν¨μ t λ₯Ό x κ° νλμ λν μμΌλ‘λ§ νμ
# - λͺ©ν ν¨μ, νμ΅λ ν¨μλ₯Ό κ·Έλνλ‘ plotting
# - μ½λλ₯Ό μ‘°κΈ λ μ 리
#
# λ±ν μ°Έκ³ ν μ½λ μμ
#
# 맀νΈλ¦μ€ μ°μ°μ μν΄μ numpy μ¬μ©νκ³ ,
# κ·Έλν μΆλ ₯μ μν΄μ matplotlib μ¬μ©νμμ
#
# κ·Έ μ΄μΈμ λ¨Έμ λ¬λ κ΄λ ¨ν λΌμ΄λΈλ¬λ¦¬λ μ¬μ©νμ§ μμμ
#
# python 3.5 νκ²½μμ μμ± νμμ
#
# μ€νμ©μΌλ‘ λμΆ© 빨리 λ§λ€μ΄λ³Έ μ½λλΌμ κΉλνμ§ μμ
#
# κ²λ€κ° python μ¬μ©νμ§ 1μ£ΌμΌλ μ λμ΄μ,
# νΉλ³ν μ΄μν μ½λκ° λ§μ κ²μΌλ‘ μΆμ λ¨
#
# Back Propagation μΈμ μꡬλλ λ°°κ²½ μ§μμ μμ
#
# μμ±μ:
# Facebook: https://facebook.com/dgtgrade
# Youtube: https://youtube.com/channel/UCEdT99nAs8nalv6Mafs9RiA
# email: dgtgrade@gmail.com
import numpy as np
import matplotlib.pyplot as plt
# plt κΈ°λ³Έ ν°νΈμ¬μ΄μ¦
plt.rc('xtick', labelsize=30)
plt.rc('ytick', labelsize=30)
# λ¨μν sigmoid ν¨μ
Z_MAX = 100
def sigmoid(z):
z = np.clip(z, -Z_MAX, Z_MAX) # overflow μλ¬ λ°©μ§
return 1.0 / (1.0 + np.exp(-z))
# λ¨μν sigmoid λ―ΈλΆ ν¨μ
# aλ scalar κ° λλ ndarray
def d_sigmoid(a):
return a * (1.0-a)
# λ€νΈμν¬ κ΅¬μ‘°λ λ€μκ³Ό κ°λ€.
# μ
λ ₯λ μ΄μ΄ (a1) --(th1)--> νλ λ μ΄μ΄ (a2)
# νλ λ μ΄μ΄ (a2) --(th2)--> μΆλ ₯λ μ΄μ΄ (a3)
# th1, th2 λ λ μ΄μ΄ μ¬μ΄μ μΈνκ° λͺ¨μ
# λͺ©ν ν¨μ t
def t(t_str,X):
Y = eval(t_str)
return Y
#t_str = "X*X"
t_str = "8*X**2-X**3"
#t_str = "10*np.sin(X)+(X-4)**2-10"
# μ
λ ₯κ° λ²μ
x_START = -5.0
x_END = 10.0
# νμ΅μ© μ λ΅ λ°μ΄ν°
X_train = np.linspace(x_START,x_END,num=20).reshape(-1,1)
Y_train = t(t_str,X_train)
# μνμ© μ λ΅ λ°μ΄ν°
X_test = np.linspace(x_START,x_END,num=100).reshape(-1,1)
Y_test = t(t_str,X_test)
# κ³ μ λ° μ λ΅ λ°μ΄ν°
# κ·Έλν μμ νμνκΈ° μν κ²
X_t = np.linspace(x_START,x_END,num=200).reshape(-1,1)
Y_t = t(t_str,X_t)
N_IN = X_train.shape[1] # μ
λ ₯ λ μ΄μ΄ λ
Έλμ
N_OUT = Y_train.shape[1] # μΆλ ₯ λ μ΄μ΄ λ
Έλμ
N_HD1 = 100 # νλ λ μ΄μ΄ λ
Έλμ
# μ λ΅ν μμ μ
m_train = X_train.shape[0]
m_test = X_test.shape[0]
# thetasλ th1μ th2λ₯Ό νλμ μΌμ°¨μ μ΄λ μ΄λ‘ ννν κ²
RSIZE = 1 # μ΄κΈ° λλ€κ°μ λ²μ μ€μ
np.random.seed(20160822) # λλ²κΉ
λ° λ
Όμλ₯Ό μ½κ² νκΈ° μν΄μ μ§μ
thetas = RSIZE * np.random.random_sample((N_IN+1)*N_HD1+(N_HD1+1)*N_OUT)
LR = 0.002 / m_train # Learning Rate
def th_split(thetas):
th1 = np.reshape(thetas[:N_HD1*(1+N_IN)], (N_HD1,1+N_IN))
th2 = np.reshape(thetas[N_HD1*(1+N_IN):], (N_OUT,1+N_HD1))
return (th1, th2)
def feed_forward(x,thetas):
(th1, th2) = th_split(thetas.copy())
a1 = np.vstack(([1.0], x)) # bias λ
Έλ μΆκ°
z2 = np.dot(th1, a1)
a2 = sigmoid(z2)
a2 = np.vstack(([1.0], a2)) # bias λ
Έλ μΆκ°
a3 = np.dot(th2, a2)
return (a1, z2, a2, a3)
def cost(a3,y):
return np.sum((a3 - y) ** 2 / 2)
def back_propagation(a1, z2, a2,a3, thetas,y, LR):
new_thetas = thetas.copy()
(new_th1, new_th2) = th_split(new_thetas)
# λ§μ§λ§ λ μ΄μ΄μ activation ν¨μ μμλ
#delta3 = (a3 - y)*d_sigmoid(a3)
# μμλ
delta3 = (a3 - y)
delta2 = np.dot(new_th2.T, delta3) * d_sigmoid(a2)
new_th2 -= LR * np.dot(delta3, a2.T)
new_th1 -= LR * np.dot(delta2[1:,:], a1.T)
return np.append(new_th1.flatten(), new_th2.flatten())
# numerical gradient descent
# κ³μ°μ κΈ°μΈκΈ°λ₯Ό μ΄μ©ν΄μ theta κ° μ‘°μ νκΈ°
def num_grad_desc(x,thetas,y,LR):
new_thetas = thetas.copy() #μλ‘μ΄ thetas
tmp_thetas = thetas.copy() #thetas μ€μμ 1κ° thetaλ§ λ³κ²½ν κ²
DELTA = 0.001
for i in range(thetas.size):
# κ³μ°μ κΈ°μΈκΈ° ꡬνκΈ°
tmp_thetas[i] = thetas[i] - DELTA
(a1, z2, a2, a3) = feed_forward(x, tmp_thetas)
c1 = cost(a3, y)
tmp_thetas[i] = thetas[i] + DELTA
(a1, z2, a2, a3) = feed_forward(x, tmp_thetas)
c2 = cost(a3, y)
tmp_thetas[i] = thetas[i]
# κΈ°μΈκΈ°μ λ°λΌμ thetas μ‘°μ
new_thetas[i] -= LR*(c2 - c1)/(DELTA*2)
return new_thetas
count =0
PLT_PAUSETIME=0.1
plt.ion()
plt.figure(figsize=(160,120))
while True:
count += 1
DOTEST=False
if ((count % 1000 == 0)
or (count < 1000 and count % 100 == 0)
or (count < 100 and count % 10 == 0)
or (count < 10)):
DOTEST = True
total_cost_train = 0.0
total_cost_test = 0.0
print (count)
# νμ΅
for i in range(m_train):
# μ λ΅
x = X_train[i:i+1,:].T
y = Y_train[i:i+1,:].T
# feed forward
(a1, z2, a2, a3) = feed_forward(x, thetas)
# cost
c = cost(a3, y)
total_cost_train += c
# back propagation
bp_thetas = back_propagation(a1, z2, a2, a3, thetas, y, LR)
thetas = bp_thetas
# κ³μ°μ κΈ°μΈκΈ°μ λΉκ΅
if False: # BP μκ³ λ¦¬μ¦ λλ²κΉ
μμλ§ μ¬μ©
ngd_thetas = num_grad_desc(x, thetas, y, LR)
thetas = ngd_thetas
# μν μΉκ³ κ·Έλν μ
λ°μ΄νΈ
if DOTEST:
A3 = np.empty([m_test,1])
for i in range(m_test):
x = X_test[i:i+1,:].T
y = Y_test[i:i+1,:].T
(a1, z2, a2, a3) = feed_forward(x, thetas)
c = cost(a3, y)
A3[i] = a3
total_cost_test += c
plt.clf()
ax = plt.subplot(111)
ax.text(0.5,0.8,"t(X)="+t_str,
fontsize=50, ha='center', transform=ax.transAxes)
ax.text(0.5,0.7,"Iteration:"+str(count),
fontsize=30, ha='center', transform=ax.transAxes)
ax.plot(X_t, Y_t,
c="green", marker="o", markersize=5.0, linewidth=5.0)
ax.plot(X_train, Y_train,
c="blue", marker="d", markersize=20.0, linestyle='None')
ax.plot(X_test, A3,
c="red", marker="o", markersize=10.0, linestyle='None')
plt.draw()
plt.pause(PLT_PAUSETIME)