1. Sigmod 函数
Sigmod 函数是神经网络中最常用的激活函数之一,其形式如下:
sigmod
(
x
)
=
f
(
x
)
=
1
1
+
e
−
x
.
\text{sigmod}(x) = f(x) = \frac{1}{1 + e^{-x}}.
sigmod(x)=f(x)=1+e−x1.
2. 取值
分析:
(1)当
x
=
0
x=0
x=0 时,
f
(
x
)
=
f
(
0
)
=
1
1
+
e
−
0
=
1
2
.
f(x) = f(0) = \frac{1}{1+e^{-0}} = \frac{1}{2}.
f(x)=f(0)=1+e−01=21.
(2)当
x
→
+
∞
x \to +\infty
x→+∞ 时,
e
−
x
→
0
e^{-x} \to 0
e−x→0,由此
f
(
x
)
→
1
f(x) \to 1
f(x)→1。
(3)当
x
→
−
∞
x \to -\infty
x→−∞ 时,
e
−
x
→
+
∞
e^{-x} \to +\infty
e−x→+∞,由此
f
(
x
)
→
0
f(x) \to 0
f(x)→0。
由此,sigmod 函数的取值范围是 [ 0 , 1 ] [0, 1] [0,1],且单调递增。
3. 图像
用 Python 来展示它的部分图像:
实现的 Python 代码如下:
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x: np.ndarray) -> np.ndarray:
"""计算Sigmoid函数值"""
return 1 / (1 + np.exp(-x))
def plot_sigmoid_origin() -> None:
"""
绘制原点在(0,0)的Sigmoid函数图像
"""
# 生成数据
x = np.linspace(-10, 10, 400)
y = sigmoid(x)
# 创建画布和坐标轴
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
# 绘制曲线
ax.plot(x, y,
label=r'$\sigma(x) = \frac{1}{1 + e^{-x}}$',
color='#E63946', # 更现代的红色
linewidth=2.5,
zorder=3)
# 设置坐标轴范围
ax.set_xlim(-10, 10)
ax.set_ylim(-0.1, 1.1) # 保持上方留白
# 移动坐标轴到原点
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# 设置刻度参数
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='both', which='major', labelsize=10)
# 自定义刻度标签
ax.set_xticks(np.arange(-10, 11, 2))
ax.set_yticks([0, 0.5, 1.0])
ax.set_yticklabels(['0', r'$\frac{1}{2}$', '1']) # 分数显示
# 添加辅助网格
ax.grid(True,
linestyle=':',
color='gray',
alpha=0.6,
zorder=0)
# 标注关键点
ax.scatter(0, 0.5, color='#2A9D8F', s=80, zorder=4)
ax.annotate(r'$(0, \frac{1}{2})$',
xy=(0, 0.5),
xytext=(2, 0.6),
arrowprops=dict(arrowstyle='->', color='black'),
fontsize=12)
# 添加渐近线
ax.axhline(1, color='#264653', linestyle='--', alpha=0.5, zorder=1)
ax.axhline(0, color='#264653', linestyle='--', alpha=0.5, zorder=1)
# 设置标签和标题
ax.set_xlabel('x', fontsize=12, loc='right')
ax.set_ylabel(r'$\sigma(x)$', fontsize=12, loc='top', rotation=0)
ax.set_title('Sigmoid 函数',
pad=20,
fontsize=14,
color='#2A9D8F')
# 添加图例
ax.legend(loc='upper left', framealpha=0.95)
plt.tight_layout()
plt.show()
# 执行绘图
if __name__ == "__main__":
plot_sigmoid_origin()
4. 导数
f ′ ( x ) = ( 1 1 + e − x ) ′ = − 1 ( 1 + e − x ) 2 ( 1 + e − x ) ′ = − 1 ( 1 + e − x ) 2 e − x ( − x ) ′ = − 1 ( 1 + e − x ) 2 e − x ( − 1 ) = e − x ( 1 + e − x ) 2 = ( 1 − 1 ) + e − x ( 1 + e − x ) 2 = ( 1 + e − x ) − 1 ( 1 + e − x ) 2 = 1 1 + e − x − 1 ( 1 + e − x ) 2 = f ( x ) − f 2 ( x ) = f ( x ) ( 1 − f ( x ) ) . \begin{aligned} f'(x) &= \left(\frac{1}{1+e^{-x}} \right)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} \left(1+e^{-x} \right)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} e^{-x} (-x)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} e^{-x} (-1) \\ &= \frac{e^{-x}}{\left(1+e^{-x}\right)^2} = \frac{(1-1) + e^{-x}}{\left(1+e^{-x}\right)^2} = \frac{(1 + e^{-x}) -1}{\left(1+e^{-x}\right)^2} \\ &= \frac{1}{1 + e^{-x}} - \frac{1}{\left(1 + e^{-x}\right)^2} \\ &= f(x) - f^2(x) \\ &= f(x) \left(1-f(x) \right). \end{aligned} f′(x)=(1+e−x1)′=−(1+e−x)21(1+e−x)′=−(1+e−x)21e−x(−x)′=−(1+e−x)21e−x(−1)=(1+e−x)2e−x=(1+e−x)2(1−1)+e−x=(1+e−x)2(1+e−x)−1=1+e−x1−(1+e−x)21=f(x)−f2(x)=f(x)(1−f(x)).
即: f ′ ( x ) = f ( x ) ( 1 − f ( x ) ) f'(x) = f(x) \left(1-f(x) \right) f′(x)=f(x)(1−f(x))。