caffe原始碼 之 Relu層

2021-07-24 19:31:24 字數 3671 閱讀 1530

本文主要實現caffe框架中/src/caffe/layers/relu_layer.cpp檔案,該檔案實現的是啟用函式relu。

relu是近些年非常流行的啟用函式。相比於sigmoid與tanh,它具有一定的優越性,這三者對比可見它的函式公式是f(x)=max(0,x)。換句話說,這個啟用函式就是乙個關於0的閾值。如下圖:::

下面記錄我在看relu層時的**注釋:::

relu_layer.hpp:::

#ifndef caffe_relu_layer_hpp_

#define caffe_relu_layer_hpp_

#include

#include "caffe/blob.hpp"

#include "caffe/layer.hpp"

#include "caffe/proto/caffe.pb.h"

#include "caffe/layers/neuron_layer.hpp"

namespace caffe

/*內聯函式,將當前層型別返回*/

virtual

inline

const

char* type() const

protected:

/*** @param bottom input blob vector (length 1)

* -# @f$ (n \times c \times h \times w) @f$

* the inputs @f$ x @f$

* @param top output blob vector (length 1)

* -# @f$ (n \times c \times h \times w) @f$

* the computed outputs @f$

* y = \max(0, x)

* @f$ by default. if a non-zero negative_slope @f$ \nu @f$ is provided,

* the computed outputs are @f$ y = \max(0, x) + \nu \min(0, x) @f$.

*///前向傳播cpu實現

virtual

void forward_cpu(const

vector

*>& bottom,

const

vector

*>& top);

//前向傳播gpu實現

virtual

void forward_gpu(const

vector

*>& bottom,

const

vector

*>& top);

/*注意:前向傳播函式以bottom為輸入,top為輸出*/

/** * @brief computes the error gradient w.r.t. the relu inputs.

** @param top output blob vector (length 1), providing the error gradient with

* respect to the outputs

* -# @f$ (n \times c \times h \times w) @f$

* containing error gradients @f$ \frac @f$

* with respect to computed outputs @f$ y @f$

* @param propagate_down see layer::backward.

* @param bottom input blob vector (length 1)

* -# @f$ (n \times c \times h \times w) @f$

* the inputs @f$ x @f$; backward fills their diff with

* gradients @f$

* \frac = \left\

* 0 & \mathrm \; x \le 0 \\

* \frac & \mathrm \; x > 0

* \end \right.

* @f$ if propagate_down[0], by default.

* if a non-zero negative_slope @f$ \nu @f$ is provided,

* the computed gradients are @f$

* \frac = \left\

* \nu \frac & \mathrm \; x \le 0 \\

* \frac & \mathrm \; x > 0

* \end \right.

* @f$.

*///返向傳播cpu實現

virtual

void backward_cpu(const

vector

*>& top,

const

vector

& propagate_down, const

vector

*>& bottom);

//返向傳播gpu實現

virtual

void backward_gpu(const

vector

*>& top,

const

vector

& propagate_down, const

vector

*>& bottom);

/*注意:返向傳播中bottom為輸出,top為輸入,其中propagate_down為bottom是否返向傳播梯度的bool值的向量,個數與bottom資料個數相同*/

};} // namespace caffe

#endif // caffe_relu_layer_hpp_

relu_layer.cpp:::

#include 

#include

#include "caffe/layers/relu_layer.hpp"

namespace caffe

}/*relu層的返向傳播函式*/

template

void relulayer::backward_cpu(const

vector

*>& top,

const

vector

& propagate_down,

const

vector

*>& bottom)

}}#ifdef cpu_only

stub_gpu(relulayer);

#endif

instantiate_class(relulayer);

} // namespace caffe

Caffe之Scale層原始碼

最近要對模型進行壓縮使用slimming,因此需要scale層對scale diff進行l1正則。所以對原始碼進行了閱讀。公式 y ax b。公式比較簡單。上述公式的意思是,對feature map乘以a,並加b。乙個feature map共用乙個a,因此 a的維度是 c 這是理解原始碼的前提。反向...

caffe原始碼 卷積層

input 3 5 5 c h w pading 1 步長 2 卷積核 2 3 3 3 n c k k output 2 3 3 c h w 如下圖所示 首先需要理解caffe裡面的im2col和col2im 然後 卷積層 其實和 全連線層 差不多了 input 3 4 4 c h w 卷積核 1 ...

Roi Pooling層caffe原始碼解讀

在看fasterrcnn以及和maskrcnn的時候,發現自己對fasterrcnn的roi pooling層的原理還是不是很明白,之前只是知道roi pooling是將rpn輸出的乙個roi的區域對映成乙個固定大小的map,再送入後面的分類層進行分類。最近看了下roi pooling層的原始碼,頓...