激活函数
激活函数函数代码
Sigmoidtf.nn.sigmoid(x)ReLUtf.nn.relu(x)LeakyReLUtf.nn.leaky_relu(x, alpha)Tanhtf.nn.tanh(x)
Sigmoid
Sigmoid图像
logistic函数,因为其形状为S型,又称Sigmoid函数
f
(
x
)
=
e
x
/
(
1
+
e
x
)
=
1
/
(
1
+
e
−
x
)
,
.
\ f(x) = e^{x}/(1+e^{x})\ =1/(1+e^{-x})\\,.
f(x)=ex/(1+ex) =1/(1+e−x),.
Sigmoid特点
单调函数定义域即自变量
x
i
x_i
xi的取值为
(
−
∞
,
+
∞
)
\ (-\infty, +\infty)
(−∞,+∞),值域为
(
0
,
1
)
(0,1)
(0,1)处处可微、处处可导可做概率分布函数
Sigmoid调用
tf.nn.sigmoid(x)
import tensorflow
as tf
x
= tf
.linspace
(-7.,7,10)
a
= tf
.nn
.sigmoid
(x
)
print(x
)
print(a
)
out
:
tf
.Tensor
(
[-7. -5.4444447 -3.8888888 -2.333333 -0.7777777 0.7777777
2.333334 3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
tf
.Tensor
(
[9.1102719e-04 4.3017268e-03 2.0057559e-02 8.8399708e-02 3.1479907e-01
6.8520093e-01 9.1160035e-01 9.7994250e-01 9.9569833e-01 9.9908900e-01], shape
=(10,), dtype
=float32
)
ReLU
ReLU 图像
线性整流函数(Rectified Linear Unit, ReLU),又称修正线性单元,是一种人工神经网络中常用的激活函数(activation function):
f
(
x
)
=
m
a
x
(
0
,
x
)
f(x)=max(0,x)
f(x)=max(0,x)
ReLU特点
更加有效率的梯度下降以及反向传播:避免了梯度爆炸和梯度消失问题简化计算过程:没有了其他复杂激活函数中诸如指数函数的影响同时活跃度的分散性使得神经网络整体计算成本下降
ReLU调用
tf.nn.relu(x)
import tensorflow
as tf
x
= tf
.linspace
(-7.,7,10)
a
= tf
.nn
.relu
(x
)
print(x
)
print(a
)
out
:
tf
.Tensor
(
[-7. -5.4444447 -3.8888888 -2.333333 -0.7777777 0.7777777
2.333334 3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
tf
.Tensor
(
[0. 0. 0. 0. 0. 0.7777777 2.333334
3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
LeakyReLU
LeakyReLU图像
f
(
x
)
=
m
a
x
(
a
l
p
h
a
∗
x
,
x
)
f(x)=max(alpha*x,x)
f(x)=max(alpha∗x,x)
leakyReLU特点
ReLU 函数在𝑥 < 0时导数值恒为 0,也可能会造成梯度弥散现象当𝑝 ≠ 0时, 𝑥 < 0处能够获得较小的导数值𝑝,从而避免出现梯度弥散现 象
leakyReLU调用
tf.nn.leaky_relu(x, alpha)alpha为斜率tf.nn.leaky_relu 对应的类为 layers.LeakyReLU,可以通过LeakyReLU(alpha)创建LeakyReLU网络层放置合适位置
import tensorflow
as tf
from tensorflow
.keras
import layers
from tensorflow
.keras
import Sequential
x
= tf
.linspace
(-7.,7,10)
a
= tf
.nn
.leaky_relu
(x
,0.1)
print(x
)
print(a
)
out
:
tf
.Tensor
(
[-7. -5.4444447 -3.8888888 -2.333333 -0.7777777 0.7777777
2.333334 3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
tf
.Tensor
(
[-0.7 -0.5444445 -0.3888889 -0.2333333 -0.07777777 0.7777777
2.333334 3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
Tanh
Tanh图像
t
a
n
h
(
x
)
=
(
e
x
−
e
−
x
)
/
(
e
x
+
e
−
x
)
=
2
S
i
g
m
o
i
d
(
2
x
)
−
1
tanh(x) =(e^x − e^{−x})/(e^x + e^{−x}) = 2Sigmoid(2x)-1
tanh(x)=(ex−e−x)/(ex+e−x)=2Sigmoid(2x)−1
Tanh特点
tanh 激活函数可通过 Sigmoid 函数缩放平移后实现向量元素值的范围被映射到(−1,1)之间
Tanh调用
import tensorflow
as tf
x
= tf
.linspace
(-7.,7,10)
a
= tf
.nn
.tanh
(x
)
print(x
)
print(a
)
out
:
tf
.Tensor
(
[-7. -5.4444447 -3.8888888 -2.333333 -0.7777777 0.7777777
2.333334 3.8888893 5.4444447 7. ], shape
=(10,), dtype
=float32
)
tf
.Tensor
(
[-0.99999833 -0.99996257 -0.9991625 -0.9813682 -0.65142936 0.65142936
0.9813681 0.9991625 0.99996257 0.99999833], shape
=(10,), dtype
=float32
)