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.

87 lines
2.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "dropoutLayer.h"
  2. //#include "utils.h"
  3. #include "../cuda/cuda.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. dropoutLayer::dropoutLayer(bool AA) : cortexLayer(DROPOUT, AA){
  7. };
  8. dropoutLayer::~dropoutLayer(){
  9. };
  10. bool dropoutLayer::create(uint32_t batch, uint32_t inputs, float probability){
  11. m_fProbability = probability;
  12. m_nInput = inputs;
  13. m_nOutput = inputs;
  14. m_nBatch = batch;
  15. m_pRand = (float*)calloc(m_nInput*m_nBatch, sizeof(float));
  16. if (m_pRand == NULL)return false;
  17. m_fScale = 1.f / (1.f - m_fProbability);
  18. // On Creation CUDA memory may be Unbounded from Host mem
  19. //m_pGpuRand = m_pPU->makeFloatArray(m_nInput*m_nBatch, m_pRand );
  20. //if (m_pGpuRand == NULL) return false;
  21. memset(m_pRand, 0, sizeof(float)*m_nInput*m_nBatch);
  22. m_pGpuRand = NULL;
  23. fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs);
  24. return true;
  25. };
  26. bool dropoutLayer::resize(uint32_t w, uint32_t h){
  27. m_pRand = (float*)realloc(m_pRand, m_nInput*m_nBatch*sizeof(float));
  28. #ifdef GPU
  29. m_pPU->freeMem(m_pGpuRand);
  30. m_pGpuRand = m_pPU->makeFloatArray(m_nInput * m_nBatch, m_pRand );
  31. #endif
  32. return true;
  33. };
  34. /*
  35. void activationLayer::forward(cortexSTATE state){
  36. #ifdef GPU
  37. if (m_bUseGPU){
  38. }
  39. else {
  40. #endif
  41. #ifdef GPU
  42. };
  43. #endif
  44. };
  45. void activationLayer::backward(cortexSTATE state){
  46. #ifdef GPU
  47. if (m_bUseGPU){
  48. }
  49. else {
  50. #endif
  51. #ifdef GPU
  52. };
  53. #endif
  54. };
  55. */
  56. //void dropoutLayer::update( int batch, float learning_rate, float momentum, float decay);
  57. void dropoutLayer::forward(cortexSTATE state){
  58. if (!state.train) return;
  59. for( uint32_t i = 0; i < m_nBatch * m_nInput; ++i){
  60. float r = rand_uniform(0, 1);
  61. m_pRand[i] = r;
  62. if(r < m_fProbability) state.input[i] = 0;
  63. else state.input[i] *= m_fScale;
  64. };
  65. };
  66. void dropoutLayer::backward(cortexSTATE state){
  67. if(!state.delta) return;
  68. for (uint32_t i = 0; i < m_nBatch * m_nInput; ++i){
  69. float r = m_pRand[i];
  70. if(r < m_fProbability) state.delta[i] = 0;
  71. else state.delta[i] *= m_fScale;
  72. };
  73. };