Caffe源码:relu_layer.cpp

来源:互联网 发布:淘宝买家修改评价链接 编辑:程序博客网 时间:2024/06/03 09:26

@brief ReluLayer层反向传播


Relu激活函数:f(x)=max(0,x)

这里写图片描述

top_data={bottom_data,0,if bottom_data>0if bottom_data0

反向传导时,
lossbottom_data=top_databottom_data{top_diff,0,if bottom_data>0if bottom_data0

#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_ONLYSTUB_GPU(ReLULayer);#endifINSTANTIATE_CLASS(ReLULayer);}  // namespace caffe
原创粉丝点击