質問編集履歴

1

中略しました。

2021/05/11 09:30

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,303 +30,7 @@
30
30
 
31
31
  from google.colab import drive
32
32
 
33
- drive.mount('/content/drive')
34
-
35
-
36
-
37
- import sys
38
-
39
-
40
-
41
- sys.path.append('/content/drive/My Drive')
42
-
43
-
44
-
45
- import numpy as np
46
-
47
- import ActivationFunction as AF
48
-
49
-
50
-
51
- # 3層ニューラルネットワーク
52
-
53
- class ThreeLayerNetwork:
54
-
55
- # コンストラクタ
56
-
57
- def __init__(self, inodes, hnodes, onodes, lr):
58
-
59
- # 各レイヤーのノード数
60
-
61
- self.inodes = inodes
62
-
63
- self.hnodes = hnodes
64
-
65
- self.onodes = onodes
66
-
67
-
68
-
69
- # 学習率
70
-
71
- self.lr = lr
72
-
73
-
74
-
75
- # 重みの初期化
76
-
77
- self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes))
78
-
79
- self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes))
80
-
81
-
82
-
83
- # 活性化関数
84
-
85
- self.af = AF.sigmoid
86
-
87
- self.daf = AF.derivative_sigmoid
88
-
89
-
90
-
91
- # 誤差逆伝搬
92
-
93
- def backprop(self, idata, tdata):
94
-
95
- # 縦ベクトルに変換
96
-
97
- o_i = np.array(idata, ndmin=2).T
98
-
99
- t = np.array(tdata, ndmin=2).T
100
-
101
-
102
-
103
- # 隠れ層
104
-
105
- x_h = np.dot(self.w_ih, o_i)
106
-
107
- o_h = self.af(x_h)
108
-
109
-
110
-
111
- # 出力層
112
-
113
- x_o = np.dot(self.w_ho, o_h)
114
-
115
- o_o = self.af(x_o)
116
-
117
-
118
-
119
- # 誤差計算
120
-
121
- e_o = (t - o_o)
122
-
123
- e_h = np.dot(self.w_ho.T, e_o)
124
-
125
-
126
-
127
- # 重みの更新
128
-
129
- self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T)
130
-
131
- self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T)
132
-
133
-
134
-
135
-
136
-
137
- # 順伝搬
138
-
139
- def feedforward(self, idata):
140
-
141
- # 入力のリストを縦ベクトルに変換
142
-
143
- o_i = np.array(idata, ndmin=2).T
144
-
145
-
146
-
147
- # 隠れ層
148
-
149
- x_h = np.dot(self.w_ih, o_i)
150
-
151
- o_h = self.af(x_h)
152
-
153
-
154
-
155
- # 出力層
156
-
157
- x_o = np.dot(self.w_ho, o_h)
158
-
159
- o_o = self.af(x_o)
160
-
161
-
162
-
163
- return o_o
164
-
165
-
166
-
167
- if __name__=='__main__':
168
-
169
- # パラメータ
170
-
171
- inodes = 784
172
-
173
- hnodes = 100
174
-
175
- onodes = 10
176
-
177
- lr = 0.3
178
-
179
-
180
-
181
- # ニューラルネットワークの初期化
182
-
183
- nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr)
184
-
185
-
186
-
187
- # トレーニングデータのロード
188
-
189
- training_data_file = open('drive/My Drive/mnist_dataset/mnist_train.csv', 'r')
190
-
191
- training_data_list = training_data_file.readlines()
192
-
193
- training_data_file.close()
194
-
195
-
196
-
197
- # テストデータのロード
198
-
199
- test_data_file = open('drive/My Drive/mnist_dataset/mnist_test.csv')
200
-
201
- test_data_list = test_data_file.readlines()
202
-
203
- test_data_file.close()
204
-
205
-
206
-
207
- # 学習
208
-
209
- epoch = 10
210
-
211
- for e in range(epoch):
212
-
213
- print('#epoch ', e)
214
-
215
- data_size = len(training_data_list)
216
-
217
- for i in range(data_size):
218
-
219
- if i % 1000 == 0:
220
-
221
- print(' train: {0:>5d} / {1:>5d}'.format(i, data_size))
222
-
223
- val = training_data_list[i].split(',')
224
-
225
- idata = (np.asfarray(val[1:]) / 255.0 * 0.99) + 0.01
226
-
227
- tdata = np.zeros(onodes) + 0.01
228
-
229
- tdata[int(val[0])] = 0.99
230
-
231
- nn.backprop(idata, tdata)
232
-
233
- pass
234
-
235
- pass
236
-
237
-
238
-
239
- # テスト
240
-
241
- scoreboard = []
242
-
243
- for record in test_data_list:
244
-
245
- val = record.split(',')
246
-
247
- idata = (np.asfarray(val[1:]) / 255.0 * 0.99) + 0.01
248
-
249
- tlabel = int(val[0])
250
-
251
- predict = nn.feedforward(idata)
252
-
253
- plabel = np.argmax(predict)
254
-
255
- print(plabel)
256
-
257
- scoreboard.append(tlabel == plabel)
258
-
259
- pass
260
-
261
-
262
-
263
- scoreboard_array = np.asarray(scoreboard)
264
-
265
- print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
266
-
267
-
268
-
269
- ```
33
+ 中略
270
-
271
- と同じコードを用いて精度をはかった結果が以下
272
-
273
-
274
-
275
- Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
276
-
277
- #epoch 0
278
-
279
- train: 0 / 3
280
-
281
- #epoch 1
282
-
283
- train: 0 / 3
284
-
285
- #epoch 2
286
-
287
- train: 0 / 3
288
-
289
- #epoch 3
290
-
291
- train: 0 / 3
292
-
293
- #epoch 4
294
-
295
- train: 0 / 3
296
-
297
- #epoch 5
298
-
299
- train: 0 / 3
300
-
301
- #epoch 6
302
-
303
- train: 0 / 3
304
-
305
- #epoch 7
306
-
307
- train: 0 / 3
308
-
309
- #epoch 8
310
-
311
- train: 0 / 3
312
-
313
- #epoch 9
314
-
315
- train: 0 / 3
316
-
317
- 0
318
-
319
- 3
320
-
321
- 7
322
-
323
- performance: 0.3333333333333333
324
-
325
- となり、
326
-
327
- ```python
328
-
329
- print(plabel)
330
34
 
331
35
  ```
332
36