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.

88 lines
2.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "softmaxLayer.h"
  2. softmaxLayer::softmaxLayer(bool useGPU) :cortexLayer( SOFTMAX, useGPU ){
  3. m_pSoftmaxTree = NULL;
  4. };
  5. softmaxLayer::~softmaxLayer(){
  6. if (m_pSoftmaxTree != NULL){
  7. //ToDo: Cleanup Tree
  8. };
  9. };
  10. bool softmaxLayer::create(uint32_t batch, uint32_t inputs, uint32_t groups){
  11. assert(inputs%groups == 0);
  12. fprintf(stderr, "softmax %4d\n", inputs);
  13. m_nBatch = batch;
  14. m_nGroups = groups;
  15. m_nInput = inputs;
  16. m_nOutput = inputs*batch;
  17. m_pOutput = (float*)calloc(m_nOutput, sizeof(float));
  18. m_nDelta = inputs*batch;
  19. m_pDelta = (float*)calloc(m_nDelta, sizeof(float));
  20. #ifdef GPU
  21. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput);// cuda_make_array(l.output, inputs*batch);
  22. m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta);// cuda_make_array(l.delta, inputs*batch);
  23. #endif
  24. return true;
  25. };
  26. void softmaxLayer::tree(float *input, uint32_t batch, uint32_t inputs, float temp, cortexTREE *hierarchy, float *output){
  27. for(uint32_t b = 0; b < batch; ++b){
  28. int count = 0;
  29. for(uint32_t i = 0; i < hierarchy->groups; ++i){
  30. uint32_t group_size = hierarchy->group_size[i];
  31. m_pPU->makeSoftMax(input+b*inputs + count, group_size, temp, output+b*inputs + count);
  32. count += group_size;
  33. };
  34. };
  35. };
  36. void softmaxLayer::forward(cortexSTATE state){
  37. uint32_t inputs = m_nInput / m_nGroups;
  38. uint32_t batch = m_nBatch * m_nGroups;
  39. if( m_pSoftmaxTree != NULL ){
  40. tree(state.input, batch, inputs, m_fTemperature, m_pSoftmaxTree, m_pOutput);
  41. } else {
  42. for(uint32_t b = 0; b < batch; ++b){
  43. m_pPU->makeSoftMax(state.input + b*inputs, inputs, m_fTemperature, m_pOutput + b*inputs);
  44. };
  45. };
  46. };
  47. void softmaxLayer::backward(cortexSTATE state){
  48. for(uint32_t i = 0; i < m_nInput*m_nBatch; ++i){
  49. state.delta[i] += m_pDelta[i];
  50. };
  51. };
  52. #ifdef GPU
  53. #include "../cuda/cuda.h"
  54. void gpuCUDA::pull_layer(softmaxLayer *layer){
  55. //cuda_pull_array(layer.output_gpu, layer.output, layer.m_nOutput);
  56. pullFloatArray(layer->m_pGpuOutput, layer->m_pOutput, layer->m_nOutput);
  57. };
  58. void gpuCUDA::forward_layer(softmaxLayer *l, cortexSTATE state){
  59. uint32_t inputs = l->m_nInput / l->m_nGroups;
  60. uint32_t batch = l->m_nBatch * l->m_nGroups;
  61. if( l->m_pSoftmaxTree != NULL ){
  62. uint32_t count = 0;
  63. for (uint32_t i = 0; i < l->m_pSoftmaxTree->groups; ++i) {
  64. uint32_t group_size = l->m_pSoftmaxTree->group_size[i];
  65. makeSoftMax(state.input + count, group_size, inputs, batch, l->m_fTemperature, l->m_pGpuOutput + count);
  66. count += group_size;
  67. };
  68. } else {
  69. makeSoftMax(state.input, inputs, inputs, batch, l->m_fTemperature, l->m_pGpuOutput);
  70. };
  71. };
  72. void gpuCUDA::backward_layer(softmaxLayer *layer, cortexSTATE state){
  73. calcAxpy(layer->m_nBatch*layer->m_nInput, 1, layer->m_pGpuDelta, 1, state.delta, 1);
  74. };
  75. #endif