python绘制同时包含Sigmoid、Tanh和Relu函数的图像

博客展示了Python相关代码及效果图,聚焦于Python编码方面的内容,为信息技术领域中Python编程相关呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

import math 
import numpy as np
import matplotlib.pyplot as plt

# set x's range
x = np.arange(-10,10,0.1)

y1=1/(1+math.e**(-x)) # sigmoid
#y2=math.e**(-x)/((1+math.e**(-x))**2)
y2=(math.e**(x)-math.e**(-x))/(math.e**(x)+math.e**(-x)) # tanh
y3=np.where(x<0,0,x) # relu

plt.xlim(-4,4)
plt.ylim(-1,1)

ax = plt.gca()
ax.spines['right'].set_color('none') 
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')   
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

#Draw pic
plt.plot(x,y1,label='sigmoid',linestyle="-", color="red")
plt.plot(x,y2,label='tanh',linestyle="-", color="green")
plt.plot(x,y3,label='relu',linestyle="-", color="blue")

# Title
plt.legend(['Sigmoid','Tanh','Relu'])

# save pic
plt.savefig('plot_test.png', dpi=100)

# show it!!
plt.show()

效果图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值