recursive_demo.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import numpy as np
  2. from model import IdentityActivator
  3. from model.recursive import RecursiveLayer, TreeNode
  4. def data_set():
  5. """ 初始化数据 """
  6. children = [
  7. TreeNode(np.array([[1], [2]])),
  8. TreeNode(np.array([[3], [4]])),
  9. TreeNode(np.array([[5], [6]]))
  10. ]
  11. d = np.array([[0.5], [0.8]])
  12. return children, d
  13. def gradient_check():
  14. """ 梯度检查 """
  15. # 设计一个误差函数,取所有节点输出项之和
  16. error_function = lambda o: o.sum()
  17. rnn = RecursiveLayer(2, 2, IdentityActivator(), 1e-3)
  18. # 计算forward值
  19. x, d = data_set()
  20. rnn.forward(x[0], x[1])
  21. rnn.forward(rnn.root, x[2])
  22. # 求取sensitivity map
  23. sensitivity_array = np.ones((rnn.node_width, 1),
  24. dtype=np.float64)
  25. # 计算梯度
  26. rnn.backward(sensitivity_array)
  27. # 检查梯度
  28. epsilon = 10e-4
  29. for i in range(rnn.W.shape[0]):
  30. for j in range(rnn.W.shape[1]):
  31. rnn.W[i, j] += epsilon
  32. rnn.reset_state()
  33. rnn.forward(x[0], x[1])
  34. rnn.forward(rnn.root, x[2])
  35. err1 = error_function(rnn.root.data)
  36. rnn.W[i, j] -= 2 * epsilon
  37. rnn.reset_state()
  38. rnn.forward(x[0], x[1])
  39. rnn.forward(rnn.root, x[2])
  40. err2 = error_function(rnn.root.data)
  41. expect_grad = (err1 - err2) / (2 * epsilon)
  42. rnn.W[i, j] += epsilon
  43. print('weights(%d,%d): expected - actural %.4e - %.4e' % (i, j, expect_grad, rnn.W_grad[i, j]))
  44. return rnn
  45. def test():
  46. children, d = data_set()
  47. rnn = RecursiveLayer(2, 2, IdentityActivator(), 1e-3)
  48. rnn.forward(children[0], children[1])
  49. rnn.dump()
  50. rnn.forward(rnn.root, children[2])
  51. rnn.dump()
  52. rnn.backward(d)
  53. rnn.dump(dump_grad='true')
  54. return rnn
  55. if __name__ == '__main__':
  56. # gradient_check()
  57. test()