caffe源码分析--softmax_layer.cpp

caffe源码分析--softmax_layer.cpp

文件位置为caffe-master/src/caffe/layers/softmax_layer.cpp

这个是一个以前版本的程序,现在的代码有些不同了,不过可以参考

caffe源码分析--softmax_layer.cpp

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // Copyright 2013 Yangqing Jia  
  2. //  
  3. #include <algorithm>  
  4. #include <vector>  
  5.   
  6. #include "caffe/layer.hpp"  
  7. #include "caffe/vision_layers.hpp"  
  8. #include "caffe/util/math_functions.hpp"  
  9.   
  10. using std::max;  
  11.   
  12. namespace caffe {  
  13.   
  14. /** 
  15.  * 建立softmax网络层 
  16.  */  
  17. template <typename Dtype>  
  18. void SoftmaxLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,  
  19.       vector<Blob<Dtype>*>* top) {  
  20.   CHECK_EQ(bottom.size(), 1) << "Softmax Layer takes a single blob as input.";  
  21.   CHECK_EQ(top->size(), 1) << "Softmax Layer takes a single blob as output.";  
  22.   //输出分配空间  
  23.   (*top)[0]->Reshape(bottom[0]->num(), bottom[0]->channels(),  
  24.       bottom[0]->height(), bottom[0]->width());  
  25.   //sum_multiplier_这里都是1,用于辅助计算,可以看作一个行向量,或者行数为1的矩阵  
  26.   sum_multiplier_.Reshape(1, bottom[0]->channels(),  
  27.       bottom[0]->height(), bottom[0]->width());  
  28.   Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();  
  29.   for (int i = 0; i < sum_multiplier_.count(); ++i) {  
  30.     multiplier_data[i] = 1.;  
  31.   }  
  32.   //临时变量scale_分配空间,大小为num,可以看作一个列向量  
  33.   scale_.Reshape(bottom[0]->num(), 1, 1, 1);  
  34. }  
  35.   
  36. template <typename Dtype>  
  37. void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,  
  38.     vector<Blob<Dtype>*>* top) {  
  39.   const Dtype* bottom_data = bottom[0]->cpu_data();  
  40.   Dtype* top_data = (*top)[0]->mutable_cpu_data();  
  41.   Dtype* scale_data = scale_.mutable_cpu_data();  
  42.   //把输出看成是num层,每层dim个元素  
  43.   int num = bottom[0]->num();  
  44.   int dim = bottom[0]->count() / bottom[0]->num();  
  45.   memcpy(top_data, bottom_data, sizeof(Dtype) * bottom[0]->count());  
  46.   // we need to subtract the max to avoid numerical issues, compute the exp,  
  47.   // and then normalize.  
  48.   //找出每一层的最大值  
  49.   for (int i = 0; i < num; ++i) {  
  50.     scale_data[i] = bottom_data[i*dim];  
  51.     for (int j = 0; j < dim; ++j) {  
  52.       scale_data[i] = max(scale_data[i], bottom_data[i * dim + j]);  
  53.     }  
  54.   }  
  55.   // subtraction  通过矩阵相乘的方式来计算,有num层的top_data,每层元素减去该层的最大值。太巧妙了  
  56.   caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.,  
  57.     scale_data, sum_multiplier_.cpu_data(), 1., top_data);  
  58.   // C = alpha*op( A )*op( B ) + beta*C  
  59.   
  60.   // Perform exponentiation 计算自然对数  
  61.   caffe_exp<Dtype>(num * dim, top_data, top_data);  
  62.   // sum after exp 每一层各自求和放到scale_data中  
  63.   caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1., top_data,  
  64.       sum_multiplier_.cpu_data(), 0., scale_data);  
  65.   // Do division 每一层各自除以该层的和  
  66.   for (int i = 0; i < num; ++i) {  
  67.     caffe_scal<Dtype>(dim, Dtype(1.) / scale_data[i], top_data + i * dim);  
  68.   }  
  69. }  
  70.   
  71. template <typename Dtype>  
  72. Dtype SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,  
  73.     const bool propagate_down,  
  74.     vector<Blob<Dtype>*>* bottom) {  
  75.   const Dtype* top_diff = top[0]->cpu_diff();  
  76.   const Dtype* top_data = top[0]->cpu_data();  
  77.   Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();  
  78.   Dtype* scale_data = scale_.mutable_cpu_data();  
  79.   int num = top[0]->num();  
  80.   int dim = top[0]->count() / top[0]->num();  
  81.   memcpy(bottom_diff, top_diff, sizeof(Dtype) * top[0]->count());  
  82.   // Compute inner1d(top_diff, top_data) and subtract them from the bottom diff  
  83.   for (int i = 0; i < num; ++i) {  
  84.     scale_data[i] = caffe_cpu_dot<Dtype>(dim, top_diff + i * dim,  
  85.         top_data + i * dim);//每一层,top_diff和top_data计算内积  
  86.   }  
  87.   // subtraction  每一层bottom_diff的元素减去该层的对应的内积  
  88.   caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.,  
  89.       scale_data, sum_multiplier_.cpu_data(), 1., bottom_diff);  
  90.   // elementwise multiplication 元素各自相乘  
  91.   caffe_mul<Dtype>(top[0]->count(), bottom_diff, top_data, bottom_diff);  
  92.   return Dtype(0);  
  93. }  
  94.   
  95.   
  96. INSTANTIATE_CLASS(SoftmaxLayer);  
  97.   
  98.   
  99. }  // namespace caffe  

 

 

本文作者:linger

本文链接:http://blog.csdn.net/lingerlanlan/article/details/32700431

posted @ 2015-05-14 15:20  菜鸡一枚  阅读(2753)  评论(0编辑  收藏  举报