-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
355 lines (311 loc) · 11.6 KB
/
test.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from optimizer import *
import numpy as np
import math
import time
'''
First generate the data,and compute the labels."train_value" is the value for 1 means in the circle, 0 means not in the circle.
"train_labels" is the one hot label of train_value for testing classification with softmax and cross_entropy
'''
x = np.random.uniform(0,1,1000)
x.shape=(x.shape[0],1)
y = np.random.uniform(0,1,1000)
y.shape=(y.shape[0],1)
train_data=np.column_stack((x, y))
r=((x-0.5)**2+(y-0.5)**2)**0.5
train_labels=r<1/(2*math.pi)**0.5
# 0 and 1 label
train_value=train_labels.copy().astype(np.int8)
# one hot label
train_labels.shape=train_labels.shape[0],
train_labels_index=train_labels.astype(np.int)
train_labels=np.zeros((1000,2))
train_labels[np.arange(1000), train_labels_index] = 1
x_test = np.random.uniform(0,1,1000)
x_test.shape=(x_test.shape[0],1)
y_test = np.random.uniform(0,1,1000)
y_test.shape=(y_test.shape[0],1)
test_data=np.column_stack((x_test, y_test))
r=((x_test-0.5)**2+(y_test-0.5)**2)**0.5
test_labels=r<1/(2*math.pi)**0.5
# 0 and 1 label
test_value=test_labels.copy().astype(np.int8)
# one hot label
test_labels.shape=test_labels.shape[0],
test_labels_index=test_labels.astype(np.int)
test_labels=np.zeros((1000,2))
test_labels[np.arange(1000), test_labels_index] = 1
# Now we define two functions to compute the error rate
# Compute the error rate for one hot label
def compute_error_onehot(model,step,data,label):
pr=Predict(model)
predict_labels=pr.batch_predict(data)
predict_labels=np.where(predict_labels>0.5,1,0)
err_rate=np.sum(abs(label-predict_labels))/2.0/1000.0
return err_rate,predict_labels
# Compute the error rate for label of 0,1
def compute_error_normal(model,step,data,value):
pr=Predict(model)
predict_labels=pr.batch_predict(data)
predict_labels=np.where(predict_labels>0.5,1,0)
err_rate=np.sum(abs(value-predict_labels))/1000.0
return err_rate,predict_labels
def compute_error_rate(model,step,train_data,train_labels,test_data,test_labels,one_hot):
if one_hot is True:
err_train, _ = compute_error_onehot(model,step,train_data,train_labels)
err_test, _ = compute_error_onehot(model,step,test_data,test_labels)
else:
err_train, _ = compute_error_normal(model,step,train_data,train_labels)
err_test, _ = compute_error_normal(model,step,test_data,test_labels)
return err_train,err_test
#########################################################################################################
print ("Test with one hot labels with softmax and cross entropy")
'''
Build the network and predict the data
You can also write like this:
l1=Inputholder()
l2=Linear(25,l1)
l3=ReLU(l2)
l4=Linear(25,l3)
l5=ReLU(l4)
l6=Linear(25,l5)
l7=ReLU(l6)
l8=Linear(2,l7)
l9=Softmax(l8)
err=LossCrossEntropy(l9)
Or you can write with this Sequential class
'''
l1 = Sequential(Inputholder(),\
Linear(25),\
ReLU(),\
Linear(25),\
ReLU(),\
Linear(25),\
ReLU(),\
Linear(2),\
Softmax(),\
LossCrossEntropy())
op=Optimizer(train_data,train_labels,l1,5000,lr=0.1,method="SGD",print_curve=True,batch_size=500,lr_options="t_frac_decay",\
decay_rate=0.01,test_function=compute_error_rate,test_function_args=(train_data,train_labels,test_data,test_labels,True),test_interval=10)
start = time.clock()
op.optimize()
elapsed = (time.clock() - start)
print("Time used:",elapsed)
'''
Now we print a plot for the train error rate and the test error rate
'''
train_err_list = np.array(list(op.test_return))[:,0]
test_err_list = np.array(list(op.test_return))[:,1]
# Test interval is 10,whole steps is 5000,the step 0 is not computed,but the step 5000 is computed
x_value = np.arange(10,5010,10)
plt.plot(x_value,train_err_list,label="train error rate")
plt.plot(x_value,test_err_list,label="test error rate")
plt.title("Error rate curve")
plt.xlabel("step")
plt.ylabel("Error rate")
plt.xlim(xmin=10)
plt.legend()
plt.show()
err_train , predict_labels = compute_error_onehot(l1,0,train_data,train_labels)
err_test , predict_labels_test = compute_error_onehot(l1,0,test_data,test_labels)
print ("Error rate for train data")
print (str(err_train*100)[0:5]+"%")
print ("Error rate for test data")
print (str(err_test*100)[0:5]+"%")
print ("Visualize the classification results")
'''
Visualize the classification results
'''
print ("Visualize training data")
plt.figure(figsize=(6,6))
plt.title("Visualize training data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x[predict_labels[:,1]==0],y[predict_labels[:,1]==0],color='lightgreen',marker=".")
plt.scatter(x[predict_labels[:,1]==1],y[predict_labels[:,1]==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()
print ("Visualize testing data")
plt.figure(figsize=(6,6))
plt.title("Visualize testing data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x_test[predict_labels_test[:,1]==0],y_test[predict_labels_test[:,1]==0],color='lightgreen',marker=".")
plt.scatter(x_test[predict_labels_test[:,1]==1],y_test[predict_labels_test[:,1]==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()
#########################################################################################################
'''
Now we do the same thing with sigmoid function and MSE error. We do not use one hot labels this time.
'''
print ("Test with MSE error and sigmoid output")
'''
Build the network and predict the data
Also you can write like this:
l1=Inputholder()
l2=Linear(25,l1)
l3=Sigmoid(l2)
l4=Linear(25,l3)
l5=Sigmoid(l4)
l6=Linear(25,l5)
l7=Sigmoid(l6)
l8=Linear(1,l7)
l9=Sigmoid(l8)
err=LossMSE(l9)
Or you can write with this Sequential class
'''
l1 = Sequential(Inputholder(),\
Linear(25),\
Sigmoid(),\
Linear(25),\
Sigmoid(),\
Linear(25),\
Sigmoid(),\
Linear(1),\
Sigmoid(),\
LossMSE())
op=Optimizer(train_data,train_value,l1,5000,lr=0.8,method="SGD",print_curve=True,batch_size=500,lr_options="exp_decay",\
decay_rate=0.0002,test_function=compute_error_rate,test_function_args=(train_data,train_value,test_data,test_value,False),test_interval=10)
start = time.clock()
op.optimize()
elapsed = (time.clock() - start)
print("Time used:",elapsed)
'''
Now we print a plot for the train error rate and the test error rate
'''
train_err_list = np.array(list(op.test_return))[:,0]
test_err_list = np.array(list(op.test_return))[:,1]
# Test interval is 10,whole steps is 5000,the step 0 is not computed,but the step 5000 is computed
x_value = np.arange(10,5010,10)
plt.plot(x_value,train_err_list,label="train error rate")
plt.plot(x_value,test_err_list,label="test error rate")
plt.title("Error rate curve")
plt.xlabel("step")
plt.ylabel("Error rate")
plt.xlim(xmin=10)
plt.legend()
plt.show()
err_train , predict_labels = compute_error_normal(l1,0,train_data,train_value)
err_test , predict_labels_test = compute_error_normal(l1,0,test_data,test_value)
print ("Error rate for train data")
print (str(err_train*100)[0:5]+"%")
print ("Error rate for test data")
print (str(err_test*100)[0:5]+"%")
print ("Visualize the classification results")
'''
Visualize the classification results
'''
print ("Visualize training data")
plt.figure(figsize=(6,6))
plt.title("Visualize training data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x[predict_labels==0],y[predict_labels==0],color='lightgreen',marker=".")
plt.scatter(x[predict_labels==1],y[predict_labels==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()
print ("Visualize testing data")
plt.figure(figsize=(6,6))
plt.title("Visualize testing data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x_test[predict_labels_test==0],y_test[predict_labels_test==0],color='lightgreen',marker=".")
plt.scatter(x_test[predict_labels_test==1],y_test[predict_labels_test==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()
#########################################################################################################
'''
Now we use cross entropy loss again.But this time we use tanh as the inner activation layer.(In order to test tanh layer)
'''
print ("Test with one hot labels with softmax and cross entropy (tanh activation)")
'''
Build the network and predict the data
You can also write like this:
l1=Inputholder()
l2=Linear(25,l1)
l3=Tanh(l2)
l4=Linear(25,l3)
l5=Tanh(l4)
l6=Linear(25,l5)
l7=Tanh(l6)
l8=Linear(2,l7)
l9=Softmax(l8)
err=LossCrossEntropy(l9)
Or you can write with this Sequential class
'''
l1 = Sequential(Inputholder(),\
Linear(25),\
Tanh(),\
Linear(25),\
Tanh(),\
Linear(25),\
Tanh(),\
Linear(2),\
Softmax(),\
LossCrossEntropy())
op=Optimizer(train_data,train_labels,l1,5000,lr=0.03,method="SGD",print_curve=True,batch_size=500,lr_options="t_frac_decay",\
decay_rate=0.0010,test_function=compute_error_rate,test_function_args=(train_data,train_labels,test_data,test_labels,True),test_interval=10)
start = time.clock()
op.optimize()
elapsed = (time.clock() - start)
print("Time used:",elapsed)
'''
Now we print a plot for the train error rate and the test error rate
'''
train_err_list = np.array(list(op.test_return))[:,0]
test_err_list = np.array(list(op.test_return))[:,1]
# Test interval is 10,whole steps is 5000,the step 0 is not computed,but the step 5000 is computed
x_value = np.arange(10,5010,10)
plt.plot(x_value,train_err_list,label="train error rate")
plt.plot(x_value,test_err_list,label="test error rate")
plt.title("Error rate curve")
plt.xlabel("step")
plt.ylabel("Error rate")
plt.xlim(xmin=10)
plt.legend()
plt.show()
err_train , predict_labels = compute_error_onehot(l1,0,train_data,train_labels)
err_test , predict_labels_test = compute_error_onehot(l1,0,test_data,test_labels)
print ("Error rate for train data")
print (str(err_train*100)[0:5]+"%")
print ("Error rate for test data")
print (str(err_test*100)[0:5]+"%")
print ("Visualize the classification results")
'''
Visualize the classification results
'''
print ("Visualize training data")
plt.figure(figsize=(6,6))
plt.title("Visualize training data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x[predict_labels[:,1]==0],y[predict_labels[:,1]==0],color='lightgreen',marker=".")
plt.scatter(x[predict_labels[:,1]==1],y[predict_labels[:,1]==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()
print ("Visualize testing data")
plt.figure(figsize=(6,6))
plt.title("Visualize testing data")
ax = plt.gca()
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
circle1 = plt.Circle((0.5, 0.5), 1/math.sqrt(2*math.pi), color='lightpink',alpha=0.5)
ax.add_artist(circle1)
plt.scatter(x_test[predict_labels_test[:,1]==0],y_test[predict_labels_test[:,1]==0],color='lightgreen',marker=".")
plt.scatter(x_test[predict_labels_test[:,1]==1],y_test[predict_labels_test[:,1]==1],color='slateblue',marker=".")
ax.set_aspect(1)
plt.show()