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.

40 lines
2.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "../base/basicProcessingUnit.h"
  2. #include "../layers/avgpoolLayer.h"
  3. #include "../layers/batchnormLayer.h"
  4. #include "../layers/connectedLayer.h"
  5. #include "../layers/convolutionalLayer.h"
  6. #define LOG( X ) m_pLog->Log( "PU", this, X )
  7. #define LOG_ERROR( X ) m_pLog->Log( "PU", this, _LOG_ERROR, X )
  8. #define SLOG( X ) me->m_pLog->Log( "PU-THREAD", NULL, X )
  9. #define SLOG_ERROR( X ) me->m_pLog->Log( "PU-THREAD", NULL, _LOG_ERROR, X )
  10. void cortexProcessingUnit::update_layer(connectedLayer *layer, int batch, float learning_rate, float momentum, float decay){
  11. calcAxpy(layer->m_nOutput, learning_rate / batch, layer->m_pBiasUpdate, 1, layer->m_pBias, 1);
  12. calcScal(layer->m_nOutput, momentum, layer->m_pBiasUpdate, 1);
  13. if (layer->m_bBatchNormalize){
  14. calcAxpy(layer->m_nOutput, learning_rate / batch, layer->m_pScaleUpdate, 1, layer->m_pScale, 1);
  15. calcScal(layer->m_nOutput, momentum, layer->m_pScaleUpdate, 1);
  16. };
  17. calcAxpy(layer->m_nWeight, -decay*batch, layer->m_pWeight, 1, layer->m_pWeightUpdate, 1);
  18. calcAxpy(layer->m_nWeight, learning_rate / batch, layer->m_pWeightUpdate, 1, layer->m_pWeight, 1);
  19. calcScal(layer->m_nWeight, momentum, layer->m_pWeightUpdate, 1);
  20. };
  21. void cortexProcessingUnit::update_layer(convolutionalLayer *layer, int batch, float learning_rate, float momentum, float decay){
  22. int size = layer->m_nSize * layer->m_nSize * layer->m_nIn_Channels * layer->m_nN;
  23. calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pBiasUpdate, 1, layer->m_pBias, 1);
  24. calcScal(layer->m_nN, momentum, layer->m_pBiasUpdate, 1);
  25. if (layer->m_pScale){
  26. calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pScaleUpdate, 1, layer->m_pScale, 1);
  27. calcScal(layer->m_nN, momentum, layer->m_pScaleUpdate, 1);
  28. };
  29. calcAxpy(size, -decay*batch, layer->m_pWeight, 1, layer->m_pWeightUpdate, 1);
  30. calcAxpy(size, learning_rate / batch, layer->m_pWeightUpdate, 1, layer->m_pWeight, 1);
  31. calcScal(size, momentum, layer->m_pWeightUpdate, 1);
  32. };
  33. #undef LOG
  34. #undef LOG_ERROR
  35. #undef SLOG
  36. #undef SLOG_ERROR