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.

69 lines
3.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cpu.h"
  2. #include "../layers/localLayer.h"
  3. void cpuHOST::forward_layer(localLayer *layer, cortexSTATE state){
  4. uint32_t out_h = (layer->m_nPad == 0) ? ((layer->m_nIn_Height - layer->m_nSize) / (layer->m_nStride + 1)) : ((layer->m_nIn_Height - 1) / (layer->m_nStride + 1));
  5. uint32_t out_w = (layer->m_nPad == 0) ? ((layer->m_nIn_Width - layer->m_nSize) / (layer->m_nStride + 1)) : ((layer->m_nIn_Width - 1) / (layer->m_nStride + 1));
  6. uint32_t locations = out_h * out_w;
  7. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  8. makeCopy(layer->m_nOutput, layer->m_pBias, 1, layer->m_pOutput + i*layer->m_nOutput, 1);
  9. };
  10. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  11. float *input = state.input + i*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nIn_Channels;
  12. im2col(layer->m_pInput, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pColImage);
  13. float *output = layer->m_pOutput + i*layer->m_nOutput;
  14. for (uint32_t j = 0; j < locations; ++j){
  15. float *a = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN;
  16. float *b = layer->m_pColImage + j;
  17. float *c = output + j;
  18. int m = layer->m_nN;
  19. int n = 1;
  20. int k = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
  21. gemm(0, 0, m, n, k, 1, a, k, b, locations, 1, c, locations);
  22. };
  23. };
  24. makeActivatedArray(layer->m_pOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation);
  25. };
  26. void cpuHOST::backward_layer(localLayer *layer, cortexSTATE state){
  27. uint32_t locations = layer->m_nOut_Width*layer->m_nOut_Height;
  28. makeGradientArray(layer->m_pOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation, layer->m_pDelta);
  29. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  30. calcAxpy(layer->m_nOutput, 1, layer->m_pDelta + i*layer->m_nOutput, 1, layer->m_pBiasUpdate, 1);
  31. };
  32. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  33. float *input = state.input + i*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nIn_Channels;
  34. im2col(layer->m_pInput, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pColImage);
  35. for (uint32_t j = 0; j < locations; ++j){
  36. float *a = layer->m_pDelta + i*layer->m_nOutput + j;
  37. float *b = layer->m_pColImage + j;
  38. float *c = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN;
  39. int m = layer->m_nN;
  40. int n = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
  41. int k = 1;
  42. gemm(0, 1, m, n, k, 1, a, locations, b, locations, 1, c, n);
  43. };
  44. if (state.delta != NULL){
  45. for (uint32_t j = 0; j < locations; ++j){
  46. float *a = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN;
  47. float *b = layer->m_pDelta + i*layer->m_nOutput + j;
  48. float *c = layer->m_pColImage + j;
  49. int m = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
  50. int n = 1;
  51. int k = layer->m_nN;
  52. gemm(1, 0, m, n, k, 1, a, m, b, locations, 0, c, locations);
  53. };
  54. col2im(layer->m_pColImage, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.delta + i*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nIn_Channels);
  55. };
  56. };
  57. };
  58. void cpuHOST::update_layer(localLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  59. int locations = layer->m_nOut_Width*layer->m_nOut_Height;
  60. int size = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN*locations;
  61. calcAxpy(layer->m_nOutput, learning_rate / batch, layer->m_pBiasUpdate, 1, layer->m_pBias, 1);
  62. calcScal(layer->m_nOutput, momentum, layer->m_pBiasUpdate, 1);
  63. calcAxpy(size, -decay*batch, layer->m_pWeight, 1, layer->m_pWeightUpdate, 1);
  64. calcAxpy(size, learning_rate / batch, layer->m_pWeightUpdate, 1, layer->m_pWeight, 1);
  65. calcScal(size, momentum, layer->m_pWeightUpdate, 1);
  66. };