caffe源码分析--softmax_layer.cpp
文件位置为caffe-master/src/caffe/layers/softmax_layer.cpp
这个是一个以前版本的程序,现在的代码有些不同了,不过可以参考
caffe源码分析--softmax_layer.cpp
- #include <algorithm>
- #include <vector>
-
- #include "caffe/layer.hpp"
- #include "caffe/vision_layers.hpp"
- #include "caffe/util/math_functions.hpp"
-
- using std::max;
-
- namespace caffe {
-
- template <typename Dtype>
- void SoftmaxLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) {
- CHECK_EQ(bottom.size(), 1) << "Softmax Layer takes a single blob as input.";
- CHECK_EQ(top->size(), 1) << "Softmax Layer takes a single blob as output.";
-
- (*top)[0]->Reshape(bottom[0]->num(), bottom[0]->channels(),
- bottom[0]->height(), bottom[0]->width());
-
- sum_multiplier_.Reshape(1, bottom[0]->channels(),
- bottom[0]->height(), bottom[0]->width());
- Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
- for (int i = 0; i < sum_multiplier_.count(); ++i) {
- multiplier_data[i] = 1.;
- }
-
- scale_.Reshape(bottom[0]->num(), 1, 1, 1);
- }
-
- template <typename Dtype>
- void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) {
- const Dtype* bottom_data = bottom[0]->cpu_data();
- Dtype* top_data = (*top)[0]->mutable_cpu_data();
- Dtype* scale_data = scale_.mutable_cpu_data();
-
- int num = bottom[0]->num();
- int dim = bottom[0]->count() / bottom[0]->num();
- memcpy(top_data, bottom_data, sizeof(Dtype) * bottom[0]->count());
-
-
-
- for (int i = 0; i < num; ++i) {
- scale_data[i] = bottom_data[i*dim];
- for (int j = 0; j < dim; ++j) {
- scale_data[i] = max(scale_data[i], bottom_data[i * dim + j]);
- }
- }
-
- caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.,
- scale_data, sum_multiplier_.cpu_data(), 1., top_data);
-
-
-
- caffe_exp<Dtype>(num * dim, top_data, top_data);
-
- caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1., top_data,
- sum_multiplier_.cpu_data(), 0., scale_data);
-
- for (int i = 0; i < num; ++i) {
- caffe_scal<Dtype>(dim, Dtype(1.) / scale_data[i], top_data + i * dim);
- }
- }
-
- template <typename Dtype>
- Dtype SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down,
- vector<Blob<Dtype>*>* bottom) {
- const Dtype* top_diff = top[0]->cpu_diff();
- const Dtype* top_data = top[0]->cpu_data();
- Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
- Dtype* scale_data = scale_.mutable_cpu_data();
- int num = top[0]->num();
- int dim = top[0]->count() / top[0]->num();
- memcpy(bottom_diff, top_diff, sizeof(Dtype) * top[0]->count());
-
- for (int i = 0; i < num; ++i) {
- scale_data[i] = caffe_cpu_dot<Dtype>(dim, top_diff + i * dim,
- top_data + i * dim);
- }
-
- caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.,
- scale_data, sum_multiplier_.cpu_data(), 1., bottom_diff);
-
- caffe_mul<Dtype>(top[0]->count(), bottom_diff, top_data, bottom_diff);
- return Dtype(0);
- }
-
-
- INSTANTIATE_CLASS(SoftmaxLayer);
-
-
- }
本文作者:linger
本文链接:http://blog.csdn.net/lingerlanlan/article/details/32700431
caffe源码分析--softmax_layer.cpp
原文:http://www.cnblogs.com/yymn/p/4503438.html