Python作图

图效果

python 复制代码
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()
相关推荐
没有梦想的咸鱼185-1037-166313 分钟前
基于R语言机器学习方法在生态经济学领域中的实践技术应用
开发语言·机器学习·数据分析·r语言
2401_8288906416 分钟前
使用 BERT 实现意图理解和实体识别
人工智能·python·自然语言处理·bert·transformer
向上的车轮38 分钟前
基于go语言的云原生TodoList Demo 项目,验证云原生核心特性
开发语言·云原生·golang
The Chosen One98539 分钟前
C++ : AVL树-详解
开发语言·c++
PH_modest1 小时前
【Qt跬步积累】—— 初识Qt
开发语言·qt
怀旧,1 小时前
【C++】18. 红⿊树实现
开发语言·c++
多恩Stone1 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
xiaopengbc2 小时前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@2 小时前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫