当前位置:   article > 正文

Sigmoid、Tanh、ReLu、Leaky ReLu激活函数的Python绘制 及图文格式_图像生成tanh激活函数

图像生成tanh激活函数

参考四种常见的激活函数的Python绘制

Sigmoid、Tanh、ReLu、Leaky ReLu 激活函数的变化曲线,在参考的基础上改了一些格式细节,完整代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 11 20:41:57 2018

@author: brucelau

CY edit on May 6 

"""

import matplotlib.pyplot as plt
import numpy as np

#  输出图像为SVG格式,比默认的png格式清晰,最后截图或许再加代码保存SVG图片
%config InlineBackend.figure_format = 'svg'

x = np.linspace(-10,10)
y_sigmoid = 1/(1+np.exp(-x))
y_tanh = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))

fig = plt.figure()
# plot sigmoid
ax = fig.add_subplot(221)
ax.plot(x,y_sigmoid)
ax.grid()
ax.set_title('a) Sigmoid',fontname="Times New Roman", y=-0.5)

# plot tanh
ax = fig.add_subplot(222)
ax.plot(x,y_tanh)
ax.grid()
ax.set_title('b) Tanh',fontname="Times New Roman", y=-0.5)

# plot relu
ax = fig.add_subplot(223)
y_relu = np.array([0*item  if item<0 else item for item in x ]) 
ax.plot(x,y_relu)
ax.grid()
ax.set_title('c) ReLu',fontname="Times New Roman", y=-0.5)

#plot leaky relu
ax = fig.add_subplot(224)
y_relu = np.array([0.2*item  if item<0 else item for item in x ]) 
ax.plot(x,y_relu)
ax.grid()
ax.set_title('d) Leaky ReLu',fontname="Times New Roman", y=-0.5)

plt.tight_layout()
# 保存为pdf文件到当前路径
plt.savefig('activation.pdf', bbox_inches='tight')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

图文格式修改参考

  1. 文字字体格式

    python - Matplotlib 在使用 "Times New Roman"时将标题设置为粗体
    较复杂的:Matplotlib 中英文及公式字体设置

  2. 图和标题相对位置调整

    【python】如何将matplotlib的标题置于图片下方

    (需要自己调整 y 的值)

    plt.title('title', y=-0.2)
    
    • 1
  3. 提高图片清晰度
    参考Jupyter notebook 绘图时,如何生成高清图片?

    %config InlineBackend.figure_format = "svg"
    
    • 1

    最后保存成pdf,图片大小

    plt.savefig('activation.pdf', bbox_inches='tight')
    
    • 1

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/333597
推荐阅读
相关标签
  

闽ICP备14008679号