1. def gradcheck_naive(f, x):
2. """
3. 对一个函数f求梯度的梯度检验
4. - f 输入x,然后输出loss和梯度的函数
5. - x 就是输入咯
6. """
7. rndstate = random.getstate()
8. random.setstate(rndstate)
9. fx, grad = f(x)
10. h = 1e-4
11. # 遍历x的每一维
12. it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
13. while not it.finished:
14. ix = it.multi_index
15. old_val = x[ix]
16. x[ix] = old_val - h
17. random.setstate(rndstate)
18. ( fxh1, _ ) = f(x)
19. x[ix] = old_val + h
20. random.setstate(rndstate)
21. ( fxh2, _ ) = f(x)
22. numgrad = (fxh2 - fxh1)/(2*h)
23. x[ix] = old_val
24. # 比对梯度
25. reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad),
abs(grad[ix]))
26. if reldiff > 1e-5:
27. print "Gradient check failed."
28. print "First gradient error found at index %s" % str(ix)
29. print "Your gradient: %f \t Numerical gradient: %f" % (grad[ix],
numgrad)
30. return
31. it.iternext() # Step to next dimension
32. print "Gradient check passed!"
(part g) (8 分)
现在,在q2 neural.py中,写出只有一个隐层且激活函数为sigmoid的神经网络前向和后向传播代码。使用
python q2_neural.py测试自己的代码。
旁白:一入DL深似海...
1. def forward_backward_prop(data, labels, params, verbose = False):
2. """
3. 2个隐层的神经网络的前向运算和反向传播
4. """
5. if len(data.shape) >= 2: