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.

168 lines
4.7 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. extern "C" {
  5. #include "activations.h"
  6. #include "dn_cuda.h"
  7. }
  8. namespace DN{
  9. __device__ float lhtan_activate_kernel(float x)
  10. {
  11. if (x < 0) return .001*x;
  12. if (x > 1) return .001*(x - 1) + 1;
  13. return x;
  14. }
  15. __device__ float lhtan_gradient_kernel(float x)
  16. {
  17. if (x > 0 && x < 1) return 1;
  18. return .001;
  19. }
  20. __device__ float hardtan_activate_kernel(float x)
  21. {
  22. if (x < -1) return -1;
  23. if (x > 1) return 1;
  24. return x;
  25. }
  26. __device__ float linear_activate_kernel(float x){ return x; }
  27. __device__ float logistic_activate_kernel(float x){ return 1. / (1. + exp(-x)); }
  28. __device__ float loggy_activate_kernel(float x){ return 2. / (1. + exp(-x)) - 1; }
  29. __device__ float relu_activate_kernel(float x){ return x*(x > 0); }
  30. __device__ float elu_activate_kernel(float x){ return (x >= 0)*x + (x < 0)*(exp(x) - 1); }
  31. __device__ float relie_activate_kernel(float x){ return (x>0) ? x : .01*x; }
  32. __device__ float ramp_activate_kernel(float x){ return x*(x > 0) + .1*x; }
  33. __device__ float leaky_activate_kernel(float x){ return (x > 0) ? x : .1*x; }
  34. __device__ float tanh_activate_kernel(float x){ return (2 / (1 + exp(-2 * x)) - 1); }
  35. __device__ float plse_activate_kernel(float x)
  36. {
  37. if (x < -4) return .01 * (x + 4);
  38. if (x > 4) return .01 * (x - 4) + 1;
  39. return .125*x + .5;
  40. }
  41. __device__ float stair_activate_kernel(float x)
  42. {
  43. int n = floor(x);
  44. if (n % 2 == 0) return floor(x / 2.);
  45. else return (x - n) + floor(x / 2.);
  46. }
  47. __device__ float hardtan_gradient_kernel(float x)
  48. {
  49. if (x > -1 && x < 1) return 1;
  50. return 0;
  51. }
  52. __device__ float linear_gradient_kernel(float x){ return 1; }
  53. __device__ float logistic_gradient_kernel(float x){ return (1 - x)*x; }
  54. __device__ float loggy_gradient_kernel(float x)
  55. {
  56. float y = (x + 1.) / 2.;
  57. return 2 * (1 - y)*y;
  58. }
  59. __device__ float relu_gradient_kernel(float x){ return (x>0); }
  60. __device__ float elu_gradient_kernel(float x){ return (x >= 0) + (x < 0)*(x + 1); }
  61. __device__ float relie_gradient_kernel(float x){ return (x>0) ? 1 : .01; }
  62. __device__ float ramp_gradient_kernel(float x){ return (x > 0) + .1; }
  63. __device__ float leaky_gradient_kernel(float x){ return (x > 0) ? 1 : .1; }
  64. __device__ float tanh_gradient_kernel(float x){ return 1 - x*x; }
  65. __device__ float plse_gradient_kernel(float x){ return (x < 0 || x > 1) ? .01 : .125; }
  66. __device__ float stair_gradient_kernel(float x)
  67. {
  68. if (floor(x) == x) return 0;
  69. return 1;
  70. }
  71. __device__ float activate_kernel(float x, ACTIVATION a)
  72. {
  73. switch (a){
  74. case LINEAR:
  75. return linear_activate_kernel(x);
  76. case LOGISTIC:
  77. return logistic_activate_kernel(x);
  78. case LOGGY:
  79. return loggy_activate_kernel(x);
  80. case RELU:
  81. return relu_activate_kernel(x);
  82. case ELU:
  83. return elu_activate_kernel(x);
  84. case RELIE:
  85. return relie_activate_kernel(x);
  86. case RAMP:
  87. return ramp_activate_kernel(x);
  88. case LEAKY:
  89. return leaky_activate_kernel(x);
  90. case TANH:
  91. return tanh_activate_kernel(x);
  92. case PLSE:
  93. return plse_activate_kernel(x);
  94. case STAIR:
  95. return stair_activate_kernel(x);
  96. case HARDTAN:
  97. return hardtan_activate_kernel(x);
  98. case LHTAN:
  99. return lhtan_activate_kernel(x);
  100. }
  101. return 0;
  102. }
  103. __device__ float gradient_kernel(float x, ACTIVATION a)
  104. {
  105. switch (a){
  106. case LINEAR:
  107. return linear_gradient_kernel(x);
  108. case LOGISTIC:
  109. return logistic_gradient_kernel(x);
  110. case LOGGY:
  111. return loggy_gradient_kernel(x);
  112. case RELU:
  113. return relu_gradient_kernel(x);
  114. case ELU:
  115. return elu_gradient_kernel(x);
  116. case RELIE:
  117. return relie_gradient_kernel(x);
  118. case RAMP:
  119. return ramp_gradient_kernel(x);
  120. case LEAKY:
  121. return leaky_gradient_kernel(x);
  122. case TANH:
  123. return tanh_gradient_kernel(x);
  124. case PLSE:
  125. return plse_gradient_kernel(x);
  126. case STAIR:
  127. return stair_gradient_kernel(x);
  128. case HARDTAN:
  129. return hardtan_gradient_kernel(x);
  130. case LHTAN:
  131. return lhtan_gradient_kernel(x);
  132. }
  133. return 0;
  134. }
  135. __global__ void activate_array_kernel(float *x, int n, ACTIVATION a)
  136. {
  137. int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  138. if (i < n) x[i] = activate_kernel(x[i], a);
  139. }
  140. __global__ void gradient_array_kernel(float *x, int n, ACTIVATION a, float *delta)
  141. {
  142. int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  143. if (i < n) delta[i] *= gradient_kernel(x[i], a);
  144. }
  145. extern "C" void activate_array_gpu(float *x, int n, ACTIVATION a)
  146. {
  147. activate_array_kernel << <cuda_gridsize(n), BLOCK >> >(x, n, a);
  148. check_error(cudaPeekAtLastError());
  149. }
  150. extern "C" void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta)
  151. {
  152. gradient_array_kernel << <cuda_gridsize(n), BLOCK >> >(x, n, a, delta);
  153. check_error(cudaPeekAtLastError());
  154. }
  155. };