Caffe 激励层(Activation)分析
Caffe_Activation
一般来说,激励层的输入输出尺寸一致,为非线性函数,完成非线性映射,从而能够拟合更为复杂的函数表达式激励层都派生于NeuronLayer: class XXXlayer : public NeuronLayer
1.基本函数
激励层的基本函数较为简单,主要包含构造函数和前向、后向函数
explicit XXXLayer(const LayerParameter& param)
:NeuronLayer<Dtype>(param){}
virtual inline const char* type() const { return "layerNane"; }
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
2.常用激励函数
(1) Relu/PRelu Rectufied Linear Units
ReLU的函数表达式为\(f(x) = x*(x>0) + negative\_slope*x*(x <= 0)\) 具体实现如下
//forward_cpu
template <typename Dtype>
void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>& top){ // 根据bottom求解top
const Dtype* bottom_data = bottom[0]->cpu_data();//const 不可修饰
Dtype* top_data = top[0]->mutable_cpu_data();//可修饰
const int count = bottom[0]->count();//因为count_一致,也可用top
Dtype negative_slope = this->layer_param_.relu_param().negative_slope();
for (size_t i = 0; i < count; i++) {
top_data[i] = bottom_data[i]*(bottom_data[i] > 0)
+ negative_slope*bottom_data[i]*(bottom_data[i] <= 0);
}
}
//Backward_cpu
// 导数形式 f'(x) = 1 x>0 ; negative_slope*x x<0
template <typename Dtype>
void ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,const vector<Blob<Dtype>*>& bottom){
const Dtype* top_diff = top[0].cpu_diff();//top diff
const Dtype* bottom_data = bottom[0].cpu_data();//用以判断x是否大于0
Dtype* bottom_diff = bottom[0].cpu_diff();//bottom diff
const int count = bottom[0].count();
for (size_t i = 0; i < count; i++) {
bottom_diff[i] = top_diff[i]*(bottom_data[i] > 0)
+negative_slope*(bottom_data[i] <= 0);
}
}
// Relu 函数形式简单,导函数简单,能有效的解决梯度弥散问题,但是当x小于0时,易碎
// 但是网络多为多神经元,所以实际应用中不会影响到网络的正常训练。
(2) Sigmoid (S曲线)
Sigmoid函数表达式为\(f(x) = 1./(1+exp(-x))\);值域0-1,常作为BP神经网络的激活函数
由于输出为0-1,也作为logistic回归分析的概率输出函数。具体实现如下;
//定义一个sigmoid函数方便计算
template <typename Dtype>
inline Dtype sigmoid(Dtype x){
return 1./(1.+exp(-x));
}
//前向 直接带入sigmoid函数即可
template <typename Dtype>
void SigmoidLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
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();//N*C*H*W;
for (size_t i = 0; i < count; i++) {
top_data[i] = sigmoid(bottom_data[i]);
}
}
//Backward_cpu 由于f'(x) = f(x)*(1-f(x)),所以需要top_data
// bottom_diff = top_diff*f'(bottom_data) = top_diff*top_data*(1-top_data)
template <typename Dtype>
void SigmoidLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,vector<Blob<Dtype>*>& bottom){
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); //需要计算
const int count = bottom[0]->count();
for (size_t i = 0; i < count; i++) {
//top_data[i] == sigmoid(bottom_data[i]);
bottom_diff[i] = top_diff[i]*top_data[i]*(1.-top_data[i]);
}
}
// Sigmoid函数可以作为二分类的概率输出,也可以作为激活函数完成非线性映射,但是网络
// 增加时,容易出现梯度弥散问题,目前在CNN中基本不使用
(3)TanH,双正切函数
TanH函数的表达式为 \(\frac{(1.-exp(-2x))}{(1.+exp(-2x))}\);值域0-1,与sigmoid函数有相同的问题,
但是TanH在RNN中使用较为广泛,理由参考,具体实现如下所示。
//定义一个tanH的函数表达式,实际已经封装
inline Dtype TanH(Dtype x){
return (1.-exp(-2*x))/(1.+exp(-2*x));
}
//Forward_cpu
template <typename Dtype>
void TanHLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
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();
for (size_t i = 0; i < count; i++) {
top[i] = TanH(bottom_data[i]);
}
}
//Backward_cpu f'(x) = 1-f(x)*f(x);
// bottom_diff = top_diff(1-top_data*top_data);
template <typename Dtype>
void TanHLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,vector<Blob<Dtype>*>& bottom){
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); //需要计算
const int count = bottom[0]->count();
for (size_t i = 0; i < count; i++) {
//top_data[i] == TanH(bottom_data[i]);
bottom_diff[i] = top_diff[i]*(1.-top_data[i]*top_data[i]);
}
}
其他的激励函数就不在枚举,可以查看具体的caffe源码,实现大致相同
3.说明
(1) 梯度弥散和梯度爆炸
网络方向传播时,loss经过激励函数会有\(loss*\partial{f(x)}\),而如sigmoid的函数,
max(\(\partial{f(x)}\))只有1/4因此深层网络传播时loss越来越小,则出现前层网络未完整学习而后层网络学习饱和的现象
(2) Caffe激励层的构建
如上述的代码所示,激励层主要完成forward和Bacward的函数实现即可,由构建的函数表达式推导出它的导函数形式,弄懂bottom_data,top_data,bottom_diff,top_diff即可
Caffe 激励层(Activation)分析的更多相关文章
- caffe 每层结构
如何在Caffe中配置每一个层的结构 最近刚在电脑上装好Caffe,由于神经网络中有不同的层结构,不同类型的层又有不同的参数,所有就根据Caffe官网的说明文档做了一个简单的总结. 1. Vision ...
- Android Hal层简要分析
Android Hal层简要分析 Android Hal层(即 Hardware Abstraction Layer)是Google开发的Android系统里上层应用对底层硬件操作屏蔽的一个软件层次, ...
- ZT Android4.2关于bluetooth在HAL层的分析(1)
我的电子杂烩饭 http://blog.sina.com.cn/wuchuchu2012 [订阅][手机订阅] 首页 博文目录 图片 关于我 正文 字体大小:大 中 小 Android4.2关于blu ...
- 1、Caffe数据层及参数
要运行Caffe,需要先创建一个模型(model),每个模型由许多个层(layer)组成,每个层又都有自己的参数, 而网络模型和参数配置的文件分别是:caffe.prototxt,caffe.solv ...
- caffe︱ImageData层、DummyData层作为原始数据导入的应用
Part1:caffe的ImageData层 ImageData是一个图像输入层,该层的好处是,直接输入原始图像信息就可以导入分析. 在案例中利用ImageData层进行数据转化,得到了一批数据. 但 ...
- 010商城项目:商品类目的选择——Dao,Service.Action层的分析
我们现在开始写商品类选择这个功能: 先看效果: 当我们点击"新增商品"---->"选择目录"然后从数据库中查出来数据并显示了. 我们分析数据库的那张表: ...
- caffe特征层可视化
#参考1:https://blog.csdn.net/sushiqian/article/details/78614133#参考2:https://blog.csdn.net/thy_2014/art ...
- 【转】caffe数据层及参数
原文: 要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个层(layer)构成,每一层又由许多参数组成.所有的参数都定义在caffe.proto ...
- [转] caffe视觉层Vision Layers 及参数
视觉层包括Convolution, Pooling, Local Response Normalization (LRN), im2col等层. 1.Convolution层: 就是卷积层,是卷积神经 ...
随机推荐
- json字符串与json对象的相互转换
什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSO ...
- Core Java(三)
三.运算符&流程控制 运算符---http://blog.csdn.net/typa01_kk/article/details/45000535 在一个程序执行的过程中,各条语句的执行顺序对程 ...
- Codeforces Round #468 (Div. 2 )D. Peculiar apple-tree_BFS
题目简单,不多解释. Code: #include<cstdio> #include<queue> using namespace std; const int maxn = ...
- mySql 使用 SQL 文件脚本 failed to open file 注意事项
1.路径不要有中文,其实最好是全英文 2.路径可以有空格 3.路径两头不要加引号 4.作为一个MySQL命令,source C:/lib/a.sql; 后边的分号是要的. 5.使用 unix 路径风格 ...
- vue项目 预览照片的插件 v-viewer
查看图片主要使用的旋转.翻转.缩放.上下切换.键盘操作等功能都有. 1.首先是安装 npm install v-viewer --save 2.安装完在main.js里面引用(还要记得引用它的css样 ...
- 对于 wepy 不是内部或外部命令 -- 的解决办法
闲来没事继续研究自己之前一直未解决的问题, 就是自己笔记本安装wepy-cli,一直提示"wepy 不是内部或外部命令". 因为公司里面用的是这个框架, 想着自己在家没事的时候去 ...
- HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
- Linux系统下安装 rpm 软件和源代码 软件包
RPM 安装方式 RPM是一个包安装管理软件,我们可以使用这个工具安装 .rpm 类型的软件.Linux的rpm包很多都能在光盘的Packages 包中找得到.首先挂载一下光盘,查看Packages中 ...
- Linux赛车游戏 SuperTuxKart 1.0 正式发布
SuperTuxKart是一款受Mario Kart(马里奥赛车)启发并以Linux/Tux为主题的开源赛车游戏,经过12年多的开发,已经达到1.0版本.并且确定这个版本确实是一个重要的里程碑. Su ...
- 如何让myeclipse左边选中文件后自动关联右边树
在左侧项目树的右上角下拉菜单里有link with editor 点击即可