cnn_demo.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import numpy as np
  2. from model.cnn import ConvLayer, MaxPoolingLayer
  3. from model import IdentityActivator
  4. def init_test():
  5. a = np.array(
  6. [[[0, 1, 1, 0, 2],
  7. [2, 2, 2, 2, 1],
  8. [1, 0, 0, 2, 0],
  9. [0, 1, 1, 0, 0],
  10. [1, 2, 0, 0, 2]],
  11. [[1, 0, 2, 2, 0],
  12. [0, 0, 0, 2, 0],
  13. [1, 2, 1, 2, 1],
  14. [1, 0, 0, 0, 0],
  15. [1, 2, 1, 1, 1]],
  16. [[2, 1, 2, 0, 0],
  17. [1, 0, 0, 1, 0],
  18. [0, 2, 1, 0, 1],
  19. [0, 1, 2, 2, 2],
  20. [2, 1, 0, 0, 1]]])
  21. b = np.array(
  22. [[[0, 1, 1],
  23. [2, 2, 2],
  24. [1, 0, 0]],
  25. [[1, 0, 2],
  26. [0, 0, 0],
  27. [1, 2, 1]]])
  28. cl = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, IdentityActivator(), 0.001)
  29. cl.filters[0].weights = np.array(
  30. [[[-1, 1, 0],
  31. [0, 1, 0],
  32. [0, 1, 1]],
  33. [[-1, -1, 0],
  34. [0, 0, 0],
  35. [0, -1, 0]],
  36. [[0, 0, -1],
  37. [0, 1, 0],
  38. [1, -1, -1]]], dtype=np.float64)
  39. cl.filters[0].bias = 1
  40. cl.filters[1].weights = np.array(
  41. [[[1, 1, -1],
  42. [-1, -1, 1],
  43. [0, -1, 1]],
  44. [[0, 1, 0],
  45. [-1, 0, -1],
  46. [-1, 1, 0]],
  47. [[-1, 0, 0],
  48. [-1, 0, 1],
  49. [-1, 0, 0]]], dtype=np.float64)
  50. return a, b, cl
  51. def gradient_check():
  52. """ 梯度检查 """
  53. # 设计一个误差函数,取所有节点输出项之和
  54. error_function = lambda o: o.sum()
  55. # 计算forward值
  56. a, b, cl = init_test()
  57. cl.forward(a)
  58. # 求取sensitivity map,是一个全1数组
  59. sensitivity_array = np.ones(cl.output_array.shape, dtype=np.float64)
  60. # 计算梯度
  61. cl.backward(a, sensitivity_array, IdentityActivator())
  62. # 检查梯度
  63. epsilon = 10e-4
  64. for d in range(cl.filters[0].weights_grad.shape[0]):
  65. for i in range(cl.filters[0].weights_grad.shape[1]):
  66. for j in range(cl.filters[0].weights_grad.shape[2]):
  67. cl.filters[0].weights[d, i, j] += epsilon
  68. cl.forward(a)
  69. err1 = error_function(cl.output_array)
  70. cl.filters[0].weights[d, i, j] -= 2 * epsilon
  71. cl.forward(a)
  72. err2 = error_function(cl.output_array)
  73. expect_grad = (err1 - err2) / (2 * epsilon)
  74. cl.filters[0].weights[d, i, j] += epsilon
  75. print('weights(%d,%d,%d): expected - actural %f - %f' % (
  76. d, i, j, expect_grad, cl.filters[0].weights_grad[d, i, j]))
  77. def init_pool_test():
  78. a = np.array(
  79. [[[1, 1, 2, 4],
  80. [5, 6, 7, 8],
  81. [3, 2, 1, 0],
  82. [1, 2, 3, 4]],
  83. [[0, 1, 2, 3],
  84. [4, 5, 6, 7],
  85. [8, 9, 0, 1],
  86. [3, 4, 5, 6]]], dtype=np.float64)
  87. b = np.array(
  88. [[[1, 2],
  89. [2, 4]],
  90. [[3, 5],
  91. [8, 2]]], dtype=np.float64)
  92. mpl = MaxPoolingLayer(4, 4, 2, 2, 2, 2)
  93. return a, b, mpl
  94. def test_pool():
  95. a, b, mpl = init_pool_test()
  96. mpl.forward(a)
  97. print('input array:\n%s\noutput array:\n%s' % (a, mpl.output_array))
  98. if __name__ == '__main__':
  99. # gradient_check()
  100. test_pool()