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)分析的更多相关文章

  1. caffe 每层结构

    如何在Caffe中配置每一个层的结构 最近刚在电脑上装好Caffe,由于神经网络中有不同的层结构,不同类型的层又有不同的参数,所有就根据Caffe官网的说明文档做了一个简单的总结. 1. Vision ...

  2. Android Hal层简要分析

    Android Hal层简要分析 Android Hal层(即 Hardware Abstraction Layer)是Google开发的Android系统里上层应用对底层硬件操作屏蔽的一个软件层次, ...

  3. ZT Android4.2关于bluetooth在HAL层的分析(1)

    我的电子杂烩饭 http://blog.sina.com.cn/wuchuchu2012 [订阅][手机订阅] 首页 博文目录 图片 关于我 正文 字体大小:大 中 小 Android4.2关于blu ...

  4. 1、Caffe数据层及参数

    要运行Caffe,需要先创建一个模型(model),每个模型由许多个层(layer)组成,每个层又都有自己的参数, 而网络模型和参数配置的文件分别是:caffe.prototxt,caffe.solv ...

  5. caffe︱ImageData层、DummyData层作为原始数据导入的应用

    Part1:caffe的ImageData层 ImageData是一个图像输入层,该层的好处是,直接输入原始图像信息就可以导入分析. 在案例中利用ImageData层进行数据转化,得到了一批数据. 但 ...

  6. 010商城项目:商品类目的选择——Dao,Service.Action层的分析

    我们现在开始写商品类选择这个功能: 先看效果: 当我们点击"新增商品"---->"选择目录"然后从数据库中查出来数据并显示了. 我们分析数据库的那张表: ...

  7. caffe特征层可视化

    #参考1:https://blog.csdn.net/sushiqian/article/details/78614133#参考2:https://blog.csdn.net/thy_2014/art ...

  8. 【转】caffe数据层及参数

    原文: 要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个层(layer)构成,每一层又由许多参数组成.所有的参数都定义在caffe.proto ...

  9. [转] caffe视觉层Vision Layers 及参数

    视觉层包括Convolution, Pooling, Local Response Normalization (LRN), im2col等层. 1.Convolution层: 就是卷积层,是卷积神经 ...

随机推荐

  1. ASP.NET中各种缓存技术的特点及使用场景

    对于一些不经常改变却经常被request的数据,我们喜欢将它们缓存在内存.这样用户请求时先到缓存中去取,如果缓存中没有,再去数据库拿,提高响应速度.缓存一般实现在BLL,这样可以与DAL分离,更换数据 ...

  2. 利用hexo+github创建个人博客

    因为想拥有一个独属于自己的个人博客啊. 安装部署hexo 进入一个安全的目录,cd ~/Desktop 在 GitHub 上新建一个空 repo,repo 名称是「你的GitHub用户名.github ...

  3. Fragment的理解

    1.生命周期    启动Fragment时: onAttachonCreateonCreateViewonViewCreatedonActivityCreatedonStartonResume 启动后 ...

  4. c++标准库都有哪些文件

    from:http://topic.csdn.net/u/20090201/16/3bd41b72-5694-474e-a68b-98b2f070e76b.html C++标准库的所有头文件都没有扩展 ...

  5. Photoshop保存文件时的选项

    以 JPEG 格式存储 您可以使用"存储为"命令以 JPEG (*.jpg) 格式存储 CMYK.RGB 和灰度图像.JPEG 通过有选择地扔掉数据来压缩文件大小.也可以使用&qu ...

  6. elasticsearch集群安全重启节点

    es2.x 关闭集群的动态分片:(动态分片开启状态如果节点宕机了,会导致集群重新分配数据进行数据转移,会导致节点直接大量传输数据)curl -XPUT 'http://192.168.248.193: ...

  7. LayUI中实现上级下拉框动态加载下级下拉框js

    js代码: var form = layui.form, layer = layui.layer; form.on("select(上级)", function(data){ va ...

  8. Eclipse中使用GIT更新项目

    GIT更新项目: 右击项目——Team——Pull:

  9. Expressjs配置加载器

    有些东西就是操刀开干,没什么好解释的.... 问题引入 解决问题 直接上码 env.js index.js 使用方法 初始化 使用方法 写在最后 问题引入 大家都知道在日常的研发过程中,我们的程序会有 ...

  10. webpack加载器(Loaders)

    加载器(Loaders) loader 是对应用程序中资源文件进行转换.它们是(运行在 Node.js 中的)函数,可以将资源文件作为参数的来源,然后返回新的资源文件. 示例 例如,你可以使用 loa ...