使用python中的matplotlib绘画激活函数图像
import matplotlib.pyplot as plt
import numpy as np
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()
绘画结果如图所示: