您的位置:首页 > Web前端

caffe源码解读(1)-softmax_loss_layer.cpp

2018-01-24 16:17 567 查看
softmax公式定义

caffe源码解读

定义

“softmax function is a generalization of the logistic function that maps a length-p vector of real values to a length-K vector of values”更多解释,可参考UFLDL教程链接,以及知乎回答链接

公式

f(Zk)=ezk∑ezk−m,m=max(zi)f(Zk)=ezk∑ezk−m,m=max(zi)

代码

(1)Reshape

template <typename Dtype>
void SoftmaxLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
softmax_axis_ =
bottom[0]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis());
top[0]->ReshapeLike(*bottom[0]);
vector<int> mult_dims(1, bottom[0]->shape(softmax_axis_));
sum_multiplier_.Reshape(mult_dims);
Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
caffe_set(sum_multiplier_.count(), Dtype(1), multiplier_data);
outer_num_ = bottom[0]->count(0, softmax_axis_);
inner_num_ = bottom[0]->count(softmax_axis_ + 1);
vector<int> scale_dims = bottom[0]->shape();
scale_dims[softmax_axis_] = 1;
scale_.Reshape(scale_dims);
}


(2)Forward

这里主要用到的math_function函数,可参考链接

template <typename Dtype>
void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
//cpu_data和mutable_cpu_data:前者用来读取数据,后者可进行写入数据操作。后面有caffe_copy
//将bottom_data的数据复制到top_data,因此在这里有cpu_data和mutable_cpu_data的区别。
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();//top_data中存的是公式中的Zk
Dtype* scale_data = scale_.mutable_cpu_data();
int channels = bottom[0]->shape(softmax_axis_);
int dim = bottom[0]->count() / outer_num_;
caffe_copy(bottom[0]->count(), bottom_data, top_data);
// We need to subtract the max to avoid numerical issues, compute the exp,
// and then normalize.(源码中给出的注释)
for (int i = 0; i < outer_num_; ++i) {
// initialize scale_data to the first plane
caffe_copy(inner_num_, bottom_data + i * dim, scale_data);
//找出最大值:公式定义中的m,保存在scale_data中。
for (int j = 0; j < channels; j++) {
for (int k = 0; k < inner_num_; k++) {
scale_data[k] = std::max(scale_data[k],
bottom_data[i * dim + j * inner_num_ + k]);
}
}
// subtraction做减法运算:Zk-m(即top_data-scale_data)
//caffe_cpu_gemm运算:top_data=-1.*sum_multiplier_.cpu_data()*scale_data+1.*top_data
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_,
1, -1., sum_multiplier_.cpu_data(), scale_data, 1., top_data);
// exponentiation做e指数运算:top_data=$$ e^{z_{k}-m} $$
caffe_exp<Dtype>(dim, top_data, top_data);
// sum after exp做求和运算:结果存入scale_data中。
//caffe_cpu_gemv运算:scale_data=1.*top_data*sum_multiplier_.cpu_data()+0.*scale_data
caffe_cpu_gemv<Dtype>(CblasTrans, channels, inner_num_, 1.,
top_data, sum_multiplier_.cpu_data(), 0., scale_data);
// division做除法运算:top_data=top_data/scale_data,即公式部分。
for (int j = 0; j < channels; j++) {
caffe_div(inner_num_, top_data, scale_data, top_data);
top_data += inner_num_;
}
}
}


(3)Backward

caffe中在做反向传到时,会用到propagate_down,该参数用来表示该层是否反向传播梯度,可在网络结构中通过设置true/false实现。关于反向求导的过程可以参考[链接](https://segmentfault.com/a/1190000010613726),在这里不做过多解释。

template <typename Dtype>
void SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const 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 channels = top[0]->shape(softmax_axis_);
int dim = top[0]->count() / outer_num_;
caffe_copy(top[0]->count(), top_diff, bottom_diff);
for (int i = 0; i < outer_num_; ++i) {
// compute dot(top_diff, top_data) and subtract them from the bottom diff
for (int k = 0; k < inner_num_; ++k) {
scale_data[k] = caffe_cpu_strided_dot<Dtype>(channels,
bottom_diff + i * dim + k, inner_num_,
top_data + i * dim + k, inner_num_);
}
// subtraction
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_, 1,
-1., sum_multiplier_.cpu_data(), scale_data, 1., bottom_diff + i * dim);
}
// elementwise multiplication
caffe_mul(top[0]->count(), bottom_diff, top_data, bottom_diff);
}


以上内容仅是个人理解,如有错误,欢迎指正,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  源码 函数