Keras 中Leaky ReLU等高级激活函数的用法


Posted in Python onJuly 05, 2020

在用Keras来实现CNN等一系列网络时,我们经常用ReLU作为激活函数,一般写法如下:

from keras import layers
from keras import models
 
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) 
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu')) 
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

上面这段代码实现了一个基本的卷积神经网络,用ReLU作为激活函数,关于ReLU具体内容不做详细介绍。还有一些常用的主流激活函数:

softmax: 在多分类中常用的激活函数,是基于逻辑回归的。

Softplus:softplus(x)=log(1+e^x),近似生物神经激活函数,最近出现的。

Relu:近似生物神经激活函数,最近出现的。

tanh:双曲正切激活函数,也是很常用的。

sigmoid:S型曲线激活函数,最常用的。

hard_sigmoid:基于S型激活函数。

linear:线性激活函数,最简单的。

主流的激活函数可以如上述例子一样通过名称直接使用,但是还有一些复杂的激活函数如:Leaky ReLU、PReLU是不可以这样直接使用的,必须使用add方法将高级激活函数作为层(layer)来使用,举例如下:

from keras import layers
from keras import models
from keras.layers import LeakyReLU
 
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2, 2))) 
 
model.add(layers.Conv2D(64, (3, 3))) 
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2, 2)))
 
model.add(layers.Conv2D(64, (3, 3))
model.add(LeakyReLU(alpha=0.05))

这里我们在卷积层中去掉激活函数的参数,并在卷积层后加入高级激活层,下面来测试:

>>model.summary()

Keras 中Leaky ReLU等高级激活函数的用法

这里从整个网络结构的结果可以看出,卷积层后确实加入了一层新的激活层,使用的是LeakyReLU函数。

补充知识:Keras 调用leaky_relu

Keras 中有leaky_relu的实现。leaky_relu被整合进了relu函数。

参考官方文档:

https://tensorflow.google.cn/api_docs/python/tf/keras/backend/relu?hl=en

Arguments
x A tensor or variable.
alpha A scalar, slope of negative section (default=0.).
max_value float. Saturation threshold.
threshold float. Threshold value for thresholded activation.

alpha(超参数)值控制负数部分线性函数的梯度。当alpha = 0 ,是原始的relu函数。当alpha >0,即为leaky_relu。

查看源码,在Keras.backbend 中,也是调用tensorflow.python.ops库nn中的leaky_relu函数实现的:

def relu(x, alpha=0., max_value=None, threshold=0):
 """Rectified linear unit.
 With default values, it returns element-wise `max(x, 0)`.
 Otherwise, it follows:
 `f(x) = max_value` for `x >= max_value`,
 `f(x) = x` for `threshold <= x < max_value`,
 `f(x) = alpha * (x - threshold)` otherwise.
 Arguments:
   x: A tensor or variable.
   alpha: A scalar, slope of negative section (default=`0.`).
   max_value: float. Saturation threshold.
   threshold: float. Threshold value for thresholded activation.
 Returns:
   A tensor.
 """

 if alpha != 0.:
  if max_value is None and threshold == 0:
   return nn.leaky_relu(x, alpha=alpha)  ##在这里调用了leaky_relu

  if threshold != 0:
   negative_part = nn.relu(-x + threshold)
  else:
   negative_part = nn.relu(-x)

 clip_max = max_value is not None

 if threshold != 0:
  # computes x for x > threshold else 0
  x = x * math_ops.cast(math_ops.greater(x, threshold), floatx())
 elif max_value == 6:
  # if no threshold, then can use nn.relu6 native TF op for performance
  x = nn.relu6(x)
  clip_max = False
 else:
  x = nn.relu(x)

 if clip_max:
  max_value = _constant_to_tensor(max_value, x.dtype.base_dtype)
  zero = _constant_to_tensor(0, x.dtype.base_dtype)
  x = clip_ops.clip_by_value(x, zero, max_value)

 if alpha != 0.:
  alpha = _to_tensor(alpha, x.dtype.base_dtype)
  x -= alpha * negative_part
 return x

以上这篇Keras 中Leaky ReLU等高级激活函数的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之玩转字符串(1)
Sep 14 Python
200行自定义python异步非阻塞Web框架
Mar 15 Python
python 全文检索引擎详解
Apr 25 Python
Python实现多线程抓取网页功能实例详解
Jun 08 Python
pyqt5自定义信号实例解析
Jan 31 Python
pandas数据框,统计某列数据对应的个数方法
Apr 11 Python
python 判断网络连通的实现方法
Apr 22 Python
Python2.7 实现引入自己写的类方法
Apr 29 Python
python广度优先搜索得到两点间最短路径
Jan 17 Python
如何让python的运行速度得到提升
Jul 08 Python
Python 数据可视化工具 Pyecharts 安装及应用
Apr 20 Python
Python使用pandas导入xlsx格式的excel文件内容操作代码
Dec 24 Python
Django --Xadmin 判断登录者身份实例
Jul 03 #Python
详解Python多线程下的list
Jul 03 #Python
Python 字符串池化的前提
Jul 03 #Python
Pycharm打开已有项目配置python环境的方法
Jul 03 #Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
Jul 03 #Python
windows支持哪个版本的python
Jul 03 #Python
Django Form设置文本框为readonly操作
Jul 03 #Python
You might like
图书管理程序(三)
2006/10/09 PHP
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
2014/04/24 PHP
PHP采用get获取url汉字出现乱码的解决方法
2014/11/13 PHP
php检查页面是否被百度收录
2015/10/28 PHP
PHP实现将标点符号正则替换为空格的方法
2017/08/09 PHP
详细对比php中类继承和接口继承
2018/10/11 PHP
自动完成JS类(纯JS, Ajax模式)
2009/03/12 Javascript
Javascript实现CheckBox的全选与取消全选的代码
2010/07/20 Javascript
js string 转 int 注意的问题小结
2013/08/15 Javascript
EasyUI,点击开启编辑框,并且编辑框获得焦点的方法
2015/03/01 Javascript
JS简单限制textarea内输入字符数量的方法
2015/10/14 Javascript
jQuery通过deferred对象管理ajax异步
2016/05/20 Javascript
Vue2学习笔记之请求数据交互vue-resource
2017/02/23 Javascript
JS实现按钮控制计时开始和停止功能
2017/07/27 Javascript
JS基于对象的特性实现去除数组中重复项功能详解
2017/11/17 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
通过JS判断网页是否为手机打开
2020/10/28 Javascript
Windows和Linux下Python输出彩色文字的方法教程
2017/05/02 Python
基于python实现KNN分类算法
2020/04/23 Python
使用Python OpenCV为CNN增加图像样本的实现
2019/06/10 Python
基于python实现学生信息管理系统
2019/11/22 Python
如何使用python切换hosts文件
2020/04/29 Python
Jacques Lemans德国:奥地利钟表品牌
2019/12/26 全球购物
澳大利亚在线消费电子产品商店:TobyDeals
2020/01/05 全球购物
自我评价的范文
2014/02/02 职场文书
感恩寄语大全
2014/04/11 职场文书
地球一小时倡议书
2014/04/15 职场文书
基石观后感
2015/06/12 职场文书
实践论读书笔记
2015/06/29 职场文书
网络研修心得体会
2016/01/08 职场文书
2019银行竞聘书
2019/06/21 职场文书
2020年元旦晚会策划书模板
2019/12/30 职场文书
Python文件的操作示例的详细讲解
2021/04/08 Python
Redis安装启动及常见数据类型
2021/04/14 Redis
解决linux下redis数据库overcommit_memory问题
2022/02/24 Redis
SQL Server一个字符串拆分多行显示或者多行数据合并成一个字符串
2022/05/25 SQL Server