部分激活函数可视化

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)

def elu(x, alpha=1.0):
    return np.where(x > 0, x, alpha * (np.exp(x) - 1))

def softmax(x):
    exp_x = np.exp(x - np.max(x))  # 防止数值溢出
    return exp_x / np.sum(exp_x)

def swish(x):
    return x * sigmoid(x)

def gelu(x):
    return 0.5 * x * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * x**3)))

def softplus(x):
    return np.log(1 + np.exp(x))

# 定义输入范围
x = np.linspace(-10, 10, 1000)

# 计算各激活函数的输出
y_sigmoid = sigmoid(x)
y_tanh = tanh(x)
y_relu = relu(x)
y_leaky_relu = leaky_relu(x)
y_elu = elu(x)
y_swish = swish(x)
y_gelu = gelu(x)
y_softplus = softplus(x)

# 绘制图像
plt.figure(figsize=(12, 8))

plt.subplot(3, 3, 1)
plt.plot(x, y_sigmoid, label="Sigmoid", color="blue")
plt.title("Sigmoid")
plt.grid()

plt.subplot(3, 3, 2)
plt.plot(x, y_tanh, label="Tanh", color="orange")
plt.title("Tanh")
plt.grid()

plt.subplot(3, 3, 3)
plt.plot(x, y_relu, label="ReLU", color="green")
plt.title("ReLU")
plt.grid()

plt.subplot(3, 3, 4)
plt.plot(x, y_leaky_relu, label="Leaky ReLU", color="red")
plt.title("Leaky ReLU")
plt.grid()

plt.subplot(3, 3, 5)
plt.plot(x, y_elu, label="ELU", color="purple")
plt.title("ELU")
plt.grid()

plt.subplot(3, 3, 6)
plt.plot(x, y_swish, label="Swish", color="brown")
plt.title("Swish")
plt.grid()

plt.subplot(3, 3, 7)
plt.plot(x, y_gelu, label="GELU", color="pink")
plt.title("GELU")
plt.grid()

plt.subplot(3, 3, 8)
plt.plot(x, y_softplus, label="Softplus", color="gray")
plt.title("Softplus")
plt.grid()

plt.tight_layout()
plt.show()

部分激活函数可视化_第1张图片

你可能感兴趣的:(python,机器学习,开发语言)