图效果
import numpy as np
import matplotlib.pyplot as plt# 定义函数
def sigmoid(x):return 1 / (1 + np.exp(-x))def tanh(x):return np.tanh(x)def relu(x):return np.maximum(0, x)def leaky_relu(x, alpha=0.01):return np.where(x > 0, x, alpha*x)# 生成数据
x = np.linspace(-5, 5, 1000)
sigmoid_y = sigmoid(x)
tanh_y = tanh(x)
relu_y = relu(x)
leaky_relu_y = leaky_relu(x)# 绘图
plt.figure(figsize=(10, 6))# Sigmoid
plt.subplot(221)
plt.plot(x, sigmoid_y, label='Sigmoid', color='blue')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.legend()
plt.title('Sigmoid')# Tanh
plt.subplot(222)
plt.plot(x, tanh_y, label='Tanh', color='red')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.legend()
plt.title('Tanh')# ReLU
plt.subplot(223)
plt.plot(x, relu_y, label='ReLU', color='green')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.legend()
plt.title('ReLU')# Leaky ReLU
plt.subplot(224)
plt.plot(x, leaky_relu_y, label='Leaky ReLU', color='purple')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.legend()
plt.title('Leaky ReLU')plt.tight_layout()
plt.show()