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
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
