// Copyright 2014 BVLC and contributors.
//
#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>
//建立softmax网络层
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."; //bottom是输入
CHECK_EQ(top->size(), 1) << "Softmax Layer takes a single blob as output.";//top是输出
//以minist为例,如果一次送进来一张图片,因为上一层inner_product层输出10个节点,则bottom : num = 1, channels = 10, height = 1, width = 1;
//输出分配空间
(*top)[0]->Reshape(bottom[0]->num(), bottom[0]->channels(),
bottom[0]->height(), bottom[0]->width());
//sum_multiplier_这里都是1,用于辅助计算,可以看作一个行向量,或者行数为1的矩阵
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.; //sum_multiplier_将sum_multiplier_每一个位置都置为1
}
//临时变量scale_分配空间,大小为num,可以看作一个列向量 ,若以minist一次送进一张图片为例,则scale_的: num= channels = height = width =1
scale_.Reshape(bottom[0]->num(), 1, 1, 1);
}
//softmax层前向传播
template <typename Dtype>
Dtype SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top)
{
//cout << "bottom[0]->cpu_data(): "<< bottom[0]->cpu_data() << endl;
const Dtype* bottom_data = bottom[0]->cpu_data(); //指向输入参数
//cout << "bottom_data: " << bottom_data << endl;
Dtype* top_data = (*top)[0]->mutable_cpu_data(); //指向输出参数
Dtype* scale_data = scale_.mutable_cpu_data(); //指向临时变量参数
//把输出看成是num层,每层dim个元素
int num = bottom[0]->num();
int dim = bottom[0]->count() / bottom[0]->num(); //count_ = num_ * channels_ * height_ * width_;
//将bottom_data的数据拷贝到top_data中
memcpy(top_data, bottom_data, sizeof(Dtype) * bottom[0]->count());
// we need to subtract the max to avoid numerical issues,
// compute the exp, and then normalize.
//以minist为例,是找出上一层10个输出中最大的一个赋值给scale_data
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]);
}
}
// subtraction
// subtraction 通过矩阵相乘的方式来计算,有num层的top_data,每层所有元素减去该层的最大值。太巧妙了(应该是防止数据太大越界)
// C = alpha*op( A )*op( B ) + beta*C
// const int M,矩阵A的行,矩阵C的行
//const int N,矩阵B的列,矩阵C的列
//const int K,矩阵A的列,矩阵B的行
//在CBLAS的函数中无论一维还是二维数据都是用一维数组存储,这就要涉及是行主序还是列主序,在C语言中数组是用 行主序
//矩阵相乘 num:scale_data与矩阵top_data的行数, dim:矩阵sum_multiplier_与top_data的列, 1:矩阵scale_data的列与矩阵sum_multiplier_的行
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1,
-1.0, scale_data, sum_multiplier_.cpu_data(), 1., top_data);
// Perform exponentiation计算自然对数
caffe_exp<Dtype>(num * dim, top_data, top_data);
// sum after exp每一层各自求和放到scale_data中
caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim,
1.0, top_data, sum_multiplier_.cpu_data(), 0., scale_data);
// Do division 每一层各自除以该层的和
for (int i = 0; i < num; ++i)
{
caffe_scal<Dtype>(dim, Dtype(1.) / scale_data[i], top_data + i * dim);
}
//nfd test, 以minist为例输出10个标签的概率
/* for (int i = 0; i < 10; i++)
{
cout << i << " "<< top_data[i]<< endl;
}*/
return Dtype(0);
}
template <typename Dtype>
void 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());
// Compute inner1d(top_diff, top_data) and subtract them from the bottom diff
for (int i = 0; i < num; ++i)
{
scale_data[i] = caffe_cpu_dot<Dtype>(dim, top_diff + i * dim,
top_data + i * dim);//每一层,top_diff和top_data计算内积
}
// subtraction每一层bottom_diff的元素减去该层的对应的内积
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.0,
scale_data, sum_multiplier_.cpu_data(), 1.0, bottom_diff);
// elementwise multiplication 元素各自相乘
caffe_mul<Dtype>(top[0]->count(), bottom_diff, top_data, bottom_diff);
}
INSTANTIATE_CLASS(SoftmaxLayer);
} // namespace caffe
caffe深度学习网络softmax层代码注释
最新推荐文章于 2020-12-21 10:18:23 发布