You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cpu.h"
  2. #include "../layers/crnnLayer.h"
  3. void cpuHOST::forward_layer(crnnLayer *layer, cortexSTATE state){
  4. cortexSTATE s = { 0 };
  5. s.train = state.train;
  6. cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
  7. cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
  8. cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
  9. uint32_t CC = layer->m_nHidden * layer->m_nBatch;
  10. makeFill(layer->m_nOutput * layer->m_nBatch * layer->m_nSteps, 0, output_layer->m_pDelta, 1);
  11. makeFill(CC * layer->m_nSteps, 0, self_layer->m_pDelta, 1);
  12. makeFill(CC * layer->m_nSteps, 0, input_layer->m_pDelta, 1);
  13. if (state.train) makeFill(CC, 0, layer->m_pState, 1);
  14. for (uint32_t i = 0; i < layer->m_nSteps; ++i) {
  15. s.input = state.input;
  16. forward_layer((convolutionalLayer*)input_layer, s);
  17. s.input = layer->m_pState;
  18. forward_layer((convolutionalLayer*)self_layer, s);
  19. float *old_state = layer->m_pState;
  20. if (state.train) layer->m_pState += CC;
  21. if (layer->m_bShortcut){
  22. makeCopy(CC, old_state, 1, layer->m_pState, 1);
  23. }else{
  24. makeFill(CC, 0, layer->m_pState, 1);
  25. };
  26. calcAxpy(CC, 1, input_layer->m_pOutput, 1, layer->m_pState, 1);
  27. calcAxpy(CC, 1, self_layer->m_pOutput, 1, layer->m_pState, 1);
  28. s.input = layer->m_pState;
  29. forward_layer((convolutionalLayer*)output_layer, s);
  30. state.input += layer->m_nInput*layer->m_nBatch;
  31. incrementLayer(input_layer, 1);
  32. incrementLayer(self_layer, 1);
  33. incrementLayer(output_layer, 1);
  34. };
  35. };
  36. void cpuHOST::backward_layer(crnnLayer *layer, cortexSTATE state){
  37. cortexSTATE s = { 0 };
  38. s.train = state.train;
  39. cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
  40. cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
  41. cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
  42. incrementLayer(input_layer, layer->m_nSteps - 1);
  43. incrementLayer(self_layer, layer->m_nSteps - 1);
  44. incrementLayer(output_layer, layer->m_nSteps - 1);
  45. uint32_t CC = layer->m_nHidden * layer->m_nBatch;
  46. layer->m_pState += CC*layer->m_nSteps;
  47. for (uint32_t i = layer->m_nSteps - 1; i >= 0; --i) {
  48. makeCopy(CC, input_layer->m_pOutput, 1, layer->m_pState, 1);
  49. calcAxpy(CC, 1, self_layer->m_pOutput, 1, layer->m_pState, 1);
  50. s.input = layer->m_pState;
  51. s.delta = self_layer->m_pDelta;
  52. backward_layer((convolutionalLayer*)output_layer, s);
  53. layer->m_pState -= CC;
  54. /*
  55. if(i > 0){
  56. makeCopy(CC, input_layer.output - CC, 1, l.state, 1);
  57. calcAxpy(CC, 1, self_layer.output - CC, 1, l.state, 1);
  58. }else{
  59. makeFill(CC, 0, l.state, 1);
  60. }
  61. */
  62. s.input = layer->m_pState;
  63. s.delta = self_layer->m_pDelta - CC;
  64. if (i == 0) s.delta = 0;
  65. backward_layer((convolutionalLayer*)self_layer, s);
  66. makeCopy(CC, self_layer->m_pDelta, 1, input_layer->m_pDelta, 1);
  67. if (i > 0 && layer->m_bShortcut) calcAxpy(CC, 1, self_layer->m_pDelta, 1, self_layer->m_pDelta - CC, 1);
  68. s.input = state.input + i*layer->m_nInput*layer->m_nBatch;
  69. if (state.delta) s.delta = state.delta + i*layer->m_nInput*layer->m_nBatch;
  70. else s.delta = 0;
  71. backward_layer((convolutionalLayer*)input_layer, s);
  72. incrementLayer(input_layer, -1);
  73. incrementLayer(self_layer, -1);
  74. incrementLayer(output_layer, -1);
  75. };
  76. };
  77. void cpuHOST::update_layer(crnnLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  78. update_layer(layer->m_pInputLayer, batch, learning_rate, momentum, decay);
  79. update_layer(layer->m_pSelfLayer, batch, learning_rate, momentum, decay);
  80. update_layer(layer->m_pOutputLayer, batch, learning_rate, momentum, decay);
  81. };