版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haluoluo211/article/details/82956157
激活函数如:ReLu
,Sigmoid
等layer
相对较为简单,所以在分析InnerProductLayer
前,我们先看下激活函数层。
常见激活层ReLU
的使用示例如下:
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
我们可以看到bottom
和top
是同一个,那是因为激活层运算均为同址计算
。
NeuronLayer
作为激活层的基类, 继承Layer,定义如下:
template <typename Dtype>
class NeuronLayer : public Layer<Dtype> {
public:
explicit NeuronLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
};
由于每个激活layer的bottom, top的shape一样,因此NeuronLayer定义了Reshape函数:
template <typename Dtype>
void NeuronLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
top[0]->ReshapeLike(*bottom[0]);
}
ReLULayer的核心函数在与Forward_cpu,Backward_cpu,而且相对也比较简单(简单起见删除了negative_slope运算):
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));
}
}
反向传播主要是更新blob中的diff_反向传递梯度:
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
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();
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const int count = bottom[0]->count();
for (int i = 0; i < count; ++i) {
bottom_diff[i] = top_diff[i] * ((bottom_data[i] > 0);
}
}
}