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.

92 lines
2.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "softmax_layer.h"
  2. #include "blas.h"
  3. #include "dn_cuda.h"
  4. #include <float.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <assert.h>
  9. namespace DN{
  10. softmax_layer make_softmax_layer(int batch, int inputs, int groups)
  11. {
  12. assert(inputs%groups == 0);
  13. fprintf(stderr, "softmax %4d\n", inputs);
  14. softmax_layer l;
  15. memset(&l, 0, sizeof(softmax_layer));
  16. l.type = SOFTMAX;
  17. l.batch = batch;
  18. l.groups = groups;
  19. l.inputs = inputs;
  20. l.outputs = inputs;
  21. l.output = (float*)calloc(inputs*batch, sizeof(float));
  22. l.delta = (float*)calloc(inputs*batch, sizeof(float));
  23. l.forward = forward_softmax_layer;
  24. l.backward = backward_softmax_layer;
  25. #ifdef GPU
  26. l.forward_gpu = forward_softmax_layer_gpu;
  27. l.backward_gpu = backward_softmax_layer_gpu;
  28. l.output_gpu = cuda_make_array(l.output, inputs*batch);
  29. l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
  30. #endif
  31. return l;
  32. }
  33. void forward_softmax_layer(const softmax_layer l, network net)
  34. {
  35. if (l.softmax_tree){
  36. int i;
  37. int count = 0;
  38. for (i = 0; i < l.softmax_tree->groups; ++i) {
  39. int group_size = l.softmax_tree->group_size[i];
  40. softmax_cpu(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count);
  41. count += group_size;
  42. }
  43. }
  44. else {
  45. softmax_cpu(net.input, l.inputs / l.groups, l.batch, l.inputs, l.groups, l.inputs / l.groups, 1, l.temperature, l.output);
  46. }
  47. }
  48. void backward_softmax_layer(const softmax_layer l, network net)
  49. {
  50. axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1);
  51. }
  52. #ifdef GPU
  53. void pull_softmax_layer_output(const softmax_layer layer)
  54. {
  55. cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch);
  56. }
  57. void forward_softmax_layer_gpu(const softmax_layer l, network net)
  58. {
  59. if(l.softmax_tree){
  60. int i;
  61. int count = 0;
  62. for (i = 0; i < l.softmax_tree->groups; ++i) {
  63. int group_size = l.softmax_tree->group_size[i];
  64. softmax_gpu(net.input_gpu + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output_gpu + count);
  65. count += group_size;
  66. }
  67. } else {
  68. if(l.spatial){
  69. softmax_gpu(net.input_gpu, l.c, l.batch*l.c, l.inputs/l.c, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu);
  70. }else{
  71. softmax_gpu(net.input_gpu, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output_gpu);
  72. }
  73. }
  74. }
  75. void backward_softmax_layer_gpu(const softmax_layer layer, network net)
  76. {
  77. axpy_gpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, net.delta_gpu, 1);
  78. }
  79. #endif
  80. };