使用python中的matplotlib绘画激活函数图像

使用python中的matplotlib绘画激活函数图像

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示汉字

def sigmoid(x):
    return 1. / (1. + np.exp(-x))

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

def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))


def han_plot():
    x = np.linspace(-10, 10, 10000)
    y = sigmoid(x)
    y1 = relu(x)
    y2 = tanh(x)
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.set(title="Activate the function", xlabel="x", ylabel="y=f(x)")
	# 设置线宽和颜色
    ax.plot(x, y,linewidth=3.0,color="red")
    ax.plot(x, y1, linewidth=3.0, color="blue")
    ax.plot(x, y2, linewidth=3.0, color="green")
    plt.legend(['sigmoid','relu','tanh'])
	# 保存图片
    plt.savefig("./hanshu.png",format="png")
	# 显示
    plt.show()

if __name__ == "__main__":
   han_plot()

绘画结果如图所示:

使用python中的matplotlib绘画激活函数图像_第1张图片

你可能感兴趣的:(笔记,python)