Sigmoid、Tanh、ReLu、Leaky ReLu激活函数的Python绘制 及图文格式

参考四种常见的激活函数的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. 文字字体格式

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

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

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

    (需要自己调整 y 的值)

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

    %config InlineBackend.figure_format = "svg"
    

    最后保存成pdf,图片大小

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

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值