- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Keras 中Leaky ReLU等高级激活函数的用法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
在用Keras来实现CNN等一系列网络时,我们经常用ReLU作为激活函数,一般写法如下:
1
2
3
4
5
6
7
8
9
|
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)来使用,举例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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() 。
这里从整个网络结构的结果可以看出,卷积层后确实加入了一层新的激活层,使用的是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函数实现的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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等高级激活函数的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/hesongzefairy/article/details/86707352 。
最后此篇关于Keras 中Leaky ReLU等高级激活函数的用法的文章就讲到这里了,如果你想了解更多关于Keras 中Leaky ReLU等高级激活函数的用法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
对于相当简单的表结构,即。人员、标准和 PersonCriteria(组合表),我现在设置了一个查询,选择所有符合所有选定条件的人。 此时查询本身看起来像这样: SELECT p.PersonID
我在使用高级 SQL 查询时遇到了一些问题,而且我已经有很长时间没有使用 SQL 数据库了。我们使用 MySQL。 背景: 我们将使用两个表: “交易表” 表:expire_history +----
我找不到错误。也许你可以帮助我:我的代码如下: var data = {"product":[{"config":[{"id":"1","price":"100","sku":"1054879634
我有一个列表列表的列表(最后一个列表并不重要) data = [[[['f', 0], 'C'], [['X', 0], 'X']], [[['s', 1], 'X'], [['X', 0], 'X'
我想准备将使用表格的 session ,并在另一个网站上将新项目添加到 session 中。 默认.cs string[] tab = new string[100];
我知道有一些像: Bubble sort Insertion sort Shell sort Merge sort Heapsort Quicksort Bucket sort Radix sort
像https://softwareengineering.stackexchange.com/questions/150616/return-random-list-item-by-its-weigh
我正在开发一个 posix 脚本 (Linux),它获取一个网页,将内容存储在一个变量中并查找字符串“SUCCESS”。如果找到字符串,则不执行循环内容,如果没有找到字符串,则反复执行循环,直到找到为
我不确定这个问题是否已在其他地方得到解答,而且我似乎无法通过谷歌找到任何不是“Hello World”示例的内容...我正在使用 C# .NET 4.0 进行编码。 我正在尝试开发一个控制台应用程序,
我创建了一个房地产网站,我希望按照列表的最后更新和完整性对列表进行排序。所以我一直想弄清楚如何结合最近更新的列表按mysql中的字段(completion_score)进行排序。完成分数将采用 1
只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似。很多场合下需要把同一
之前的章节中,我们学习了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法从 XML 文档中取回数据 本章节我们将继续学习其它重要的 XML DOM
我对我尝试编写的 SQL 查询有疑问。 我需要从数据库中查询数据。该数据库除其他外,还包括以下 3 个字段: Account_ID #, Date_Created, Time_Created 我需要编
我正在使用非常激进的视频压缩,例如 -crf 51 .我将其用于“艺术”效果,因此从普通视频压缩的角度来看,我所做的可能没有意义。 到目前为止,我只使用了非常基本的压缩控制,只使用了 -crf。或 -
我真的在学习 lucene 和 ravendb 上的绳索 - 我在 Raven 中有以下文档 - { "InternalEvent": { "Desec": "MachineInfo: 1
通常 grep 命令用于显示包含指定模式的行。有没有办法在包含指定模式的行之前和之后显示 n 行? 这可以使用awk来实现吗? 最佳答案 是的,使用 grep -B num1 -A num2 在匹配之
我搜索了高低,并尝试了几个小时来操纵似乎适合的各种其他查询,但我没有快乐。 我试图加入 Microsoft SQL Server 2005 中的几个表,其中一个示例是: Company Table (
我有一个如下所示的 XML 文件: teacher1Name
我将如何在 CF 中创建此语句? 显然括号不起作用,但说明了我想要完成的工作。这是什么语法? 编辑: 好的,我了解如何使用 EQ 等等。我有点匆忙地发布了这个。我的问题是关于括号。以这种方式使用它们
主要问题:我需要使用具体对象结构对任何对象结构进行类型扩展。 我在 VS Code 中测试的默认值。 我的解决方案: /** @template A @typedef {{[Ki in keyof A
我是一名优秀的程序员,十分优秀!