本文主要实现caffe框架中/src/caffe/layers/Relu_layer.cpp文件,该文件实现的是激活函数Relu。
ReLU是近些年非常流行的激活函数。相比于sigmoid与Tanh,它具有一定的优越性,这三者对比可见https://zhuanlan.zhihu.com/p/21462488?refer=intelligentunit,它的函数公式是f(x)=max(0,x)。换句话说,这个激活函数就是一个关于0的阈值。如下图:::
下面记录我在看relu层时的代码注释:::
Relu_layer.hpp:::
#ifndef CAFFE_RELU_LAYER_HPP_
#define CAFFE_RELU_LAYER_HPP_
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/layers/neuron_layer.hpp"
namespace caffe {
/**
* @brief Rectified Linear Unit non-linearity @f$ y = \max(0, x) @f$.
* The simple max is fast to compute, and the function does not saturate.
*/
/*Relu层类,派生于NeuronLayer类*/
template <typename Dtype>
class ReLULayer : public NeuronLayer<Dtype> {
public:
/**
* @param param provides ReLUParameter relu_param,
* with ReLULayer options:
* - negative_slope (\b optional, default 0).
* the value @f$ \nu @f$ by which negative values are multiplied.
*/
/*构造函数,NeuronLayer层的参数显式传递给ReluLayer,这些参数就是protobuf文件中存储的参数*/
explicit ReLULayer(const LayerParameter& param)
: NeuronLayer<Dtype>(param) {}
/*内联函数,将当前层类型返回*/
virtual inline const char* typ