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.

88 lines
3.1 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef ACTIVATIONS_H
  2. #define ACTIVATIONS_H
  3. #include "darknet.h"
  4. #include "dn_cuda.h"
  5. #include "math.h"
  6. namespace DN{
  7. ACTIVATION get_activation(char *s);
  8. char *get_activation_string(ACTIVATION a);
  9. float activate(float x, ACTIVATION a);
  10. float gradient(float x, ACTIVATION a);
  11. void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta);
  12. void activate_array(float *x, const int n, const ACTIVATION a);
  13. #ifdef GPU
  14. void activate_array_gpu(float *x, int n, ACTIVATION a);
  15. void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta);
  16. #endif
  17. static __inline float stair_activate(float x)
  18. {
  19. int n = (int)floor(x);
  20. if (n % 2 == 0) return (float)floor(x / 2.);
  21. else return (float)((x - n) + floor(x / 2.));
  22. }
  23. static __inline float hardtan_activate(float x)
  24. {
  25. if (x < -1) return -1;
  26. if (x > 1) return 1;
  27. return x;
  28. }
  29. static __inline float linear_activate(float x){ return x; }
  30. static __inline float logistic_activate(float x){ return (float)(1. / (1. + exp(-x))); }
  31. static __inline float loggy_activate(float x){ return (float)(2. / (1. + exp(-x)) - 1); }
  32. static __inline float relu_activate(float x){ return x*(x > 0); }
  33. static __inline float elu_activate(float x){ return (float)((x >= 0)*x + (x < 0)*(exp(x) - 1)); }
  34. static __inline float relie_activate(float x){ return (float)((x>0) ? x : .01*x); }
  35. static __inline float ramp_activate(float x){ return (float)(x*(x > 0) + .1*x); }
  36. static __inline float leaky_activate(float x){ return (float)((x > 0) ? x : .1*x); }
  37. static __inline float tanh_activate(float x){ return (float)((exp(2 * x) - 1) / (exp(2 * x) + 1)); }
  38. static __inline float plse_activate(float x)
  39. {
  40. if (x < -4) return (float)(.01 * (x + 4));
  41. if (x > 4) return (float)(.01 * (x - 4) + 1);
  42. return (float)(.125*x + .5);
  43. }
  44. static __inline float lhtan_activate(float x)
  45. {
  46. if (x < 0) return (float)(.001*x);
  47. if (x > 1) return (float)(.001*(x - 1) + 1);
  48. return x;
  49. }
  50. static __inline float lhtan_gradient(float x)
  51. {
  52. if (x > 0 && x < 1) return (float)1;
  53. return (float).001;
  54. }
  55. static __inline float hardtan_gradient(float x)
  56. {
  57. if (x > -1 && x < 1) return (float)1;
  58. return (float)0;
  59. }
  60. static __inline float linear_gradient(float x){ return (float)1; }
  61. static __inline float logistic_gradient(float x){ return (float)((1 - x)*x); }
  62. static __inline float loggy_gradient(float x)
  63. {
  64. float y = (float)((x + 1.) / 2.);
  65. return (float)(2 * (1 - y)*y);
  66. }
  67. static __inline float stair_gradient(float x)
  68. {
  69. if (floor(x) == x) return (float)0;
  70. return (float)1;
  71. }
  72. static __inline float relu_gradient(float x){ return (float)(x > 0); }
  73. static __inline float elu_gradient(float x){ return (float)((x >= 0) + (x < 0)*(x + 1)); }
  74. static __inline float relie_gradient(float x){ return (float)((x>0) ? 1 : .01); }
  75. static __inline float ramp_gradient(float x){ return (float)((x > 0) + .1); }
  76. static __inline float leaky_gradient(float x){ return (float)((x > 0) ? 1 : .1); }
  77. static __inline float tanh_gradient(float x){ return (float)(1 - x*x); }
  78. static __inline float plse_gradient(float x){ return (float)((x < 0 || x > 1) ? .01 : .125); }
  79. };
  80. #endif