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.

80 lines
2.7 KiB

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