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.

135 lines
3.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicActivations.h"
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. char *getActivationString(ACTIVATION a){
  7. switch(a){
  8. case LOGISTIC:
  9. return "logistic";
  10. case LOGGY:
  11. return "loggy";
  12. case RELU:
  13. return "relu";
  14. case ELU:
  15. return "elu";
  16. case RELIE:
  17. return "relie";
  18. case RAMP:
  19. return "ramp";
  20. case LINEAR:
  21. return "linear";
  22. case TANH:
  23. return "tanh";
  24. case PLSE:
  25. return "plse";
  26. case LEAKY:
  27. return "leaky";
  28. case STAIR:
  29. return "stair";
  30. case HARDTAN:
  31. return "hardtan";
  32. case LHTAN:
  33. return "lhtan";
  34. default:
  35. break;
  36. }
  37. return "relu";
  38. };
  39. ACTIVATION getActivation(char *s){
  40. if (strcmp(s, "logistic")==0) return LOGISTIC;
  41. if (strcmp(s, "loggy")==0) return LOGGY;
  42. if (strcmp(s, "relu")==0) return RELU;
  43. if (strcmp(s, "elu")==0) return ELU;
  44. if (strcmp(s, "relie")==0) return RELIE;
  45. if (strcmp(s, "plse")==0) return PLSE;
  46. if (strcmp(s, "hardtan")==0) return HARDTAN;
  47. if (strcmp(s, "lhtan")==0) return LHTAN;
  48. if (strcmp(s, "linear")==0) return LINEAR;
  49. if (strcmp(s, "ramp")==0) return RAMP;
  50. if (strcmp(s, "leaky")==0) return LEAKY;
  51. if (strcmp(s, "tanh")==0) return TANH;
  52. if (strcmp(s, "stair")==0) return STAIR;
  53. fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s);
  54. return RELU;
  55. };
  56. float activate(float x, ACTIVATION a){
  57. switch(a){
  58. case LINEAR:
  59. return linear_activate(x);
  60. case LOGISTIC:
  61. return logistic_activate(x);
  62. case LOGGY:
  63. return loggy_activate(x);
  64. case RELU:
  65. return relu_activate(x);
  66. case ELU:
  67. return elu_activate(x);
  68. case RELIE:
  69. return relie_activate(x);
  70. case RAMP:
  71. return ramp_activate(x);
  72. case LEAKY:
  73. return leaky_activate(x);
  74. case TANH:
  75. return tanh_activate(x);
  76. case PLSE:
  77. return plse_activate(x);
  78. case STAIR:
  79. return stair_activate(x);
  80. case HARDTAN:
  81. return hardtan_activate(x);
  82. case LHTAN:
  83. return lhtan_activate(x);
  84. };
  85. return 0.f;
  86. };
  87. void activateArray(float *x, uint32_t n, const ACTIVATION a){
  88. for(uint32_t i = 0; i < n; ++i){
  89. x[i] = activate(x[i], a);
  90. };
  91. };
  92. float gradient(float x, ACTIVATION a){
  93. switch(a){
  94. case LINEAR:
  95. return linear_gradient(x);
  96. case LOGISTIC:
  97. return logistic_gradient(x);
  98. case LOGGY:
  99. return loggy_gradient(x);
  100. case RELU:
  101. return relu_gradient(x);
  102. case ELU:
  103. return elu_gradient(x);
  104. case RELIE:
  105. return relie_gradient(x);
  106. case RAMP:
  107. return ramp_gradient(x);
  108. case LEAKY:
  109. return leaky_gradient(x);
  110. case TANH:
  111. return tanh_gradient(x);
  112. case PLSE:
  113. return plse_gradient(x);
  114. case STAIR:
  115. return stair_gradient(x);
  116. case HARDTAN:
  117. return hardtan_gradient(x);
  118. case LHTAN:
  119. return lhtan_gradient(x);
  120. };
  121. return 0.f;
  122. };
  123. void gradientArray( float *x, uint32_t n, ACTIVATION a, float *delta){
  124. for(uint32_t i = 0; i < n; ++i){
  125. delta[i] *= gradient(x[i], a);
  126. };
  127. };