参考四种常见的激活函数的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')
图文格式修改参考
-
文字字体格式
python - Matplotlib 在使用 "Times New Roman"时将标题设置为粗体
较复杂的:Matplotlib 中英文及公式字体设置 -
图和标题相对位置调整
【python】如何将matplotlib的标题置于图片下方
(需要自己调整 y 的值)
plt.title('title', y=-0.2)
-
提高图片清晰度
参考Jupyter notebook 绘图时,如何生成高清图片?%config InlineBackend.figure_format = "svg"
最后保存成pdf,图片大小
plt.savefig('activation.pdf', bbox_inches='tight')