88
99from sklearn import datasets
1010from sklearn .preprocessing import StandardScaler
11+ import time
1112
1213def neuralNetwork (input_layer_size ,hidden_layer_size ,out_put_layer ):
1314 data_img = loadmat_data ("data_digits.mat" )
14- #data_weights = loadmat_data("weights.mat")
1515 X = data_img ['X' ]
1616 y = data_img ['y' ]
17- #Theta1 = data_weights['Theta1']
18- #Theta2 = data_weights['Theta2']
19-
17+
18+ '''scaler = StandardScaler()
19+ scaler.fit(X)
20+ X = scaler.transform(X)'''
2021
21- #y = np.ravel(y)
2222 m ,n = X .shape
2323 """digits = datasets.load_digits()
2424 X = digits.data
@@ -36,13 +36,15 @@ def neuralNetwork(input_layer_size,hidden_layer_size,out_put_layer):
3636 #nn_params = np.vstack((Theta1.reshape(-1,1),Theta2.reshape(-1,1)))
3737
3838 Lambda = 1
39-
39+
4040 initial_Theta1 = randInitializeWeights (input_layer_size ,hidden_layer_size );
4141 initial_Theta2 = randInitializeWeights (hidden_layer_size ,out_put_layer )
4242
4343 initial_nn_params = np .vstack ((initial_Theta1 .reshape (- 1 ,1 ),initial_Theta2 .reshape (- 1 ,1 ))) #展开theta
44- np .savetxt ("testTheta.csv" ,initial_nn_params ,delimiter = "," )
44+ #np.savetxt("testTheta.csv",initial_nn_params,delimiter=",")
45+ start = time .time ()
4546 result = optimize .fmin_cg (nnCostFunction , initial_nn_params , fprime = nnGradient , args = (input_layer_size ,hidden_layer_size ,out_put_layer ,X ,y ,Lambda ))
47+ print time .time ()- start
4648 print result
4749 '''可视化 Theta1'''
4850 length = result .shape [0 ]
@@ -181,6 +183,46 @@ def randInitializeWeights(L_in,L_out):
181183 W = np .random .rand (L_out ,1 + L_in )* 2 * epsilon_init - epsilon_init
182184 return W
183185
186+
187+ # 检验梯度是否计算正确
188+ def checkGradient (Lambda = 0 ):
189+ input_layer_size = 3
190+ hidden_layer_size = 5
191+ num_labels = 3
192+ m = 5
193+ initial_Theta1 = debugInitializeWeights (input_layer_size ,hidden_layer_size );
194+ initial_Theta2 = debugInitializeWeights (hidden_layer_size ,num_labels )
195+ X = debugInitializeWeights (input_layer_size - 1 ,m )
196+ y = 1 + np .transpose (np .mod (np .arange (1 ,m + 1 ), num_labels ))# 初始化y
197+
198+ y = y .reshape (- 1 ,1 )
199+ nn_params = np .vstack ((initial_Theta1 .reshape (- 1 ,1 ),initial_Theta2 .reshape (- 1 ,1 ))) #展开theta
200+ grad = nnGradient (nn_params , input_layer_size , hidden_layer_size ,
201+ num_labels , X , y , Lambda )
202+
203+ num_grad = np .zeros ((nn_params .shape [0 ]))
204+ step = np .zeros ((nn_params .shape [0 ]))
205+ e = 1e-4
206+ for i in range (nn_params .shape [0 ]):
207+ step [i ] = e
208+ loss1 = nnCostFunction (nn_params - step .reshape (- 1 ,1 ), input_layer_size , hidden_layer_size ,
209+ num_labels , X , y ,
210+ Lambda )
211+ loss2 = nnCostFunction (nn_params + step .reshape (- 1 ,1 ), input_layer_size , hidden_layer_size ,
212+ num_labels , X , y ,
213+ Lambda )
214+ num_grad [i ] = (loss2 - loss1 )/ (2 * e )
215+ step [i ]= 0
216+ res = np .hstack ((num_grad .reshape (- 1 ,1 ),grad .reshape (- 1 ,1 )))
217+ print res
218+
219+ # 初始化调试的theta权重
220+ def debugInitializeWeights (fan_in ,fan_out ):
221+ W = np .zeros ((fan_out ,fan_in + 1 ))
222+ x = np .arange (1 ,fan_out * (fan_in + 1 )+ 1 )
223+ W = np .sin (x ).reshape (W .shape )/ 10
224+ return W
225+
184226# 预测
185227def predict (Theta1 ,Theta2 ,X ):
186228 m = X .shape [0 ]
@@ -205,4 +247,5 @@ def predict(Theta1,Theta2,X):
205247 return p
206248
207249if __name__ == "__main__" :
208- neuralNetwork (400 , 25 , 10 )
250+ checkGradient ()
251+ #neuralNetwork(400, 25, 10)
0 commit comments