@brief ReluLayer层反向传播
Relu激活函数: f(x)=max(0,x)
top_data={bottom_data,0,if bottom_data>0if bottom_data≤0
反向传导时,
∂loss∂bottom_data=输入梯度∗∂top_data∂bottom_data{输入梯度∗top_diff,输入梯度∗0,if bottom_data>0if bottom_data≤0
#include <algorithm>
#include <vector>
#include "caffe/layers/relu_layer.hpp"
namespace caffe {
template <typename Dtype>
void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
const int count = bottom[0]->count();
Dtype negative_slope = this->layer_param_.relu_param().negative_slope();
for (int i = 0; i < count; ++i) {
top_data[i] = std::max(bottom_data[i], Dtype(0))
+ negative_slope * std::min(bottom_data[i], Dtype(0));//proto中设置为0
}
}
template <typename Dtype>
void ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[0]) {
const Dtype* bottom_data = bottom[0]->cpu_data();//拿到ReluLayer的输入值
const Dtype* top_diff = top[0]->cpu_diff();//拿到ReluLayer输入梯度
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();//要从ReluLayer反向传播的梯度
const int count = bottom[0]->count();//该层具有神经元的个数
Dtype negative_slope = this->layer_param_.relu_param().negative_slope();
for (int i = 0; i < count; ++i) {
bottom_diff[i] = top_diff[i] * ((bottom_data[i] > 0)//如果输入激活值>0 要反向传播的梯度=该层传进的梯度值*输入的值
+ negative_slope * (bottom_data[i] <= 0));//如果输入激活值<=0 梯度为0
}
}
}
#ifdef CPU_ONLY
STUB_GPU(ReLULayer);
#endif
INSTANTIATE_CLASS(ReLULayer);
} // namespace caffe