通常情况下,我们所说的Sigmoid函数定义如下:
σ
(
x
)
=
1
1
+
e
−
x
=
e
x
e
x
+
1
.
\sigma(x)=\frac{1}{1+e^{-x}}=\frac{e^x}{e^x+1}.
σ(x)=1+e−x1=ex+1ex.
它的形状如下:
导数如下:
d
σ
(
x
)
d
x
=
σ
(
x
)
⋅
(
1
−
σ
(
x
)
)
.
\frac{d\sigma(x)}{dx}=\sigma(x)\cdot (1-\sigma(x)).
dxdσ(x)=σ(x)⋅(1−σ(x)).
本篇博文讲
σ
(
x
)
\sigma(x)
σ(x)导数的推导过程。
注意
Sigmoid函数实际上是指形状呈S形的一组曲线[1],上述公式中的 σ ( x ) \sigma(x) σ(x)正式名称为logistic函数,为Sigmoid函数簇的一个特例(这也是 σ ( x ) \sigma(x) σ(x)的另一个名字,即 l o g s i g logsig logsig的命名来源)。我们经常用到的hyperbolic tangent函数,即 tanh x = e x − e − x e x + e − x \tanh x=\frac{e^x-e^{-x}}{e^x+e^{-x}} tanhx=ex+e−xex−e−x也是一种sigmoid函数。
下文依旧称 σ ( x ) \sigma(x) σ(x)为logistic函数。
logistic函数的有效工作范围是 ( − 10 , 10 ) (-10,10) (−10,10),从它的图像也可以看出来:在 ( − 10 , 10 ) (-10,10) (−10,10)以外,函数值的变化非常小。那么问题来了,如果用logistic函数当神经网络的激活函数,当 x > 10 x>10 x>10或者 x < − 10 x<-10 x<−10时会出现梯度消失(gradient vanishing)的问题,即 d σ ( x ) d x ≈ 0 \frac{d\sigma(x)}{dx}\approx 0 dxdσ(x)≈0。换句话说,梯度下降算法会进入死胡同。这一点要特别注意。
求导过程[2]
d
σ
(
x
)
d
x
=
d
d
x
[
1
1
+
e
−
x
]
=
d
d
x
(
1
+
e
−
x
)
−
1
=
−
(
1
+
e
−
x
)
−
2
(
−
e
−
x
)
\frac{d\sigma(x)}{dx}=\frac{d}{dx}\left[\frac{1}{1+e^{-x}}\right] = \dfrac{d}{dx} \left( 1 + \mathrm{e}^{-x} \right)^{-1} = -(1 + e^{-x})^{-2}(-e^{-x})
dxdσ(x)=dxd[1+e−x1]=dxd(1+e−x)−1=−(1+e−x)−2(−e−x)
=
e
−
x
(
1
+
e
−
x
)
2
= \dfrac{e^{-x}}{\left(1 + e^{-x}\right)^2}
=(1+e−x)2e−x
=
1
1
+
e
−
x
⋅
e
−
x
1
+
e
−
x
= \dfrac{1}{1 + e^{-x}\ } \cdot \dfrac{e^{-x}}{1 + e^{-x}}
=1+e−x 1⋅1+e−xe−x
=
1
1
+
e
−
x
⋅
(
1
+
e
−
x
)
−
1
1
+
e
−
x
= \dfrac{1}{1 + e^{-x}\ } \cdot \dfrac{(1 + e^{-x}) - 1}{1 + e^{-x}}
=1+e−x 1⋅1+e−x(1+e−x)−1
=
1
1
+
e
−
x
⋅
(
1
+
e
−
x
1
+
e
−
x
−
1
1
+
e
−
x
)
= \dfrac{1}{1 + e^{-x}\ } \cdot \left( \dfrac{1 + e^{-x}}{1 + e^{-x}} - \dfrac{1}{1 + e^{-x}} \right)
=1+e−x 1⋅(1+e−x1+e−x−1+e−x1)
=
1
1
+
e
−
x
⋅
(
1
−
1
1
+
e
−
x
)
= \dfrac{1}{1 + e^{-x}\ } \cdot \left( 1 - \dfrac{1}{1 + e^{-x}} \right)
=1+e−x 1⋅(1−1+e−x1)
=
σ
(
x
)
⋅
(
1
−
σ
(
x
)
)
= \sigma(x) \cdot (1 - \sigma(x))
=σ(x)⋅(1−σ(x))
复习一下一般求导法则
- 乘法法则
( f ⋅ g ) ′ = f ′ g + f g ′ (f\cdot g)'=f'g+fg' (f⋅g)′=f′g+fg′ - 除法法则
( f g ) ′ = f ′ g − f g ′ g 2 ( g ≠ 0 ) \left(\frac{f}{g}\right)'=\frac{f'g-fg'}{g^2} \qquad (g\neq 0) (gf)′=g2f′g−fg′(g=0) - 倒数法则
( 1 g ) ′ = − g ′ g 2 ( g ≠ 0 ) \left(\frac{1}{g}\right)'=\frac{-g'}{g^2} \qquad (g\neq 0) (g1)′=g2−g′(g=0) - 复合函数求导法则
( f [ g ( x ) ] ) ′ = d f ( g ) d g d g d x (f[g(x)])'=\frac{df(g)}{dg}\frac{dg}{dx} (f[g(x)])′=dgdf(g)dxdg
[1] https://en.wikipedia.org/wiki/Sigmoid_function
[2] https://math.stackexchange.com/questions/78575/derivative-of-sigmoid-function-sigma-x-frac11e-x