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.

254 lines
6.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "blas.h"
  2. #include "math.h"
  3. #include <assert.h>
  4. #include <float.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. void add_bias(float *output, float *biases, int batch, int n, int size){
  9. int i,j,b;
  10. for(b = 0; b < batch; ++b){
  11. for(i = 0; i < n; ++i){
  12. for(j = 0; j < size; ++j){
  13. output[(b*n + i)*size + j] += biases[i];
  14. }
  15. }
  16. }
  17. }
  18. void scale_bias(float *output, float *scales, int batch, int n, int size){
  19. int i,j,b;
  20. for(b = 0; b < batch; ++b){
  21. for(i = 0; i < n; ++i){
  22. for(j = 0; j < size; ++j){
  23. output[(b*n + i)*size + j] *= scales[i];
  24. }
  25. }
  26. }
  27. }
  28. void backward_bias(float *bias_updates, float *delta, int batch, int n, int size){
  29. int i,b;
  30. for(b = 0; b < batch; ++b){
  31. for(i = 0; i < n; ++i){
  32. bias_updates[i] += sum_array(delta+size*(i+b*n), size);
  33. }
  34. }
  35. }
  36. void reorg_cpu(float *x, int w, int h, int c, int batch, int stride, int forward, float *out)
  37. {
  38. int b,i,j,k;
  39. int out_c = c/(stride*stride);
  40. for(b = 0; b < batch; ++b){
  41. for(k = 0; k < c; ++k){
  42. for(j = 0; j < h; ++j){
  43. for(i = 0; i < w; ++i){
  44. int in_index = i + w*(j + h*(k + c*b));
  45. int c2 = k % out_c;
  46. int offset = k / out_c;
  47. int w2 = i*stride + offset % stride;
  48. int h2 = j*stride + offset / stride;
  49. int out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
  50. if(forward) out[out_index] = x[in_index];
  51. else out[in_index] = x[out_index];
  52. }
  53. }
  54. }
  55. }
  56. }
  57. void flatten(float *x, int size, int layers, int batch, int forward)
  58. {
  59. float *swap = (float*) calloc(size*layers*batch, sizeof(float));
  60. int i,c,b;
  61. for(b = 0; b < batch; ++b){
  62. for(c = 0; c < layers; ++c){
  63. for(i = 0; i < size; ++i){
  64. int i1 = b*layers*size + c*size + i;
  65. int i2 = b*layers*size + i*layers + c;
  66. if (forward) swap[i2] = x[i1];
  67. else swap[i1] = x[i2];
  68. }
  69. }
  70. }
  71. memcpy(x, swap, size*layers*batch*sizeof(float));
  72. free(swap);
  73. }
  74. void weighted_sum_cpu(float *a, float *b, float *s, int n, float *c)
  75. {
  76. int i;
  77. for(i = 0; i < n; ++i){
  78. c[i] = s[i]*a[i] + (1-s[i])*(b ? b[i] : 0);
  79. }
  80. }
  81. void shortcut_cpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out)
  82. {
  83. int stride = w1/w2;
  84. int sample = w2/w1;
  85. assert(stride == h1/h2);
  86. assert(sample == h2/h1);
  87. if(stride < 1) stride = 1;
  88. if(sample < 1) sample = 1;
  89. int minw = (w1 < w2) ? w1 : w2;
  90. int minh = (h1 < h2) ? h1 : h2;
  91. int minc = (c1 < c2) ? c1 : c2;
  92. int i,j,k,b;
  93. for(b = 0; b < batch; ++b){
  94. for(k = 0; k < minc; ++k){
  95. for(j = 0; j < minh; ++j){
  96. for(i = 0; i < minw; ++i){
  97. int out_index = i*sample + w2*(j*sample + h2*(k + c2*b));
  98. int add_index = i*stride + w1*(j*stride + h1*(k + c1*b));
  99. out[out_index] += add[add_index];
  100. }
  101. }
  102. }
  103. }
  104. }
  105. void mean_cpu(float *x, int batch, int filters, int spatial, float *mean)
  106. {
  107. float scale = 1.f/(batch * spatial);
  108. int i,j,k;
  109. for(i = 0; i < filters; ++i){
  110. mean[i] = 0;
  111. for(j = 0; j < batch; ++j){
  112. for(k = 0; k < spatial; ++k){
  113. int index = j*filters*spatial + i*spatial + k;
  114. mean[i] += x[index];
  115. }
  116. }
  117. mean[i] *= scale;
  118. }
  119. }
  120. void variance_cpu(float *x, float *mean, int batch, int filters, int spatial, float *variance)
  121. {
  122. float scale = 1.f/(batch * spatial - 1.f);
  123. int i,j,k;
  124. for(i = 0; i < filters; ++i){
  125. variance[i] = 0;
  126. for(j = 0; j < batch; ++j){
  127. for(k = 0; k < spatial; ++k){
  128. int index = j*filters*spatial + i*spatial + k;
  129. variance[i] += (float)pow((x[index] - mean[i]), 2.f);
  130. }
  131. }
  132. variance[i] *= scale;
  133. }
  134. }
  135. void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
  136. {
  137. int b, f, i;
  138. for(b = 0; b < batch; ++b){
  139. for(f = 0; f < filters; ++f){
  140. for(i = 0; i < spatial; ++i){
  141. int index = b*filters*spatial + f*spatial + i;
  142. x[index] = (float)((x[index] - mean[f])/(sqrt(variance[f]) + .000001f));
  143. }
  144. }
  145. }
  146. }
  147. void const_cpu(int N, float ALPHA, float *X, int INCX)
  148. {
  149. int i;
  150. for(i = 0; i < N; ++i) X[i*INCX] = ALPHA;
  151. }
  152. void mul_cpu(int N, float *X, int INCX, float *Y, int INCY)
  153. {
  154. int i;
  155. for(i = 0; i < N; ++i) Y[i*INCY] *= X[i*INCX];
  156. }
  157. void pow_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
  158. {
  159. int i;
  160. for(i = 0; i < N; ++i) Y[i*INCY] = (float)pow(X[i*INCX], ALPHA);
  161. }
  162. void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
  163. {
  164. int i;
  165. for(i = 0; i < N; ++i) Y[i*INCY] += ALPHA*X[i*INCX];
  166. }
  167. void scal_cpu(int N, float ALPHA, float *X, int INCX)
  168. {
  169. int i;
  170. for(i = 0; i < N; ++i) X[i*INCX] *= ALPHA;
  171. }
  172. void fill_cpu(int N, float ALPHA, float *X, int INCX)
  173. {
  174. int i;
  175. for(i = 0; i < N; ++i) X[i*INCX] = ALPHA;
  176. }
  177. void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
  178. {
  179. int i;
  180. for(i = 0; i < N; ++i) Y[i*INCY] = X[i*INCX];
  181. }
  182. void smooth_l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
  183. {
  184. int i;
  185. for(i = 0; i < n; ++i){
  186. float diff = truth[i] - pred[i];
  187. float abs_val = (float)fabs(diff);
  188. if(abs_val < 1) {
  189. error[i] = diff * diff;
  190. delta[i] = diff;
  191. }
  192. else {
  193. error[i] = 2.f*abs_val - 1.f;
  194. delta[i] = (diff < 0) ? -1.f : 1.f;
  195. }
  196. }
  197. }
  198. void l2_cpu(int n, float *pred, float *truth, float *delta, float *error)
  199. {
  200. int i;
  201. for(i = 0; i < n; ++i){
  202. float diff = truth[i] - pred[i];
  203. error[i] = diff * diff;
  204. delta[i] = diff;
  205. }
  206. }
  207. float dot_cpu(int N, float *X, int INCX, float *Y, int INCY)
  208. {
  209. int i;
  210. float dot = 0;
  211. for(i = 0; i < N; ++i) dot += X[i*INCX] * Y[i*INCY];
  212. return dot;
  213. }
  214. void softmax(float *input, int n, float temp, float *output)
  215. {
  216. int i;
  217. float sum = 0;
  218. float largest = -FLT_MAX;
  219. for(i = 0; i < n; ++i){
  220. if(input[i] > largest) largest = input[i];
  221. }
  222. for(i = 0; i < n; ++i){
  223. float e = (float)exp(input[i]/temp - largest/temp);
  224. sum += e;
  225. output[i] = e;
  226. }
  227. for(i = 0; i < n; ++i){
  228. output[i] /= sum;
  229. }
  230. }