rnn_demo.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import numpy as np
  2. from model.rnn import RecurrentLayer
  3. from model import IdentityActivator, ReluActivator
  4. def data_set():
  5. x = [np.array([[1], [2], [3]]),
  6. np.array([[2], [3], [4]])]
  7. d = np.array([[1], [2]])
  8. return x, d
  9. def gradient_check():
  10. """ 梯度检查 """
  11. # 设计一个误差函数,取所有节点输出项之和
  12. error_function = lambda o: o.sum()
  13. rl = RecurrentLayer(3, 2, IdentityActivator(), 1e-3)
  14. # 计算forward值
  15. x, d = data_set()
  16. rl.forward(x[0])
  17. rl.forward(x[1])
  18. # 求取sensitivity map
  19. sensitivity_array = np.ones(rl.state_list[-1].shape, dtype=np.float64)
  20. # 计算梯度
  21. rl.backward(sensitivity_array, IdentityActivator())
  22. # 检查梯度
  23. epsilon = 10e-4
  24. for i in range(rl.W.shape[0]):
  25. for j in range(rl.W.shape[1]):
  26. rl.W[i, j] += epsilon
  27. rl.reset_state()
  28. rl.forward(x[0])
  29. rl.forward(x[1])
  30. err1 = error_function(rl.state_list[-1])
  31. rl.W[i, j] -= 2 * epsilon
  32. rl.reset_state()
  33. rl.forward(x[0])
  34. rl.forward(x[1])
  35. err2 = error_function(rl.state_list[-1])
  36. expect_grad = (err1 - err2) / (2 * epsilon)
  37. rl.W[i, j] += epsilon
  38. print('weights(%d,%d): expected - actural %f - %f' % (i, j, expect_grad, rl.gradient[i, j]))
  39. def test():
  40. l = RecurrentLayer(3, 2, ReluActivator(), 1e-3)
  41. x, d = data_set()
  42. l.forward(x[0])
  43. l.forward(x[1])
  44. l.backward(d, ReluActivator())
  45. return x, d, l
  46. if __name__ == '__main__':
  47. # gradient_check()
  48. a, b, rnn = test()
  49. print(a)
  50. print(rnn.gradient_list)