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.

74 lines
2.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "shortcut_layer.h"
  2. #include "dn_cuda.h"
  3. #include "blas.h"
  4. #include "activations.h"
  5. #include <stdio.h>
  6. #include <assert.h>
  7. namespace DN{
  8. layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2)
  9. {
  10. fprintf(stderr, "Shortcut Layer: %d\n", index);
  11. layer l;
  12. memset(&l, 0, sizeof(layer));
  13. l.type = SHORTCUT;
  14. l.batch = batch;
  15. l.w = w2;
  16. l.h = h2;
  17. l.c = c2;
  18. l.out_w = w;
  19. l.out_h = h;
  20. l.out_c = c;
  21. l.outputs = w*h*c;
  22. l.inputs = l.outputs;
  23. l.index = index;
  24. l.delta = (float*)calloc(l.outputs*batch, sizeof(float));
  25. l.output = (float*)calloc(l.outputs*batch, sizeof(float));;
  26. l.forward = forward_shortcut_layer;
  27. l.backward = backward_shortcut_layer;
  28. #ifdef GPU
  29. l.forward_gpu = forward_shortcut_layer_gpu;
  30. l.backward_gpu = backward_shortcut_layer_gpu;
  31. l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch);
  32. l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
  33. #endif
  34. return l;
  35. }
  36. void forward_shortcut_layer(const layer l, network net)
  37. {
  38. copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1);
  39. shortcut_cpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.output);
  40. activate_array(l.output, l.outputs*l.batch, l.activation);
  41. }
  42. void backward_shortcut_layer(const layer l, network net)
  43. {
  44. gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
  45. axpy_cpu(l.outputs*l.batch, 1, l.delta, 1, net.delta, 1);
  46. shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, net.layers[l.index].delta);
  47. }
  48. #ifdef GPU
  49. void forward_shortcut_layer_gpu(const layer l, network net)
  50. {
  51. copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1);
  52. shortcut_gpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.output_gpu);
  53. activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation);
  54. }
  55. void backward_shortcut_layer_gpu(const layer l, network net)
  56. {
  57. gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
  58. axpy_gpu(l.outputs*l.batch, 1, l.delta_gpu, 1, net.delta_gpu, 1);
  59. shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, net.layers[l.index].delta_gpu);
  60. }
  61. #endif
  62. };