此为caffe里比较简单的sigmoid函数的定义,位于
your path/src/caffe/layers/sigmoid_layer.cpp
#include <cmath>
#include <vector>
#include "caffe/layers/sigmoid_layer.hpp"
namespace caffe
{
template <typename Dtype>
inline Dtype sigmoid(Dtype x)
{
return 1. / (1. + exp(-x));
}
template <typename Dtype>
void SigmoidLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top)
{
//bottom[0]->cpu_data()为前一层传入的激活值
const Dtype* bottom_data = bottom[0]->cpu_data();
//top[0]->mutable_cpu_data()拿到了传到前一层的数值所存的地址
Dtype* top_data = top[0]->mutable_cpu_data();
const int count = bottom[0]->count();
//把要输入下一层的激活值进行赋值
for (int i = 0; i < count; ++i)
{
top_data[i] = sigmoid(bottom_data[i]);
}
}
template <typename Dtype>
void SigmoidLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,const vector<Blob<Dtype>*>& bottom)
{
/*现在为反向传播,所以现在是有top输入,输出到bottom*/
if (propagate_down[0])
{
const Dtype* top_data = top[0]->cpu_data();
//top[0]->cpu_diff()此为后一层神经元反向传播时计算出的梯度值
const Dtype* top_diff = top[0]->cpu_diff();
//同样是取到了存放输出梯度值的地址
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
//count为输出梯度值的数量
const int count = bottom[0]->count();
//对所有的梯度值赋值
for (int i = 0; i < count; ++i)
{
const Dtype sigmoid_x = top_data[i];
bottom_diff[i] = top_diff[i] * sigmoid_x * (1. - sigmoid_x);
}
}
}
#ifdef CPU_ONLY
STUB_GPU(SigmoidLayer);
#endif
INSTANTIATE_CLASS(SigmoidLayer);
} // namespace caffe