/
test.py
191 lines (88 loc) · 2.32 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
#!/usr/bin/env python
# coding: utf-8
# # 该文件对各个模块进行应用
#
# In[ ]:
import numpy as np
from load_datas import *
import matplotlib.pyplot as plt
import neuralNet as net
import netParts as parts
import optim as op
import Trainer
# ## 数据加载
# In[ ]:
X_train,Y_train,X_test,Y_test = load_data() #加载数据
# In[ ]:
classes = [x for x in range(10)]
for c in classes:
images = X_train[Y_train == c][:7]
for index,image in enumerate(images):
plt.subplot(7,10,index*10 + c+1)
plt.imshow(image.astype('uint8'))
plt.axis('off')
plt.show()
# ## 两层神经网络
# In[ ]:
x = X_train[:100]
y = Y_train[:100]
# In[ ]:
net.TwoLayerNet(x,y,100,10,1e-4,100) #两层神经网络
# ## 多层神经网络
# In[ ]:
model = net.FullyConnectedNets(3*32*32,[500,100],10,
loss_function=op.svm_loss,activation_function = 'relu',
config = {'lr':1e-5,'momentum':0.9,'decay_rate':0.99},grad_function = net.rmsprop) #多层神经网络
# In[ ]:
x = X_train[:3000]
y = Y_train[:3000]
# In[ ]:
#loss,grads = model.loss(x,y)
# In[ ]:
#print(loss)
# In[ ]:
processor = Trainer.ModelProcessor(model,x,y) #训练器
# In[ ]:
loss_history,acc_history = processor.train(epoch=10,iterations=50,printFreq=20)
# In[ ]:
test_x = X_test[:100]
test_y = Y_test[:100]
# In[ ]:
score,acc = model.predict(test_x,test_y)
print(acc)
# In[ ]:
plt.plot(loss_history)
plt.title('loss')
plt.xlabel('itertions')
plt.ylabel('loss')
plt.show()
# In[ ]:
plt.plot(acc_history)
plt.show()
# ## 卷积神经网络
# In[ ]:
conv_dims = [(3,3,5,2,2),(3,3,3,2,2)]
pool_dims = [(3,3,1),(3,3,1)]
fc_dims = [500,100]
model = net.ConvNets((32,32,3),conv_dims,pool_dims,fc_dims,10, config = {'lr':1e-5,'momentum':0.9,'decay_rate':0.99},reg=0.6)
# In[ ]:
x = X_train[:3000]
y = Y_train[:3000]
# In[ ]:
loss,grads = model.loss(x,y)
# In[ ]:
print(loss)
# In[ ]:
processor = Trainer.ModelProcessor(model,x,y) #训练器
# In[ ]:
loss_history,acc_history = processor.train(epoch=2,iterations=1000,printFreq=1)
# In[ ]:
plt.plot(loss_history)
plt.title('loss')
plt.xlabel('itertions')
plt.ylabel('loss')
plt.show()
# In[ ]:
plt.plot(acc_history)
plt.show()
# In[ ]: