python拟合求参_机器学习作业(四)神经网络参数的拟合——Python(numpy)实现
1 importnumpy as np2 importscipy.io as scio3 importmatplotlib.pyplot as plt4 importscipy.optimize as op5
6 #X:5000*400
7 #Y:5000*10
8 #a1:5000*401(后5000*400)
9 #z2:5000*25
10 #a2:5000*26(后5000*25)
11 #z3:5000*10
12 #a3:5000*10
13 #Theta1:25*401
14 #Theta2:10*26
15 #delta3:5000*10
16 #delta2:5000*25
17 #bigDelta1:25*401
18 #bigDelta2:10*26
19 #Theta1_grad:25*401
20 #Theta2_grad:10*26
21
22
23 #顯示圖片數據
24 defdisplayData(X):25 m = np.size(X, 0) #X的行數,即樣本數量
26 n = np.size(X, 1) #X的列數,即單個樣本大小
27 example_width = int(np.round(np.sqrt(n))) #單張圖片寬度
28 example_height = int(np.floor(n / example_width)) #單張圖片高度
29 display_rows = int(np.floor(np.sqrt(m))) #顯示圖中,一行多少張圖
30 display_cols = int(np.ceil(m / display_rows)) #顯示圖中,一列多少張圖片
31 pad = 1 #圖片間的間隔
32 display_array = - np.ones((pad + display_rows * (example_height +pad),33 pad + display_cols * (example_width + pad))) #初始化圖片矩陣
34 curr_ex = 0 #當前的圖片計數
35 #將每張小圖插入圖片數組中
36 for j inrange(0, display_rows):37 for i inrange(0, display_cols):38 if curr_ex >=m:39 break
40 max_val =np.max(abs(X[curr_ex, :]))41 jstart = pad + j * (example_height +pad)42 istart = pad + i * (example_width +pad)43 display_array[jstart: (jstart + example_height), istart: (istart + example_width)] =\44 np.array(X[curr_ex, :]).reshape(example_height, example_width) /max_val45 curr_ex = curr_ex + 1
46 if curr_ex >=m:47 break
48 display_array =display_array.T49 plt.imshow(display_array,cmap=plt.cm.gray)50 plt.axis('off')51 plt.show()52
53
54 #計算hθ(z)
55 defsigmoid(z):56 g = 1.0 / (1.0 + np.exp(-z))57 returng58
59
60 #初始化Θ,保持在[-ε,ε]
61 defrandInitializeWeights(sizeList):62 epsilon_init = 0.12
63 theta1_lx = sizeList['theta1_lx']64 theta1_ly = sizeList['theta1_ly']65 theta2_lx = sizeList['theta2_lx']66 theta2_ly = sizeList['theta2_ly']67 theta_size = theta1_lx * theta1_ly + theta2_lx *theta2_ly68 W = np.random.uniform(-epsilon_init, epsilon_init, theta_size)69 returnW70
71
72 #把一維的矩陣改寫為多維
73 defchangeForm(theta_vector, theta1_lx, theta1_ly, theta2_lx, theta2_ly):74 theta1 = np.array(theta_vector[0: theta1_lx *theta1_ly]).reshape(theta1_lx, theta1_ly)75 theta2 = np.array(theta_vector[theta1_lx * theta1_ly: theta1_lx * theta1_ly + theta2_lx *theta2_ly])\76 .reshape(theta2_lx, theta2_ly)77 theta = {'Theta1': theta1, 'Theta2': theta2}78 returntheta79
80
81 #計算正向激勵的參數a
82 defcomputeA(nn_params, X):83 theta1 = nn_params['Theta1']84 theta2 = nn_params['Theta2']85 m =np.size(X, 0)86
87 #第二層計算
88 one =np.ones(m)89 a1 = np.insert(X, 0, values=one, axis=1)90 a2 =sigmoid(np.dot(a1, theta1.T))91 #第三層計算
92 one =np.ones(np.size(a2, 0))93 a2 = np.insert(a2, 0, values=one, axis=1)94 a3 =sigmoid(np.dot(a2, theta2.T))95 a_res = {'a1': a1, 'a2': a2, 'a3': a3}96 returna_res97
98
99 #計算g'(z)
100 defsigmoidGradient(z):101 g = np.multiply(sigmoid(z), 1 -sigmoid(z))102 returng103
104
105 #計算 J
106 defnnCostFunction(nn_params, X, Y, lamb, sizeList):107 theta =changeForm(nn_params,108 sizeList['theta1_lx'], sizeList['theta1_ly'],109 sizeList['theta2_lx'], sizeList['theta2_ly'])110 theta1 = theta['Theta1']111 theta2 = theta['Theta2']112 m =np.size(X, 0)113 a_res =computeA(theta, X)114 a3 = a_res['a3']115 #計算J
116 J = 1 / m * np.sum(-np.multiply(Y, np.log(a3)) - np.multiply((1 - Y), np.log(1 -a3)))117 #規格化
118 theta1_copy = theta1[:, 1:]119 theta2_copy = theta2[:, 1:]120 J = J + lamb / (2 * m) * (np.sum(theta1_copy ** 2) + np.sum(theta2_copy ** 2))121 print(J)122 returnJ123
124
125 #計算 D
126 defnnGradient(nn_params, X, Y, lamb, sizeList):127 theta =changeForm(nn_params,128 sizeList['theta1_lx'], sizeList['theta1_ly'],129 sizeList['theta2_lx'], sizeList['theta2_ly'])130 theta1 = theta['Theta1']131 theta2 = theta['Theta2']132 m =np.size(X, 0)133 a_res =computeA(theta, X)134 a1 = a_res['a1']135 a2 = a_res['a2']136 a3 = a_res['a3']137 theta1_copy = theta1[:, 1:]138 theta2_copy = theta2[:, 1:]139 #計算δ
140 delta3 = a3 -Y141 delta2 =np.multiply(np.dot(delta3, theta2_copy), sigmoidGradient(np.dot(a1, theta1.T)))142 #計算Δ
143 bigDeilta1 =np.dot(delta2.T, a1)144 bigDeilta2 =np.dot(delta3.T, a2)145 #計算D
146 theta1_grad = bigDeilta1 / m + lamb / m *theta1147 theta2_grad = bigDeilta2 / m + lamb / m *theta2148 theta1_grad[:, 0] = bigDeilta1[:, 0] /m149 theta2_grad[:, 0] = bigDeilta2[:, 0] /m150 #當使用高級優化方法來優化神經網絡時,需要將多個參數矩陣展開,才能傳入優化函數
151 grad =np.r_[theta1_grad.flatten(), theta2_grad.flatten()]152 #print(np.size(grad))
153 returngrad154
155
156 #測試參數的初始化
157 defdebugInitializeWeights(L_out, L_in):158 W = np.arange(1, L_out * (L_in + 1)+1)159 W =np.sin(W)160 W = np.array(W).reshape(L_out, (L_in + 1)) / 10;161 returnW162
163
164 #數值方法計算梯度
165 defcomputeNumericalGradient(theta, X, Y ,lamb, sizeList):166 numgrad =np.zeros(np.size(theta))167 perturb =np.zeros(np.size(theta))168 e = 1e-4
169 for p inrange(0, np.size(theta)):170 perturb[p] =e171 theta_minus = theta -perturb172 theta_plus = theta +perturb173 loss1 =nnCostFunction(theta_minus, X, Y, lamb, sizeList)174 loss2 =nnCostFunction(theta_plus, X, Y, lamb, sizeList)175 numgrad[p] = (loss2 - loss1) / (2 *e)176 perturb[p] =0177 returnnumgrad178
179
180 #梯度檢測函數
181 defcheckNNGradients(lamb):182 #設置測試參數
183 input_layer_size = 3;184 hidden_layer_size = 5;185 num_labels = 3;186 lamb = 1
187 m = 5;188 sizeList = {'theta1_lx': hidden_layer_size,189 'theta1_ly': input_layer_size + 1,190 'theta2_lx': num_labels,191 'theta2_ly': hidden_layer_size + 1} #保存θ大小的參數
192 theta1 =debugInitializeWeights(hidden_layer_size, input_layer_size)193 theta2 =debugInitializeWeights(num_labels, hidden_layer_size)194 theta =np.r_[theta1.flatten(), theta2.flatten()]195 X = debugInitializeWeights(m, input_layer_size - 1)196 y = np.random.randint(0, num_labels, (m, 1))197 #對y進行改寫,改為 m*num_labels 規格的矩陣
198 Y =np.zeros((m, num_labels))199 for i inrange(0, m):200 Y[i, y[i, 0]] = 1
201 grad =nnGradient(theta, X, Y, lamb, sizeList)202 numGrad =computeNumericalGradient(theta, X, Y, lamb, sizeList)203 diff = np.linalg.norm(numGrad - grad) / np.linalg.norm(numGrad +grad)204 print('check NN Gradient: diff =', diff)205
206
207 #使用模型進行預測
208 defpredict(theta1, theta2, X):209 m =np.size(X,0)210 p = np.zeros((np.size(X, 0), 1))211 #第二層計算
212 one =np.ones(m)213 X = np.insert(X, 0, values=one, axis=1)214 a2 =sigmoid(np.dot(X, theta1.T))215 #第三層計算
216 one =np.ones(np.size(a2,0))217 a2 = np.insert(a2, 0, values=one, axis=1)218 a3 =sigmoid(np.dot(a2, theta2.T))219 p = a3.argmax(axis=1) + 1 #y的值為1-10,所以此處0-9要加1
220 returnp.flatten()221
222
223 #——————————————主函數————————————————————
224 #初始化數據
225 input_layer_size = 400
226 hidden_layer_size = 25
227 num_labels = 10
228 sizeList = {'theta1_lx': hidden_layer_size,229 'theta1_ly': input_layer_size + 1,230 'theta2_lx': num_labels,231 'theta2_ly': hidden_layer_size + 1} #保存θ大小的參數
232 lamb = 1
233
234 #加載數據文件
235 data = scio.loadmat('ex4data1.mat')236 X = data['X']237 m =np.size(X, 0)238 y = data['y']239 #對y進行改寫,改為5000*10規格的矩陣,第0-9個位置分別表示1,2,...,9,0
240 Y =np.zeros((m, num_labels))241 for i inrange(0, m):242 Y[i, y[i, 0] - 1] = 1
243 rand_indices = np.random.randint(0, m, 100)244 sel =X[rand_indices, :]245 displayData(sel)246
247 #測試數據θ
248 theta = scio.loadmat('ex4weights.mat')249 theta1 = theta['Theta1']250 theta2 = theta['Theta2']251 nn_theta =np.r_[theta1.flatten(), theta2.flatten()]252
253 #測試nnCostFunction
254 #J = nnCostFunction(nn_theta, X, Y, 3, sizeList)
255 #print(J)
256
257 #測試nnGradient
258 print(nnGradient(nn_theta, X, Y, lamb, sizeList))259
260 #初始化參數
261 nn_params =randInitializeWeights(sizeList)262
263 #梯度檢測
264 #checkNNGradients(lamb)
265
266 #訓練模型
267 res = op.minimize(fun=nnCostFunction,268 x0=nn_params,269 args=(X, Y, lamb, sizeList),270 method='TNC',271 jac=nnGradient,272 options={'maxiter': 100})273 print(res)274
275 #計算準確率
276 all_theta = changeForm(res.x, sizeList['theta1_lx'], sizeList['theta1_ly'],277 sizeList['theta2_lx'], sizeList['theta2_ly'])278 res_theta1 = all_theta['Theta1']279 res_theta2 = all_theta['Theta2']280 pred =predict(res_theta1, res_theta2, X)281 acc = np.mean(pred == y.flatten())*100
282 print('Training Set Accuracy:',acc,'%')283
284 #顯示中間隱藏層
285 displayData(res_theta1[:, 1:])
總結
以上是生活随笔為你收集整理的python拟合求参_机器学习作业(四)神经网络参数的拟合——Python(numpy)实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个好听的蛋糕店名字
- 下一篇: 拒绝了我们的连接请求_职场上,我们该如何