激活函数及python实现

1 threshold激活函数

f ( x ) = { 1 , x ≥ 0 0 , x &lt; 0 f\left(x\right)=\begin{cases}1,&amp;\text {$x\geq0$}\\0,&amp;\text{x &lt; 0}\end{cases} f(x)={1,0,x0x < 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")

在这里插入图片描述

图1 Threshold激活函数

2 sigmoid激活函数

f ( x ) = 1 1 + e − x f\left(x\right)=\frac{1}{1+e^{-x}} f(x)=1+ex1

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")

在这里插入图片描述

图2 sigmoid激活函数

3 Relu激活函数

f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 0 , x &lt; 0 f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&amp;\text{$x\geq0$}\\0,&amp;\text{x &lt; 0} \end{cases} f(x)=max(0,x)={x,0,x0x < 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")

在这里插入图片描述

图3 Relu激活函数

4 PRelu激活函数

f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 α x , x &lt; 0  f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&amp;\text{$x\geq0$}\\\alpha x,&amp;\text{x &lt; 0 }\end{cases} f(x)=max(0,x)={x,αx,x0x < 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")

在这里插入图片描述

图4 PRelu激活函数

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)=tanh(x)=ex+exexex

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")

在这里插入图片描述

图5 双正切激活函数

【参考文献】
[1]https://blog.csdn.net/Xin_101/article/details/81844355
[2]https://matplotlib.org/users/mathtext.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值