MCPcopy Index your code
hub / github.com/lawlite19/MachineLearning_Python / checkGradient

Function checkGradient

NeuralNetwok/NeuralNetwork.py:199–232  ·  view source on GitHub ↗

构造一个小型的神经网络验证,因为数值法计算梯度很浪费时间,而且验证正确后之后就不再需要验证了

(Lambda = 0)

Source from the content-addressed store, hash-verified

197
198# 检验梯度是否计算正确
199def checkGradient(Lambda = 0):
200 '''构造一个小型的神经网络验证,因为数值法计算梯度很浪费时间,而且验证正确后之后就不再需要验证了'''
201 input_layer_size = 3
202 hidden_layer_size = 5
203 num_labels = 3
204 m = 5
205 initial_Theta1 = debugInitializeWeights(input_layer_size,hidden_layer_size);
206 initial_Theta2 = debugInitializeWeights(hidden_layer_size,num_labels)
207 X = debugInitializeWeights(input_layer_size-1,m)
208 y = np.transpose(np.mod(np.arange(1,m+1), num_labels))# 初始化y
209
210 y = y.reshape(-1,1)
211 nn_params = np.vstack((initial_Theta1.reshape(-1,1),initial_Theta2.reshape(-1,1))) #展开theta
212 '''BP求出梯度'''
213 grad = nnGradient(nn_params, input_layer_size, hidden_layer_size,
214 num_labels, X, y, Lambda)
215 '''使用数值法计算梯度'''
216 num_grad = np.zeros((nn_params.shape[0]))
217 step = np.zeros((nn_params.shape[0]))
218 e = 1e-4
219 for i in range(nn_params.shape[0]):
220 step[i] = e
221 loss1 = nnCostFunction(nn_params-step.reshape(-1,1), input_layer_size, hidden_layer_size,
222 num_labels, X, y,
223 Lambda)
224 loss2 = nnCostFunction(nn_params+step.reshape(-1,1), input_layer_size, hidden_layer_size,
225 num_labels, X, y,
226 Lambda)
227 num_grad[i] = (loss2-loss1)/(2*e)
228 step[i]=0
229 # 显示两列比较
230 res = np.hstack((num_grad.reshape(-1,1),grad.reshape(-1,1)))
231 print("检查梯度的结果,第一列为数值法计算得到的,第二列为BP得到的:")
232 print (res)
233
234# 初始化调试的theta权重
235def debugInitializeWeights(fan_in,fan_out):

Callers 1

NeuralNetwork.pyFile · 0.85

Calls 3

debugInitializeWeightsFunction · 0.85
nnGradientFunction · 0.85
nnCostFunctionFunction · 0.85

Tested by

no test coverage detected