1 threshold激活函数
f ( x ) = { 1 , x ≥ 0 0 , x < 0 f\left(x\right)=\begin{cases}1,&\text {$x\geq0$}\\0,&\text{x < 0}\end{cases} f ( x ) = { 1 , 0 , x ≥ 0 x < 0
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
)
)
ax
=
axisartist
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
# 隐藏坐标轴
ax
.
axis
[
:
]
.
set_visible
(
False
)
# 添加坐标轴
ax
.
axis
[
'x'
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
'y'
]
=
ax
.
new_floating_axis
(
1
,
0
)
# x轴添加箭头
ax
.
axis
[
'x'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
ax
.
axis
[
'y'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
# 设置坐标轴刻度显示方向
ax
.
axis
[
'x'
]
.
set_axis_direction
(
'top'
)
ax
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
plt
.
ylim
(
-
0.2
,
1.25
)
x_1
=
np
.
arange
(
0
,
10
,
0.1
)
y_1
=
x_1
-
x_1
+
1
x_axis
=
np
.
arange
(
-
10
,
10
,
0.2
)
y_axis
=
np
.
arange
(
-
0
,
1
,
0.2
)
plt
.
plot
(
x_1
,
y_1
,
'r'
,
label
=
r
'threshold=$\{\stackrel{1, x>=0}{0, x<0}$'
)
plt
.
legend
(
)
x_2
=
np
.
arange
(
-
5
,
0
,
0.1
)
y_2
=
x_2
-
x_2
plt
.
plot
(
x_2
,
y_2
,
'r'
,
label
=
'threshold'
)
plt
.
scatter
(
0
,
1
,
color
=
'r'
)
# 绘制圆圈:color设置为空
plt
.
scatter
(
0
,
0
,
marker
=
'o'
,
color
=
''
,
edgecolors
=
'r'
)
plt
.
savefig
(
"./image_test/threshold.png"
,
format
=
"png"
)
2 sigmoid激活函数
f ( x ) = 1 1 + e − x f\left(x\right)=\frac{1}{1+e^{-x}} f ( x ) = 1 + e − x 1
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
)
)
ax
=
axisartist
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
# 隐藏坐标轴
ax
.
axis
[
:
]
.
set_visible
(
False
)
# 添加坐标轴
ax
.
axis
[
'x'
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
'y'
]
=
ax
.
new_floating_axis
(
1
,
0
)
# x轴添加箭头
ax
.
axis
[
'x'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
ax
.
axis
[
'y'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
# 设置坐标轴刻度显示方向
ax
.
axis
[
'x'
]
.
set_axis_direction
(
'top'
)
ax
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
plt
.
xlim
(
-
10
,
10
)
plt
.
ylim
(
-
0.1
,
1.2
)
# x轴添加箭头
x
=
np
.
arange
(
-
10
,
10
,
0.1
)
y
=
1
/
(
1
+
np
.
exp
(
-
x
)
)
plt
.
plot
(
x
,
y
,
label
=
r
"$sigmoid=\frac{1}{1+e^{-x}}$"
,
c
=
'r'
)
plt
.
legend
(
)
plt
.
savefig
(
"./image_test/sigmoid.png"
,
format
=
"png"
)
3 Relu激活函数
f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 0 , x < 0 f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&\text{$x\geq0$}\\0,&\text{x < 0} \end{cases} f ( x ) = m a x ( 0 , x ) = { x , 0 , x ≥ 0 x < 0
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
)
)
ax
=
axisartist
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
# 隐藏坐标轴
ax
.
axis
[
:
]
.
set_visible
(
False
)
# 添加坐标轴
ax
.
axis
[
'x'
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
'y'
]
=
ax
.
new_floating_axis
(
1
,
0
)
# x轴添加箭头
ax
.
axis
[
'x'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
ax
.
axis
[
'y'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
# 设置坐标轴刻度显示方向
ax
.
axis
[
'x'
]
.
set_axis_direction
(
'top'
)
ax
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
plt
.
xlim
(
-
10
,
10
)
plt
.
ylim
(
-
0.1
,
10
)
x_1
=
np
.
arange
(
0
,
10
,
0.1
)
y_1
=
x_1
x_axis
=
np
.
arange
(
-
10
,
10
,
0.2
)
y_axis
=
np
.
arange
(
-
0
,
1
,
0.2
)
plt
.
plot
(
x_1
,
y_1
,
'r-'
,
label
=
r
'Relu=$\{\stackrel{1, x>=0}{0, x<0}$'
)
x_2
=
np
.
arange
(
-
5
,
0
,
0.1
)
y_2
=
x_2
-
x_2
plt
.
plot
(
x_2
,
y_2
,
'r-'
)
plt
.
legend
(
)
plt
.
savefig
(
"./image_test/relu.png"
,
format
=
"png"
)
4 PRelu激活函数
f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 α x , x < 0 f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&\text{$x\geq0$}\\\alpha x,&\text{x < 0 }\end{cases} f ( x ) = m a x ( 0 , x ) = { x , α x , x ≥ 0 x < 0
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
)
)
ax
=
axisartist
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
# 隐藏坐标轴
ax
.
axis
[
:
]
.
set_visible
(
False
)
# 添加坐标轴
ax
.
axis
[
'x'
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
'y'
]
=
ax
.
new_floating_axis
(
1
,
0
)
# x轴添加箭头
ax
.
axis
[
'x'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
ax
.
axis
[
'y'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
# 设置坐标轴刻度显示方向
ax
.
axis
[
'x'
]
.
set_axis_direction
(
'top'
)
ax
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
plt
.
xlim
(
-
10
,
10
)
plt
.
ylim
(
-
10
,
10
)
x_1
=
np
.
arange
(
0
,
10
,
0.1
)
y_1
=
x_1
x_axis
=
np
.
arange
(
-
10
,
10
,
0.2
)
y_axis
=
np
.
arange
(
-
0
,
1
,
0.2
)
plt
.
plot
(
x_1
,
y_1
,
'r-'
,
label
=
r
'PRelu=$\{\stackrel{x,x>=0}{\alpha x, x<0}$'
)
x_2
=
np
.
arange
(
-
10
,
0
,
0.1
)
y_2
=
0.3
*
x_2
plt
.
plot
(
x_2
,
y_2
,
'r-'
)
plt
.
legend
(
)
plt
.
savefig
(
"./image_test/PRelu.png"
,
format
=
"png"
)
5 tanh激活函数
f ( x ) = t a n h ( x ) = e x − e − x e x + e − x f\left(x\right)=tanh\left(x\right)=\frac{e^x-e^{-x}}{e^x+e^{-x}} f ( x ) = t a n h ( x ) = e x + e − x e x − e − x
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
)
)
ax
=
axisartist
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
# 隐藏坐标轴
ax
.
axis
[
:
]
.
set_visible
(
False
)
# 添加坐标轴
ax
.
axis
[
'x'
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
'y'
]
=
ax
.
new_floating_axis
(
1
,
0
)
# x轴添加箭头
ax
.
axis
[
'x'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
ax
.
axis
[
'y'
]
.
set_axisline_style
(
'-|>'
,
size
=
1.0
)
# 设置坐标轴刻度显示方向
ax
.
axis
[
'x'
]
.
set_axis_direction
(
'top'
)
ax
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
plt
.
xlim
(
-
10
,
10
)
plt
.
ylim
(
-
1
,
1
)
x
=
np
.
arange
(
-
10
,
10
,
0.1
)
y
=
(
np
.
exp
(
x
)
-
np
.
exp
(
-
x
)
)
/
(
np
.
exp
(
x
)
+
np
.
exp
(
-
x
)
)
plt
.
plot
(
x
,
y
,
'r'
,
label
=
r
"tanh=$\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}$"
)
plt
.
legend
(
)
plt
.
savefig
(
"./image_test/tanh.png"
,
format
=
"png"
)
【参考文献】
[1]https://blog.csdn.net/Xin_101/article/details/81844355
[2]https://matplotlib.org/users/mathtext.html