一、 在多分类模型中, 常使用softmax分类,它是logistic模型的推广, 将输出映射成概率,在(0,1)之间, 并能够起到突出最大值的作用。
e指数曲线 softmax公式
由e指数曲线可知, 曲线呈凹函数性质, 即增长的很快, 所以对于输入较大的z,起到了放大作用。
1.2) 盗用别人的一张图,来说明softmax的计算过程:
一目了然,就不多说了, 下面解释下caffe中的前向传播的源码:
二:
#include
#include
#include "caffe/layers/softmax_layer.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
template
void SoftmaxLayer::Reshape(const vector*>& bottom, //该函数做一些blob形状的变化,top和scale,
const vector*>& top) {
softmax_axis_ =
bottom[0]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis()); // 默认是1, 若以VGG16的1000分类模型单张图片为例, 则 bottom[0]:1,1000,1,1
top[0]->ReshapeLike(*bottom[0]); //top的结构按照bottom的结构 reshape n,c,h,w: 1,1000,1,1
vector mult_dims(1, bottom[0]->shape(softmax_axis_)); // mult_dims = c 包含1个 值为 c的元素。 1000
sum_multiplier_.Reshape(mult_dims); // 1000
Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
caffe_set(sum_multiplier_.count(), Dtype(1), multiplier_data); //multiplier_data every one is Dtype(1)
outer_num_ = bottom[0]->count(0, softmax_axis_); // n 1
inner_num_ = bottom[0]->count(softmax_axis_ + 1); //h*w 1
vector scale_dims = bottom[0]->shape(); //return shape_; it is a vector,contain the blob shape; 1,1000,1,1
scale_dims[softmax_axis_] = 1; //将scale_dims的c 设为1, 则此时的scale_dims和bottom[0]的结构的c是不同的。
scale_.Reshape(scale_dims); //将scale_设置成 n*1*h*w 1,1,1,1
}
template
void SoftmaxLayer::Forward_cpu(const vector*>& bottom,
const vector*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
Dtype* scale_data = scale_.mutable_cpu_data(); //scale_临时存放结果的blob
int channels = bottom[0]->shape(softmax_axis_); // c, 1000
int dim = bottom[0]->count() / outer_num_; //dim =n*c*w*h/n = c*w*h 1000 ,
caffe_copy(bottom[0]->count(), bottom_data, top_data); //将bottom数据copy到top中, 因为在reshape函数中,已经将top和bottom的结构设置成相同的了。
// We need to subtract the max to avoid numerical issues, compute the exp,
// and then normalize.
//取出最大值, 为了数值稳定。
for (int i = 0; i < outer_num_; ++i) { // n 1
// initialize scale_data to the first plane
caffe_copy(inner_num_, bottom_data + i * dim, scale_data); //copy h*w 给输出 1
for (int j = 0; j < channels; j++) { //c 1000 2
for (int k = 0; k < inner_num_; k++) { //h*w 1 14*14
scale_data[k] = std::max(scale_data[k], // scale_data[0] 存放的是0--999个中的最大值。
bottom_data[i * dim + j * inner_num_ + k]); //
}
}
// subtraction, 减掉最大值。 为了数值稳定
// C = alpha*op( A )*op( B ) + beta*C alpha=-1;beta=1
// const int M,矩阵A的行,矩阵C的行 channels 1000
//const int N,矩阵B的列,矩阵C的列 inner_num_ 1
//const int K,矩阵A的列,矩阵B的行 1列
//在CBLAS的函数中无论一维还是二维数据都是用一维数组存储,这就要涉及是行主序还是列主序,在C语言中数组是用 行主序
//矩阵相乘 num:scale_data与矩阵top_data的行数, dim:矩阵sum_multiplier_与top_data的列, 1:矩阵scale_data的列与矩阵sum_multiplier_的行
caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, channels, inner_num_,
1, -1., sum_multiplier_.cpu_data(), scale_data, 1., top_data);
// exponentiation, 计算分子
caffe_exp(dim, top_data, top_data); //dim:1000
// sum after exp 计算分母
caffe_cpu_gemv(CblasTrans, channels, inner_num_, 1.,
top_data, sum_multiplier_.cpu_data(), 0., scale_data);
// division 计算除法
for (int j = 0; j < channels; j++) {
caffe_div(inner_num_, top_data, scale_data, top_data);
top_data += inner_num_;
}
}
}
有一点需要注意的时, 当inner_num_的值不是1的时候, 即w*h不为1时, 则 softmax对w×h的每一个点 都进行在channel上的softmax运算。
三、 下面是反向传播的公式推到
因为编辑公式太麻烦,所以我手写了,字迹潦草,请多见谅:
下面是caffe中关于反向传播的代码:
template
void SoftmaxLayer::Backward_cpu(const vector*>& top,
const vector& propagate_down,
const vector*>& 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) { //outer_num是批次, 预测时一般为1
// compute dot(top_diff, top_data) and subtract them from the bottom diff
//计算内括号中的 内积; 注意l对a的导数,已经存放到了从top_diff中 赋值到 bottom_diff中了。 而a值 则存放在top_data中。
for (int k = 0; k < inner_num_; ++k) {
scale_data[k] = caffe_cpu_strided_dot(channels,
bottom_diff + i * dim + k, inner_num_,
top_data + i * dim + k, inner_num_);
}
// subtraction 计算外括号中的-1 和 加法。
caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, channels, inner_num_, 1,
-1., sum_multiplier_.cpu_data(), scale_data, 1., bottom_diff + i * dim);
}
// elementwise multiplication 计算最外边的与 a 相乘部分。
caffe_mul(top[0]->count(), bottom_diff, top_data, bottom_diff);
}