python绘制激活函数

  • 代码
  • 示例

代码

我构建了一个关于激活函数的类,大家可以随意使用,包括其输出值和梯度值
关于这些激活函数详解可以参考我这篇博客:深度学习—激活函数详解(Sigmoid、tanh、ReLU、ReLU6及变体P-R-Leaky、ELU、SELU、Swish、Mish、Maxout、hard-sigmoid、hard-swish)

import matplotlib.pyplot as plt
import numpy as npclass ActivateFunc():def __init__(self, x, b=None, lamb=None, alpha=None, a=None):super(ActivateFunc, self).__init__()self.x = xself.b = bself.lamb = lambself.alpha = alphaself.a = adef Sigmoid(self):y = np.exp(self.x) / (np.exp(self.x) + 1)y_grad = y*(1-y)return [y, y_grad]def Tanh(self):y = np.tanh(self.x)y_grad = 1 - y * yreturn [y, y_grad]def Swish(self): #b是一个常数,指定by = self.x * (np.exp(self.b*self.x) / (np.exp(self.b*self.x) + 1))y_grad = np.exp(self.b*self.x)/(1+np.exp(self.b*self.x)) + self.x * (self.b*np.exp(self.b*self.x) / ((1+np.exp(self.b*self.x))*(1+np.exp(self.b*self.x))))return [y, y_grad]def ELU(self): # alpha是个常数,指定alphay = np.where(self.x > 0, self.x, self.alpha * (np.exp(self.x) - 1))y_grad = np.where(self.x > 0, 1, self.alpha * np.exp(self.x))return [y, y_grad]def SELU(self):  # lamb大于1,指定lamb和alphay = np.where(self.x > 0, self.lamb * self.x, self.lamb * self.alpha * (np.exp(self.x) - 1))y_grad = np.where(self.x > 0, self.lamb*1, self.lamb * self.alpha * np.exp(self.x))return [y, y_grad]def ReLU(self):y = np.where(self.x < 0, 0, self.x)y_grad = np.where(self.x < 0, 0, 1)return [y, y_grad]def PReLU(self):    # a大于1,指定ay = np.where(self.x < 0, self.x / self.a, self.x)y_grad = np.where(self.x < 0, 1 / self.a, 1)return [y, y_grad]def LeakyReLU(self):   # a大于1,指定ay = np.where(self.x < 0, self.x / self.a, self.x)y_grad = np.where(self.x < 0, 1 / self.a, 1)return [y, y_grad]def Mish(self):f = 1 + np.exp(x)y = self.x * ((f*f-1) / (f*f+1))y_grad = (f*f-1) / (f*f+1) + self.x*(4*f*(f-1)) / ((f*f+1)*(f*f+1))return [y, y_grad]def ReLU6(self):y = np.where(np.where(self.x < 0, 0, self.x) > 6, 6, np.where(self.x < 0, 0, self.x))y_grad = np.where(self.x > 6, 0, np.where(self.x < 0, 0, 1))return [y, y_grad]def Hard_Swish(self):f = self.x + 3relu6 = np.where(np.where(f < 0, 0, f) > 6, 6, np.where(f < 0, 0, f))relu6_grad = np.where(f > 6, 0, np.where(f < 0, 0, 1))y = self.x * relu6 / 6y_grad = relu6 / 6 + self.x * relu6_grad / 6return [y, y_grad]def Hard_Sigmoid(self):f = (2 * self.x + 5) / 10y = np.where(np.where(f > 1, 1, f) < 0, 0, np.where(f > 1, 1, f))y_grad = np.where(f > 0, np.where(f >= 1, 0, 1 / 5), 0)return [y, y_grad]def PlotActiFunc(x, y, title):plt.grid(which='minor', alpha=0.2)plt.grid(which='major', alpha=0.5)plt.plot(x, y)plt.title(title)plt.show()def PlotMultiFunc(x, y):plt.grid(which='minor', alpha=0.2)plt.grid(which='major', alpha=0.5)plt.plot(x, y)if __name__ == '__main__':x = np.arange(-10, 10, 0.01)activateFunc = ActivateFunc(x)activateFunc.b = 1PlotActiFunc(x, activateFunc.Sigmoid()[0], title='Sigmoid')PlotActiFunc(x, activateFunc.Swish()[0], title='Swish')PlotActiFunc(x, activateFunc.ReLU()[0], title='ReLU')PlotActiFunc(x, activateFunc.Mish()[0], title='Mish')PlotActiFunc(x, activateFunc.Mish()[1], title='Mish-grad')PlotActiFunc(x, activateFunc.Swish()[1], title='Swish-grad')plt.figure(1)PlotMultiFunc(x, activateFunc.Mish()[1])PlotMultiFunc(x, activateFunc.Swish()[1])plt.legend(['Mish-grad', 'Swish-grad'])plt.figure(2)PlotMultiFunc(x, activateFunc.Swish()[0])PlotMultiFunc(x, activateFunc.Mish()[0])plt.legend(['Swish', 'Mish'])plt.figure(3)PlotMultiFunc(x, activateFunc.Swish()[0])PlotMultiFunc(x, activateFunc.Hard_Swish()[0])plt.legend(['Swish', 'Hard-Swish'])plt.figure(4)PlotMultiFunc(x, activateFunc.Sigmoid()[0])PlotMultiFunc(x, activateFunc.Hard_Sigmoid()[0])plt.legend(['Sigmoid', 'Hard-Sigmoid'])plt.figure(5)PlotMultiFunc(x, activateFunc.ReLU()[0])PlotMultiFunc(x, activateFunc.ReLU6()[0])plt.legend(['ReLU', 'ReLU6'])plt.figure(6)PlotMultiFunc(x, activateFunc.Swish()[1])PlotMultiFunc(x, activateFunc.Hard_Swish()[1])plt.legend(['Swish-grad', 'Hard-Swish-grad'])plt.figure(7)PlotMultiFunc(x, activateFunc.Sigmoid()[1])PlotMultiFunc(x, activateFunc.Hard_Sigmoid()[1])plt.legend(['Sigmoid-grad', 'Hard-Sigmoid-grad'])plt.figure(8)PlotMultiFunc(x, activateFunc.ReLU()[1])PlotMultiFunc(x, activateFunc.ReLU6()[1])plt.legend(['ReLU-grad', 'ReLU6-grad'])plt.show()

示例




2019.10.30
希望能帮到你。


最全面:python绘制Sigmoid、Tanh、Swish、ELU、SELU、ReLU、ReLU6、Leaky ReLU、Mish、hard-Sigmoid、hard-Swish等激活函数(有源码)相关推荐

  1. 深度学习激活函数总结(sigmoid,tanh,ReLU,Leaky ReLU,EReLU,PReLU,Softmax,Swish,Maxout,Softplus)

    摘要 本文总结了深度学习领域最常见的10中激活函数(sigmoid.Tanh.ReLU.Leaky ReLU.ELU.PReLU.Softmax.Swith.Maxout.Softplus)及其优缺点 ...

  2. 激活函数(sigmoid、Tanh、ReLU、Leaky ReLU、ELU、Maxout)

    sigmoid函数 公式: 图像: sigmoid可以将数据压缩到[0,1]范围内,可看作神经元的饱和放电率.在历史上,sigmoid函数非常有用,这是因为它对神经元的激活频率有很好的解释:从完全不激 ...

  3. 常用激活函数:Sigmoid、Tanh、Relu、Leaky Relu、ELU优缺点总结

    1.激活函数的作用 什么是激活函数? 在神经网络中,输入经过权值加权计算并求和之后,需要经过一个函数的作用,这个函数就是激活函数(Activation Function). 激活函数的作用? 首先我们 ...

  4. 激活函数 sigmoid、tanh、ReLu、Leaky ReLu、ELU、Maxout

    1. sigmoid sigmoid 是逻辑函数,常见的 sigmoid 函数定义为: S(x)=11+e−xS(x)=\frac{1}{1+e^{-x}}S(x)=1+e−x1​ dS(x)dx=e ...

  5. 【Deep Learning 三】神经网络中的非线性激活函数之间的优缺点:sigmoid、tanh、ReLu、Leaky ReLu...

    1 什么是激活函数? 激活函数,并不是去激活什么,而是指如何把"激活的神经元的特征"通过函数把特征保留并映射出来(保留特征,去除一些数据中是的冗余),这是神经网络能解决非线性问题关 ...

  6. 激活函数(sigmoid、tanh、ReLU、leaky ReLU)

    为了保证神经元的计算包含简洁性和功能性,神经元的计算包括线性计算和非线性计算. 今天主要讲集中非线性计算(即激活函数),包括: sigmoid tanh ReLU leaky ReLU 1.sigmo ...

  7. PyTorch | 激活函数(Sigmoid、Tanh、ReLU和Leaky ReLU)

    PyTorch | 激活函数(Sigmoid.Tanh.ReLU) 1. 简介 2. 函数饱和性 3. 以零为中心 3.1 收敛速度 3.2 参数更新 3.3 更新方向 3.4 以零为中心的影响 4. ...

  8. 激活函数ReLU、Leaky ReLU、tanh(双曲正切函数Hyperbolic tangent function)

    深度学习的激活函数 :sigmoid.tanh.ReLU .Leaky Relu.RReLU.softsign .softplus - 程序员大本营 https://www.pianshen.com/ ...

  9. 激活函数变种(Sigmoid、Hard-Sigmoid、Tanh、ReLU、Leaky ReLU、ELU、SELU、ReLU6、Swish、Hard-Swish、Mish)

    激活函数的作用:提供网络的非线性表达建模能力. 线性可分数据:可以通过机器学习(感知机.SVM)找到的线性方程来进行划分. 非线性可分数据:找不到一种线性方程来划分数据,此时需要引入非线性函数. 什么 ...

  10. 常用激活函数activation function(Softmax、Sigmoid、Tanh、ReLU和Leaky ReLU) 附激活函数图像绘制python代码

    激活函数是确定神经网络输出的数学方程式. 激活函数的作用:给神经元引入了非线性因素,使得神经网络可以任意逼近任何非线性函数. 1.附加到网络中的每个神经元,并根据每个神经元的输入来确定是否应激活. 2 ...

最新文章

  1. Linux —— 目录(文件夹)及文件相关处理指令
  2. java 数据库数据脱敏_Sharding-JDBC-数据脱敏
  3. Android ListView存在多个item样式的处理方法
  4. java怎么把system.out的东西输出到文件上
  5. 布尔盲注怎么用,一看你就明白了。布尔盲注原理+步骤+实战教程
  6. JVM实战总结:一个多线程中对象引用的问题
  7. 如何用 Python 爬取网易云音乐的 10w+ 评论?附详细代码解读
  8. dp线和hdmi区别_各类视频线有什么区别?应该怎么选呢?
  9. 如何从JavaScript对象中删除键? [重复]
  10. 开启win7笔记本自带无线功能
  11. PJzhang:360压缩的用户许可协议和隐私政策阅读
  12. 全球和国产十大AI芯片
  13. navicat使用手册
  14. html form提交heard,德普前妻Amber Heard戛纳合辑
  15. Windows 10 让所有程序默认为“以管理员身份运行”并且取消“确认”按钮
  16. 用python扑克随机发牌_Python小应用之发扑克牌
  17. android 自动加微信群,如何完美实现微信自动发朋友圈自动添加好友等等
  18. 1248 - Every derived table must have its own alias
  19. 计算机修改wif教程,电脑修改wifi密码步骤
  20. JavaSE-IO流

热门文章

  1. Squid之正向代理反向代理
  2. 趣味算法-求波峰波谷最大值
  3. Xshell-7工具栏隐藏了
  4. linux 内核态 cos函数,浅析μCOS/II v2.85内核OSMboxPend()和OSMboxPost()函数工作原理
  5. 软件项目管理 7.5.项目进度模型(SPSP)
  6. 图像传感器c语言,[转载]图像传感器的主要特性。
  7. 基于Zigbee的智能路灯控制系统的Qt操作界面
  8. 计算机专用安全机箱,电脑数据信息安全机箱,PC防盗安全机箱
  9. java毕业生设计学校食堂管理计算机源码+系统+mysql+调试部署+lw
  10. php 顺时针打印矩阵,这题