-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_1.py
328 lines (298 loc) · 23.4 KB
/
lab_1.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 6 01:44:02 2018
@author: HP
"""
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import random
import skimage.io as io
from skimage import data_dir
str0='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/0/*.png'
str1='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/1/*.png'
str2='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/2/*.png'
str3='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/3/*.png'
str4='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/4/*.png'
str5='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/5/*.png'
str6='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/6/*.png'
str7='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/7/*.png'
str8='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/8/*.png'
str9='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/9/*.png'
coll0 = io.ImageCollection(str0)
coll1 = io.ImageCollection(str1)
coll2 = io.ImageCollection(str2)
coll3 = io.ImageCollection(str3)
coll4 = io.ImageCollection(str4)
coll5 = io.ImageCollection(str5)
coll6 = io.ImageCollection(str6)
coll7 = io.ImageCollection(str7)
coll8 = io.ImageCollection(str8)
coll9 = io.ImageCollection(str9)
matrix0 = np.array(coll0)/255
matrix1 = np.array(coll1)/255
matrix2 = np.array(coll2)/255
matrix3 = np.array(coll3)/255
matrix4 = np.array(coll4)/255
matrix5 = np.array(coll5)/255
matrix6 = np.array(coll6)/255
matrix7 = np.array(coll7)/255
matrix8 = np.array(coll8)/255
matrix9 = np.array(coll9)/255
def f(x):
return 1 / (1 + np.exp(-x))
matrix_0 = np.zeros(shape=(4000,784,1))
matrix_1 = np.zeros(shape=(4000,784,1))
matrix_2 = np.zeros(shape=(4000,784,1))
matrix_3 = np.zeros(shape=(4000,784,1))
matrix_4 = np.zeros(shape=(4000,784,1))
matrix_5 = np.zeros(shape=(4000,784,1))
matrix_6 = np.zeros(shape=(4000,784,1))
matrix_7 = np.zeros(shape=(4000,784,1))
matrix_8 = np.zeros(shape=(4000,784,1))
matrix_9 = np.zeros(shape=(4000,784,1))
for i in range(len(coll0)):
matrix_0[i] = np.transpose(np.matrix(np.hstack((matrix0[i,0,:],matrix0[i,1,:],matrix0[i,2,:],matrix0[i,3,:],matrix0[i,4,:],\
matrix0[i,5,:],matrix0[i,6,:],matrix0[i,7,:],matrix0[i,8,:],matrix0[i,9,:],\
matrix0[i,10,:],matrix0[i,11,:],matrix0[i,12,:],matrix0[i,13,:],matrix0[i,14,:],matrix0[i,15,:],\
matrix0[i,16,:],matrix0[i,17,:],matrix0[i,18,:],matrix0[i,19,:],matrix0[i,20,:],matrix0[i,21,:],\
matrix0[i,22,:],matrix0[i,23,:],matrix0[i,24,:],matrix0[i,25,:],matrix0[i,26,:],matrix0[i,27,:]))))
matrix_1[i] = np.transpose(np.matrix(np.hstack((matrix1[i,0,:],matrix1[i,1,:],matrix1[i,2,:],matrix1[i,3,:],matrix1[i,4,:],\
matrix1[i,5,:],matrix1[i,6,:],matrix1[i,7,:],matrix1[i,8,:],matrix1[i,9,:],\
matrix1[i,10,:],matrix1[i,11,:],matrix1[i,12,:],matrix1[i,13,:],matrix1[i,14,:],matrix1[i,15,:],\
matrix1[i,16,:],matrix1[i,17,:],matrix1[i,18,:],matrix1[i,19,:],matrix1[i,20,:],matrix1[i,21,:],\
matrix1[i,22,:],matrix1[i,23,:],matrix1[i,24,:],matrix1[i,25,:],matrix1[i,26,:],matrix1[i,27,:]))))
matrix_2[i] = np.transpose(np.matrix(np.hstack((matrix2[i,0,:],matrix2[i,1,:],matrix2[i,2,:],matrix2[i,3,:],matrix2[i,4,:],\
matrix2[i,5,:],matrix2[i,6,:],matrix2[i,7,:],matrix2[i,8,:],matrix2[i,9,:],\
matrix2[i,10,:],matrix2[i,11,:],matrix2[i,12,:],matrix2[i,13,:],matrix2[i,14,:],matrix2[i,15,:],\
matrix2[i,16,:],matrix2[i,17,:],matrix2[i,18,:],matrix2[i,19,:],matrix2[i,20,:],matrix2[i,21,:],\
matrix2[i,22,:],matrix2[i,23,:],matrix2[i,24,:],matrix2[i,25,:],matrix2[i,26,:],matrix2[i,27,:]))))
matrix_3[i] = np.transpose(np.matrix(np.hstack((matrix3[i,0,:],matrix3[i,1,:],matrix3[i,2,:],matrix3[i,3,:],matrix3[i,4,:],\
matrix3[i,5,:],matrix3[i,6,:],matrix3[i,7,:],matrix3[i,8,:],matrix3[i,9,:],\
matrix3[i,10,:],matrix3[i,11,:],matrix3[i,12,:],matrix3[i,13,:],matrix3[i,14,:],matrix3[i,15,:],\
matrix3[i,16,:],matrix3[i,17,:],matrix3[i,18,:],matrix3[i,19,:],matrix3[i,20,:],matrix3[i,21,:],\
matrix3[i,22,:],matrix3[i,23,:],matrix3[i,24,:],matrix3[i,25,:],matrix3[i,26,:],matrix3[i,27,:]))))
matrix_4[i] = np.transpose(np.matrix(np.hstack((matrix4[i,0,:],matrix4[i,1,:],matrix4[i,2,:],matrix4[i,3,:],matrix4[i,4,:],\
matrix4[i,5,:],matrix4[i,6,:],matrix4[i,7,:],matrix4[i,8,:],matrix4[i,9,:],\
matrix4[i,10,:],matrix4[i,11,:],matrix4[i,12,:],matrix4[i,13,:],matrix4[i,14,:],matrix4[i,15,:],\
matrix4[i,16,:],matrix4[i,17,:],matrix4[i,18,:],matrix4[i,19,:],matrix4[i,20,:],matrix4[i,21,:],\
matrix4[i,22,:],matrix4[i,23,:],matrix4[i,24,:],matrix4[i,25,:],matrix4[i,26,:],matrix4[i,27,:]))))
matrix_5[i] = np.transpose(np.matrix(np.hstack((matrix5[i,0,:],matrix5[i,1,:],matrix5[i,2,:],matrix5[i,3,:],matrix5[i,4,:],\
matrix5[i,5,:],matrix5[i,6,:],matrix5[i,7,:],matrix5[i,8,:],matrix5[i,9,:],\
matrix5[i,10,:],matrix5[i,11,:],matrix5[i,12,:],matrix5[i,13,:],matrix5[i,14,:],matrix5[i,15,:],\
matrix5[i,16,:],matrix5[i,17,:],matrix5[i,18,:],matrix5[i,19,:],matrix5[i,20,:],matrix5[i,21,:],\
matrix5[i,22,:],matrix5[i,23,:],matrix5[i,24,:],matrix5[i,25,:],matrix5[i,26,:],matrix5[i,27,:]))))
matrix_6[i] = np.transpose(np.matrix(np.hstack((matrix6[i,0,:],matrix6[i,1,:],matrix6[i,2,:],matrix6[i,3,:],matrix6[i,4,:],\
matrix6[i,5,:],matrix6[i,6,:],matrix6[i,7,:],matrix6[i,8,:],matrix6[i,9,:],\
matrix6[i,10,:],matrix6[i,11,:],matrix6[i,12,:],matrix6[i,13,:],matrix6[i,14,:],matrix6[i,15,:],\
matrix6[i,16,:],matrix6[i,17,:],matrix6[i,18,:],matrix6[i,19,:],matrix6[i,20,:],matrix6[i,21,:],\
matrix6[i,22,:],matrix6[i,23,:],matrix6[i,24,:],matrix6[i,25,:],matrix6[i,26,:],matrix6[i,27,:]))))
matrix_7[i] = np.transpose(np.matrix(np.hstack((matrix7[i,0,:],matrix7[i,1,:],matrix7[i,2,:],matrix7[i,3,:],matrix7[i,4,:],\
matrix7[i,5,:],matrix7[i,6,:],matrix7[i,7,:],matrix7[i,8,:],matrix7[i,9,:],\
matrix7[i,10,:],matrix7[i,11,:],matrix7[i,12,:],matrix7[i,13,:],matrix7[i,14,:],matrix7[i,15,:],\
matrix7[i,16,:],matrix7[i,17,:],matrix7[i,18,:],matrix7[i,19,:],matrix7[i,20,:],matrix7[i,21,:],\
matrix7[i,22,:],matrix7[i,23,:],matrix7[i,24,:],matrix7[i,25,:],matrix7[i,26,:],matrix7[i,27,:]))))
matrix_8[i] = np.transpose(np.matrix(np.hstack((matrix8[i,0,:],matrix8[i,1,:],matrix8[i,2,:],matrix8[i,3,:],matrix8[i,4,:],\
matrix8[i,5,:],matrix8[i,6,:],matrix8[i,7,:],matrix8[i,8,:],matrix8[i,9,:],\
matrix8[i,10,:],matrix8[i,11,:],matrix8[i,12,:],matrix8[i,13,:],matrix8[i,14,:],matrix8[i,15,:],\
matrix8[i,16,:],matrix8[i,17,:],matrix8[i,18,:],matrix8[i,19,:],matrix8[i,20,:],matrix8[i,21,:],\
matrix8[i,22,:],matrix8[i,23,:],matrix8[i,24,:],matrix8[i,25,:],matrix8[i,26,:],matrix8[i,27,:]))))
matrix_9[i] = np.transpose(np.matrix(np.hstack((matrix9[i,0,:],matrix9[i,1,:],matrix9[i,2,:],matrix9[i,3,:],matrix9[i,4,:],\
matrix9[i,5,:],matrix9[i,6,:],matrix9[i,7,:],matrix9[i,8,:],matrix9[i,9,:],\
matrix9[i,10,:],matrix9[i,11,:],matrix9[i,12,:],matrix9[i,13,:],matrix9[i,14,:],matrix9[i,15,:],\
matrix9[i,16,:],matrix9[i,17,:],matrix9[i,18,:],matrix9[i,19,:],matrix9[i,20,:],matrix9[i,21,:],\
matrix9[i,22,:],matrix9[i,23,:],matrix9[i,24,:],matrix9[i,25,:],matrix9[i,26,:],matrix9[i,27,:]))))
matrix = np.zeros(shape = (40000,784,1))
matrix[0:4000] = matrix_0
matrix[4000:8000] = matrix_1
matrix[8000:12000] = matrix_2
matrix[12000:16000] = matrix_3
matrix[16000:20000] = matrix_4
matrix[20000:24000] = matrix_5
matrix[24000:28000] = matrix_6
matrix[28000:32000] = matrix_7
matrix[32000:36000] = matrix_8
matrix[36000:40000] = matrix_9
np.random.shuffle(matrix)
iteration = 20
hidden_layers_nod = 30
eta = 0.5
consequence = 10
#weight = np.zeros(shape=(number_of_hidden_layers,hidden_layers_nod,hidden_layers_nod))
weight_input = np.matrix(np.random.uniform(-1,1,(hidden_layers_nod,784)))
weight_output = np.matrix(np.random.uniform(-1,1,(consequence,hidden_layers_nod)))
aim = np.zeros(shape = (40000,10,1))
aim[0:4000] = np.array([[1],[0],[0],[0],[0],[0],[0],[0],[0],[0]])
aim[4000:8000] = np.array([[0],[1],[0],[0],[0],[0],[0],[0],[0],[0]])
aim[8000:12000] = np.array([[0],[0],[1],[0],[0],[0],[0],[0],[0],[0]])
aim[12000:16000] = np.array([[0],[0],[0],[1],[0],[0],[0],[0],[0],[0]])
aim[16000:20000] = np.array([[0],[0],[0],[0],[1],[0],[0],[0],[0],[0]])
aim[20000:24000] = np.array([[0],[0],[0],[0],[0],[1],[0],[0],[0],[0]])
aim[24000:28000] = np.array([[0],[0],[0],[0],[0],[0],[1],[0],[0],[0]])
aim[28000:32000] = np.array([[0],[0],[0],[0],[0],[0],[0],[1],[0],[0]])
aim[32000:36000] = np.array([[0],[0],[0],[0],[0],[0],[0],[0],[1],[0]])
aim[36000:40000] = np.array([[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]])
allmatrix = np.zeros(shape = (40000,794,1))
for m in range(40000):
allmatrix[m] = np.vstack((aim[m],matrix[m]))
np.random.shuffle(allmatrix)
for l in range(40000):
aim[l] = allmatrix[l,0:10]
for u in range(40000):
matrix[u] = allmatrix[u,10:40000]
def f(x):
return 1 / (1 + np.exp(-x))
#隱藏層 = 1,forward,"0"
#訓練模型
for c in range(iteration):
for d in range(40000):
hidden_layer1 = np.matrix( weight_input * matrix[d] )
hidden_layer1_output = f(hidden_layer1)
#hidden_layer2 = np.matrix( weight * hidden_layer1_output)
#hidden_layer2_output = f(hidden_layer2)
final = weight_output * hidden_layer1_output
final_output = f(final)
if (1/2)*(np.asscalar((sum(aim[d] - final)))**2) < 0.001:
print("it's good")
else:
error_outputlayer = np.array((aim[d] - final_output)) * np.array(final_output) * np.array(np.transpose(np.matrix([1,1,1,1,1,1,1,1,1,1])) - final_output)
error_hiddenlayer = np.array(hidden_layer1_output) * np.array((1 - hidden_layer1_output)) * np.transpose(np.array(np.transpose(error_outputlayer) * weight_output ))
delta_weight_hidden_layer = eta * matrix[d] * np.transpose(error_hiddenlayer)
delta_weight_output_layer = eta * hidden_layer1_output * np.transpose(error_outputlayer)
weight_input = weight_input + np.transpose(delta_weight_hidden_layer)
weight_output = weight_output + np.transpose(delta_weight_output_layer)
#測試
def cost_function(x):
return (1/2)*((1 - x)**2)
str0='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/0/*.png'
str1='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/1/*.png'
str2='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/2/*.png'
str3='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/3/*.png'
str4='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/4/*.png'
str5='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/5/*.png'
str6='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/6/*.png'
str7='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/7/*.png'
str8='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/8/*.png'
str9='C:/Users/HP/Desktop/碩士班/類神經網路與深度學習/training/9/*.png'
coll0 = io.ImageCollection(str0)
coll1 = io.ImageCollection(str1)
coll2 = io.ImageCollection(str2)
coll3 = io.ImageCollection(str3)
coll4 = io.ImageCollection(str4)
coll5 = io.ImageCollection(str5)
coll6 = io.ImageCollection(str6)
coll7 = io.ImageCollection(str7)
coll8 = io.ImageCollection(str8)
coll9 = io.ImageCollection(str9)
matrix0 = np.array(coll0)/255
matrix1 = np.array(coll1)/255
matrix2 = np.array(coll2)/255
matrix3 = np.array(coll3)/255
matrix4 = np.array(coll4)/255
matrix5 = np.array(coll5)/255
matrix6 = np.array(coll6)/255
matrix7 = np.array(coll7)/255
matrix8 = np.array(coll8)/255
matrix9 = np.array(coll9)/255
for i in range(len(coll0)):
matrix_0[i] = np.transpose(np.matrix(np.hstack((matrix0[i,0,:],matrix0[i,1,:],matrix0[i,2,:],matrix0[i,3,:],matrix0[i,4,:],\
matrix0[i,5,:],matrix0[i,6,:],matrix0[i,7,:],matrix0[i,8,:],matrix0[i,9,:],\
matrix0[i,10,:],matrix0[i,11,:],matrix0[i,12,:],matrix0[i,13,:],matrix0[i,14,:],matrix0[i,15,:],\
matrix0[i,16,:],matrix0[i,17,:],matrix0[i,18,:],matrix0[i,19,:],matrix0[i,20,:],matrix0[i,21,:],\
matrix0[i,22,:],matrix0[i,23,:],matrix0[i,24,:],matrix0[i,25,:],matrix0[i,26,:],matrix0[i,27,:]))))
matrix_1[i] = np.transpose(np.matrix(np.hstack((matrix1[i,0,:],matrix1[i,1,:],matrix1[i,2,:],matrix1[i,3,:],matrix1[i,4,:],\
matrix1[i,5,:],matrix1[i,6,:],matrix1[i,7,:],matrix1[i,8,:],matrix1[i,9,:],\
matrix1[i,10,:],matrix1[i,11,:],matrix1[i,12,:],matrix1[i,13,:],matrix1[i,14,:],matrix1[i,15,:],\
matrix1[i,16,:],matrix1[i,17,:],matrix1[i,18,:],matrix1[i,19,:],matrix1[i,20,:],matrix1[i,21,:],\
matrix1[i,22,:],matrix1[i,23,:],matrix1[i,24,:],matrix1[i,25,:],matrix1[i,26,:],matrix1[i,27,:]))))
matrix_2[i] = np.transpose(np.matrix(np.hstack((matrix2[i,0,:],matrix2[i,1,:],matrix2[i,2,:],matrix2[i,3,:],matrix2[i,4,:],\
matrix2[i,5,:],matrix2[i,6,:],matrix2[i,7,:],matrix2[i,8,:],matrix2[i,9,:],\
matrix2[i,10,:],matrix2[i,11,:],matrix2[i,12,:],matrix2[i,13,:],matrix2[i,14,:],matrix2[i,15,:],\
matrix2[i,16,:],matrix2[i,17,:],matrix2[i,18,:],matrix2[i,19,:],matrix2[i,20,:],matrix2[i,21,:],\
matrix2[i,22,:],matrix2[i,23,:],matrix2[i,24,:],matrix2[i,25,:],matrix2[i,26,:],matrix2[i,27,:]))))
matrix_3[i] = np.transpose(np.matrix(np.hstack((matrix3[i,0,:],matrix3[i,1,:],matrix3[i,2,:],matrix3[i,3,:],matrix3[i,4,:],\
matrix3[i,5,:],matrix3[i,6,:],matrix3[i,7,:],matrix3[i,8,:],matrix3[i,9,:],\
matrix3[i,10,:],matrix3[i,11,:],matrix3[i,12,:],matrix3[i,13,:],matrix3[i,14,:],matrix3[i,15,:],\
matrix3[i,16,:],matrix3[i,17,:],matrix3[i,18,:],matrix3[i,19,:],matrix3[i,20,:],matrix3[i,21,:],\
matrix3[i,22,:],matrix3[i,23,:],matrix3[i,24,:],matrix3[i,25,:],matrix3[i,26,:],matrix3[i,27,:]))))
matrix_4[i] = np.transpose(np.matrix(np.hstack((matrix4[i,0,:],matrix4[i,1,:],matrix4[i,2,:],matrix4[i,3,:],matrix4[i,4,:],\
matrix4[i,5,:],matrix4[i,6,:],matrix4[i,7,:],matrix4[i,8,:],matrix4[i,9,:],\
matrix4[i,10,:],matrix4[i,11,:],matrix4[i,12,:],matrix4[i,13,:],matrix4[i,14,:],matrix4[i,15,:],\
matrix4[i,16,:],matrix4[i,17,:],matrix4[i,18,:],matrix4[i,19,:],matrix4[i,20,:],matrix4[i,21,:],\
matrix4[i,22,:],matrix4[i,23,:],matrix4[i,24,:],matrix4[i,25,:],matrix4[i,26,:],matrix4[i,27,:]))))
matrix_5[i] = np.transpose(np.matrix(np.hstack((matrix5[i,0,:],matrix5[i,1,:],matrix5[i,2,:],matrix5[i,3,:],matrix5[i,4,:],\
matrix5[i,5,:],matrix5[i,6,:],matrix5[i,7,:],matrix5[i,8,:],matrix5[i,9,:],\
matrix5[i,10,:],matrix5[i,11,:],matrix5[i,12,:],matrix5[i,13,:],matrix5[i,14,:],matrix5[i,15,:],\
matrix5[i,16,:],matrix5[i,17,:],matrix5[i,18,:],matrix5[i,19,:],matrix5[i,20,:],matrix5[i,21,:],\
matrix5[i,22,:],matrix5[i,23,:],matrix5[i,24,:],matrix5[i,25,:],matrix5[i,26,:],matrix5[i,27,:]))))
matrix_6[i] = np.transpose(np.matrix(np.hstack((matrix6[i,0,:],matrix6[i,1,:],matrix6[i,2,:],matrix6[i,3,:],matrix6[i,4,:],\
matrix6[i,5,:],matrix6[i,6,:],matrix6[i,7,:],matrix6[i,8,:],matrix6[i,9,:],\
matrix6[i,10,:],matrix6[i,11,:],matrix6[i,12,:],matrix6[i,13,:],matrix6[i,14,:],matrix6[i,15,:],\
matrix6[i,16,:],matrix6[i,17,:],matrix6[i,18,:],matrix6[i,19,:],matrix6[i,20,:],matrix6[i,21,:],\
matrix6[i,22,:],matrix6[i,23,:],matrix6[i,24,:],matrix6[i,25,:],matrix6[i,26,:],matrix6[i,27,:]))))
matrix_7[i] = np.transpose(np.matrix(np.hstack((matrix7[i,0,:],matrix7[i,1,:],matrix7[i,2,:],matrix7[i,3,:],matrix7[i,4,:],\
matrix7[i,5,:],matrix7[i,6,:],matrix7[i,7,:],matrix7[i,8,:],matrix7[i,9,:],\
matrix7[i,10,:],matrix7[i,11,:],matrix7[i,12,:],matrix7[i,13,:],matrix7[i,14,:],matrix7[i,15,:],\
matrix7[i,16,:],matrix7[i,17,:],matrix7[i,18,:],matrix7[i,19,:],matrix7[i,20,:],matrix7[i,21,:],\
matrix7[i,22,:],matrix7[i,23,:],matrix7[i,24,:],matrix7[i,25,:],matrix7[i,26,:],matrix7[i,27,:]))))
matrix_8[i] = np.transpose(np.matrix(np.hstack((matrix8[i,0,:],matrix8[i,1,:],matrix8[i,2,:],matrix8[i,3,:],matrix8[i,4,:],\
matrix8[i,5,:],matrix8[i,6,:],matrix8[i,7,:],matrix8[i,8,:],matrix8[i,9,:],\
matrix8[i,10,:],matrix8[i,11,:],matrix8[i,12,:],matrix8[i,13,:],matrix8[i,14,:],matrix8[i,15,:],\
matrix8[i,16,:],matrix8[i,17,:],matrix8[i,18,:],matrix8[i,19,:],matrix8[i,20,:],matrix8[i,21,:],\
matrix8[i,22,:],matrix8[i,23,:],matrix8[i,24,:],matrix8[i,25,:],matrix8[i,26,:],matrix8[i,27,:]))))
matrix_9[i] = np.transpose(np.matrix(np.hstack((matrix9[i,0,:],matrix9[i,1,:],matrix9[i,2,:],matrix9[i,3,:],matrix9[i,4,:],\
matrix9[i,5,:],matrix9[i,6,:],matrix9[i,7,:],matrix9[i,8,:],matrix9[i,9,:],\
matrix9[i,10,:],matrix9[i,11,:],matrix9[i,12,:],matrix9[i,13,:],matrix9[i,14,:],matrix9[i,15,:],\
matrix9[i,16,:],matrix9[i,17,:],matrix9[i,18,:],matrix9[i,19,:],matrix9[i,20,:],matrix9[i,21,:],\
matrix9[i,22,:],matrix9[i,23,:],matrix9[i,24,:],matrix9[i,25,:],matrix9[i,26,:],matrix9[i,27,:]))))
matrix_test = np.zeros(shape = (40000,784,1))
matrix_test[0:4000] = matrix_0
matrix_test[4000:8000] = matrix_1
matrix_test[8000:12000] = matrix_2
matrix_test[12000:16000] = matrix_3
matrix_test[16000:20000] = matrix_4
matrix_test[20000:24000] = matrix_5
matrix_test[24000:28000] = matrix_6
matrix_test[28000:32000] = matrix_7
matrix_test[32000:36000] = matrix_8
matrix_test[36000:40000] = matrix_9
#讀取資料
string_path='C:/Users/HP/Desktop/test/data/*.png'
coll_final = io.ImageCollection(string_path)
matrix_final = np.array(coll_final)/255
matrix_test_final = np.zeros(shape=(len(matrix_final),784,1))
for n in range(len(coll_final)):
matrix_test_final[n] = np.transpose(np.matrix(np.hstack((matrix_final[n,0,:],matrix_final[n,1,:],matrix_final[n,2,:],matrix_final[n,3,:],matrix_final[n,4,:],\
matrix_final[n,5,:],matrix_final[n,6,:],matrix_final[n,7,:],matrix_final[n,8,:],matrix_final[n,9,:],\
matrix_final[n,10,:],matrix_final[n,11,:],matrix_final[n,12,:],matrix_final[n,13,:],matrix_final[n,14,:],matrix_final[n,15,:],\
matrix_final[n,16,:],matrix_final[n,17,:],matrix_final[n,18,:],matrix_final[n,19,:],matrix_final[n,20,:],matrix_final[n,21,:],\
matrix_final[n,22,:],matrix_final[n,23,:],matrix_final[n,24,:],matrix_final[n,25,:],matrix_final[n,26,:],matrix_final[n,27,:]))))
def f(x):
return 1 / (1 + np.exp(-x))
answer = np.zeros(shape = (len(matrix_final),1))
for p in range(10000):
hidden_layer1 = np.matrix( weight_input * matrix_test_final[p] )
hidden_layer1_output = f(hidden_layer1)
final = weight_output * hidden_layer1_output
final_output = f(final)
temp = cost_function(np.array(final_output))
temp[0] = round(np.asscalar(temp[0]),5)
temp[1] = round(np.asscalar(temp[1]),5)
temp[2] = round(np.asscalar(temp[2]),5)
temp[3] = round(np.asscalar(temp[3]),5)
temp[4] = round(np.asscalar(temp[4]),5)
temp[5] = round(np.asscalar(temp[5]),5)
temp[6] = round(np.asscalar(temp[6]),5)
temp[7] = round(np.asscalar(temp[7]),5)
temp[8] = round(np.asscalar(temp[8]),5)
temp[9] = round(np.asscalar(temp[9]),5)
temp_list = temp.tolist()
answers = temp_list.index([np.asscalar(min(temp))])
answer[p] = answers
print(answers)
#生成檔案
answer1 = np.transpose(np.array(answer,dtype = int))
f = open('C:\\Users\\HP\\Desktop\\碩士班\\類神經網路與深度學習\\A.txt', 'w', encoding = 'UTF-8')
for y in range(10000):
f.write('{0:05}'.format(y+1)+ " "+ str(answer1[0,y])+"\n")
f.close()