神经网络中的激活函数图像绘制

在深度学习中,常用的激活函数主要有:Sigmoid函数,Tanh函数,ReLU函数等。


图像绘制效果:

图像绘制代码下载链接:激活函数绘制.py


Sigmoid函数

该函数是将取值为 (−∞,+∞) 的数映射到 (0,1) 之间;
Sigmoid函数作为非线性激活函数,但是其并不被经常使用,缺点是当 z 值非常大或者非常小时,通过上图我们可以看到,sigmoid函数的导数 g′(z) 将接近 0 。这会导致权重 W 的梯度将接近 0 ,使得梯度更新十分缓慢,即梯度消失。

def sigmoid(x):result = 1 / (1 + math.e ** (-x))return result

Tanh函数

Tanh函数相较于sigmoid函数要常见一些,该函数是将取值为 (−∞,+∞) 的数映射到 (−1,1) 之间。t该函数在 0 附近很短一段区域内可看做线性的。由于tanh函数均值为 0 ,因此弥补了Sigmoid函数均值为 0.5 的缺点。Tanh函数的缺点同sigmoid函数的第一个缺点一样,当 z 很大或很小时,g′(z) 接近于 0 ,会导致梯度很小,权重更新非常缓慢,即梯度消失问题。

def tanh(x):# result = np.exp(x)-np.exp(-x)/np.exp(x)+np.exp(-x)result = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))return result

ReLU函数

ReLU函数又称为修正线性单元(Rectified Linear Unit),是一种分段线性函数,其弥补了sigmoid函数以及tanh函数的梯度消失问题。ReLU函数的优点:在输入为正数的时候(对于大多数输入 z 空间来说),不存在梯度消失问题。ReLU函数只有线性关系,不管是前向传播还是反向传播,都比sigmod和tanh要快。(sigmod和tanh要计算指数,计算速度会比较慢)ReLU函数的缺点:当输入为负时,梯度为0,会产生梯度消失问题。

def relu(x):result = np.maximum(0, x)return result

Leaky ReLU函数

Leaky ReLU函数是一种对ReLU函数改进的函数,又称为PReLU函数,但其并不常用。

def leaky(x):a = x[x > 0]b = 0.1 * x[x < 0]result = np.concatenate((b, a), axis=0)return result

完整代码

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['axes.unicode_minus'] = Falsedef sigmoid(x):result = 1 / (1 + math.e ** (-x))return resultdef tanh(x):# result = np.exp(x)-np.exp(-x)/np.exp(x)+np.exp(-x)result = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))return resultdef relu(x):result = np.maximum(0, x)return resultdef elu(x, alpha=1):a = x[x > 0]b = alpha * (math.e ** (x[x < 0]) - 1)result = np.concatenate((b, a), axis=0)return resultdef leaky(x):a = x[x > 0]b = 0.1 * x[x < 0]result = np.concatenate((b, a), axis=0)return resultyanse = 'teal'
# ===========================================
fig = plt.figure(figsize=(10, 8))
plt.rcParams['font.size'] = 11
plt.rc('font',family='Times New Roman')
ax = fig.add_subplot(221)
x = np.linspace(-10, 10)
y = sigmoid(x)ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])plt.plot(x, y, label="Sigmoid", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)# ============================================
ax = fig.add_subplot(222)
x = np.linspace(-10, 10)
y = tanh(x)ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])plt.plot(x, y, label="Tanh", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)# ============================================
ax = fig.add_subplot(223)
x = np.linspace(-10, 10)
y = relu(x)ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([5, 10])plt.plot(x, y, label="ReLU", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)# ============================================
# ax = fig.add_subplot(234)
# x = np.linspace(-10, 10)
# y = elu(x)
#
# ax.spines['top'].set_color('none')
# ax.spines['right'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')
# ax.spines['bottom'].set_position(('data', 0))
# ax.set_xticks([-10, -5, 0, 5, 10])
# ax.yaxis.set_ticks_position('left')
# ax.spines['left'].set_position(('data', 0))
# ax.set_yticks([5, 10])
#
# plt.plot(x, y, label="ELU alpha=1", linestyle='-', color='red')
# plt.legend()# ============================================
ax = fig.add_subplot(224)
x = np.linspace(-10, 10)
y = leaky(x)ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([5, 10])plt.plot(x, y, label="Leaky Relu", linestyle='-', color=str(yanse))
plt.legend(loc='upper left', frameon=False, ncol=1)plt.savefig('../' + str("神经网络中的激活函数图像绘制"), bbox_inches='tight', dpi=500)
plt.show()

END

基于Python实现神经网络中的激活函数图像绘制相关推荐

  1. 神经网络中常用激活函数图像绘制(Python)

    #relu激活函数 from matplotlib import pyplot import numpy as np def relu(x):if x > 0:return xelse:retu ...

  2. 深度学习:神经网络中的激活函数

    http://blog.csdn.net/pipisorry/article/details/71157037 激活函数 神经网络神经元中,输入的 inputs 通过加权,求和后,还被作用了一个函数, ...

  3. 神经网络中的激活函数与损失函数深入理解推导softmax交叉熵

    神经网络中的激活函数与损失函数&深入理解softmax交叉熵 前面在深度学习入门笔记1和深度学习入门笔记2中已经介绍了激活函数和损失函数,这里做一些补充,主要是介绍softmax交叉熵损失函数 ...

  4. Python Imaging Library:ImageDraw Module(图像绘制模块)

    Python Imaging Library:ImageDraw Module(图像绘制模块) # 图像绘制库 Mode_NewImg = "RGB" Width_NewImg = ...

  5. 神经网络激活函数对数函数_神经网络中的激活函数

    神经网络激活函数对数函数 Activation function, as the name suggests, decides whether a neuron should be activated ...

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

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

  7. 深度学习与计算机视觉:基于Python的神经网络的实现

    在前面两篇文章介绍了深度学习的一些基本概念,本文则使用Python实现一个简单的深度神经网络,并使用MNIST数据库进行测试. 神经网络的实现,包括以下内容: 神经网络权值的初始化 正向传播 误差评估 ...

  8. 【论文笔记】DSCN:基于深度孪生神经网络的光学航空图像变化检测模型

    本文是论文<Change Detection Based on Deep Siamese Convolutional Network for Optical Aerial Images>的 ...

  9. 从sigmoid到GELU——神经网络中的激活函数

    目录 一.激活函数及其作用 二.激活函数的分类 1.sigmoid 2.tanh激活函数 3.RELU函数--Rectified Linear Unit--整流线性单元 4.Leaky ReLU 5. ...

最新文章

  1. OpenCV-Python:K值聚类
  2. 基于DVB-T标准,COFDM调制系统的利用导频信号进行符号粗同步
  3. Boost:双图bimap与range范围的测试程序
  4. RequestMapping注解的作用
  5. 【Java类加载机制】深入类加载器(二)自定义加密、解密类加载器
  6. C++之String的find方法,查找一个字符串在另一个字符串的什么位置;leveldb字符串转数字解析办法...
  7. 纽约出租车计费问题:数据清洗与初探
  8. 论文笔记_S2D.71_2020_CVPR_用于自监督单目深度估计的3D packing
  9. 玩转MP4视频格式制作转换秘籍
  10. 关于机器人创业:学术界vs工业界及中国机器人企业的机会
  11. rsync 同步文件
  12. 网络与信息安全-第三章-对称秘钥加密算法
  13. 该怎么设置macOS 的开机启动项
  14. Filebeat+Kafka+Logstash+Elasticsearch+Kibana 构建日志分析系统
  15. 图说全球浏览器市场份额变迁史
  16. Tomcat配置数据库连接池
  17. 多个div水平横向排列
  18. c++实现strstr函数
  19. iOS: Motion Event
  20. Penalty-Based Multibody Animation(1)

热门文章

  1. 戴尔服务器开机无显示器,戴尔液晶显示器开机无显示原因是电容问题?
  2. python 爬虫 微博 github_GitHub 热门:各大网站的 Python 爬虫登录汇总
  3. xenserver内核的linux版本,XenServer7.0创建本地ISO库
  4. 因果推断系列18-断点回归设计(Regression Discontinuity Design,RDD)
  5. 客似云来_如何通过将本地文件卸载到云来节省驱动器空间
  6. Steam和Epic连接Nas搭建的方舟进化ARK专用服务器
  7. 业务:财务会计业务知识
  8. 2022 hgame creakme
  9. PerformanceTest, monitoring command
  10. 编译mumps库时无法链接mpi库中的函数