import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
def tanh(x):
return np.tanh(x)
def tanh_derivative(x):
return 1 - np.square(np.tanh(x))
def relu(x):
return np.maximum(0, x)
def relu_derivative(x):
return np.where(x > 0, 1, 0)
# 生成输入数据
x = np.linspace(-5, 5, 100)
# 设置画布和子图
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# 绘制Sigmoid函数及其导数
axes[0].plot(x, sigmoid(x), label='Sigmoid')
axes[0].plot(x, sigmoid_derivative(x), label='Sigmoid Derivative')
axes[0].legend(loc='best')
axes[0].set_title('Sigmoid Function')
# 绘制Tanh函数及其导数
axes[1].plot(x, tanh(x), label='Tanh', color='g')
axes[1].plot(x, tanh_derivative(x), label='Tanh Derivative', color='r')
axes[1].legend(loc='best')
axes[1].set_title('Tanh Function')
# 绘制ReLU函数及其导数
axes[2].plot(x, relu(x), label='ReLU', color='m')
axes[2].plot(x, relu_derivative(x), label='ReLU Derivative', color='c')
axes[2].legend(loc='best')
axes[2].set_title('ReLU Function')
# 设置子图共同的坐标轴标签
fig.text(0.5, 0.04, 'Input', ha='center', va='center')
fig.text(0.06, 0.5, 'Output', ha='center', va='center', rotation='vertical')
# 绘制十字坐标轴
for ax in axes:
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()
激活函数画图1
最新推荐文章于 2026-06-19 17:00:49 发布
2万+

被折叠的 条评论
为什么被折叠?



