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层: 就是卷积层,是卷积神经 ...
随机推荐
- 含神经网络的离线AI翻译 APP
功能特性 下载 https://www.microsoft.com/en-us/store/p/translator/9wzdncrfj3pg
- 手机、电脑、安卓、iOS、微信浏览器判断
微信浏览器判断: // true为微信浏览器function is_weixin() { var ua = window.navigator.userAgent.toLowerCase(); if ( ...
- shell-6.环境变量配置文件
1. 2. 3. 4. 5. 6.
- JDBC程序实例
实例 ( Statement ): public class JDBC { public static void main(String[] args) throws Exception { Conn ...
- Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题
不解释,题目过水 Code: #include<cstdio> #include<cmath> #include<algorithm> using namespac ...
- Pyhton学习——Day10
#################################################################################################### ...
- UVA10082-WERTYU(紫书例题3.2)
A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...
- VUE使用中踩过的坑
前言 vue如今可谓是一匹黑马,github star数已居第一位!前端开发对于vue的使用已经越来越多,它的优点就不做介绍了,本篇是我对vue使用过程中以及对一些社区朋友提问我的问题中做的一些总结, ...
- 数据库-mongodb-Gridfs
GridFS是一种将大型文件存储在MongoDB的文件规范: 数据库支持以BSON格式保存二进制对象. 但是MongoDB中BSON对象最大不能超过4MB. GridFS 规范提供了一种透明的机制,可 ...
- ubuntu系统AndroidStudio改动内存大小
位于android-studio/bin文件夹下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize.-XX:Reserve ...