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.

68 lines
1.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "activation_layer.h"
  2. #include "utils.h"
  3. #include "dn_cuda.h"
  4. #include "blas.h"
  5. #include "gemm.h"
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. namespace DN{
  11. layer make_activation_layer(int batch, int inputs, ACTIVATION activation)
  12. {
  13. layer l;
  14. memset(&l, 0, sizeof(layer));
  15. l.type = ACTIVE;
  16. l.inputs = inputs;
  17. l.outputs = inputs;
  18. l.batch = batch;
  19. l.output = (float*)calloc(batch*inputs, sizeof(float*));
  20. l.delta = (float*)calloc(batch*inputs, sizeof(float*));
  21. l.forward = forward_activation_layer;
  22. l.backward = backward_activation_layer;
  23. #ifdef GPU
  24. l.forward_gpu = forward_activation_layer_gpu;
  25. l.backward_gpu = backward_activation_layer_gpu;
  26. l.output_gpu = cuda_make_array(l.output, inputs*batch);
  27. l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
  28. #endif
  29. l.activation = activation;
  30. fprintf(stderr, "Activation Layer: %d inputs\n", inputs);
  31. return l;
  32. }
  33. void forward_activation_layer(layer l, network net)
  34. {
  35. copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1);
  36. activate_array(l.output, l.outputs*l.batch, l.activation);
  37. }
  38. void backward_activation_layer(layer l, network net)
  39. {
  40. gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
  41. copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1);
  42. }
  43. #ifdef GPU
  44. void forward_activation_layer_gpu(layer l, network net)
  45. {
  46. copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1);
  47. activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation);
  48. }
  49. void backward_activation_layer_gpu(layer l, network net)
  50. {
  51. gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
  52. copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1);
  53. }
  54. #endif
  55. };