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.

147 lines
2.9 KiB

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