Caffe代码阅读11:absval_layer层的实现
2016-08-17 10:22
771 查看
这一层比较简单:主要就是求绝对值,反传部分的代码也很简单
里头用到了caffe_abs这个函数以及caffe_cpu_sign这两个函数
需要注意的是caffe_cpu_sign在math_functions.hpp里头定义得比较特别
在math_functions.hpp里只有caffe_sign,通过一个宏定义生成了caffe_cpu_sign这个函数
整体来说没啥特别的内容,直接上代码吧。
里头用到了caffe_abs这个函数以及caffe_cpu_sign这两个函数
需要注意的是caffe_cpu_sign在math_functions.hpp里头定义得比较特别
在math_functions.hpp里只有caffe_sign,通过一个宏定义生成了caffe_cpu_sign这个函数
整体来说没啥特别的内容,直接上代码吧。
#include <vector> #include "caffe/layers/absval_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { template <typename Dtype> void AbsValLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { NeuronLayer<Dtype>::LayerSetUp(bottom, top); CHECK_NE(top[0], bottom[0]) << this->type() << " Layer does not " "allow in-place computation."; } template <typename Dtype> void AbsValLayer<Dtype>::Forward_cpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const int count = top[0]->count(); Dtype* top_data = top[0]->mutable_cpu_data(); caffe_abs(count, bottom[0]->cpu_data(), top_data); } template <typename Dtype> void AbsValLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { const int count = top[0]->count(); // 前面一层关于本层top的偏导 const Dtype* top_diff = top[0]->cpu_diff(); if (propagate_down[0]) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); // 将bottom_data里头的每个元素的正负值复制到bottom_diff caffe_cpu_sign(count, bottom_data, bottom_diff); // 计算偏导数计算关于本层bottom的偏导 // 将关于top的偏导乘以当前层bottom中每个数据的符号 caffe_mul(count, bottom_diff, top_diff, bottom_diff); } } #ifdef CPU_ONLY STUB_GPU(AbsValLayer); #endif INSTANTIATE_CLASS(AbsValLayer); REGISTER_LAYER_CLASS(AbsVal); } // namespace caffe
相关文章推荐
- Caffe代码阅读8:absval_layer层的实现
- caffe代码阅读10:Caffe中卷积的实现细节(涉及到BaseConvolutionLayer、ConvolutionLayer、im2col等)-2016.4.3
- caffe代码阅读4:DataTransformer以及io的实现细节-2016.3.16
- caffe代码阅读6:Filler的实现细节-2016.3.18
- caffe代码阅读1:blob的实现细节-2016.3.14
- caffe代码阅读5:Layer的实现细节
- caffe代码阅读8: Data_layers的实现细节(各个数据读取层的实现细节) 2016.3.25-28
- caffe代码阅读7:Caffe中卷积的实现
- caffe代码阅读2:DataTransformer以及io的实现细节
- caffe代码阅读3:data_reader、internalthread以及blocking_queue的实现细节-2016.3.15
- caffe代码阅读9:SyncedMemory的实现细节-2016.3.28
- caffe代码阅读6:SyncedMemory的j介绍与实现
- Caffe代码阅读3-Layer的实现细节
- caffe代码阅读10:Caffe中卷积的实现细节(涉及到BaseConvolutionLayer、ConvolutionLayer、im2col等)-2016.4.3
- caffe代码阅读4:LayerRegistry的介绍与实现
- caffe代码阅读3:Filler的实现
- caffe代码阅读1:Layer的介绍与实现细节
- caffe代码阅读7:LayerRegistry的实现细节-2016.3.18
- Caffe代码阅读2-common实现细节
- caffe代码阅读2:common的实现细节-2016.3.14