python绘制sigmoid及tanh函数

Sigmoid激活函数

import math 
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
a=np.array(x)
y1=1/(1+math.e**(-x))
y2=math.e**(-x)/((1+math.e**(-x))**2)
plt.xlim(-11,11)
ax = plt.gca()                                            # get current axis 获得坐标轴对象
ax.spines['right'].set_color('none') 
ax.spines['top'].set_color('none')         # 将右边 上边的两条边颜色设置为空 其实就相当于抹掉这两条边
ax.xaxis.set_ticks_position('bottom')   
ax.yaxis.set_ticks_position('left')          # 指定下边的边作为 x 轴   指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0))   #指定 data  设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
plt.plot(x,y1,label='Sigmoid',linestyle="-", color="blue")#label为标签
plt.plot(x,y2,label='Deriv.Sigmoid',linestyle="--", color="red")#l
#plt.legend(loc=0,ncol=2)
plt.legend(['Sigmoid','Deriv.Sigmoid'])

plt.savefig('plot_test.png', dpi=500) #指定分辨率
plt.show()

Tanh激活函数

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

x = np.arange(-10,10)
a=np.array(x)
y1=(math.e**(x)-math.e**(-x))/(math.e**(x)+math.e**(-x))
plt.xlim(-11,11)
ax = plt.gca()                                            # get current axis 获得坐标轴对象
ax.spines['right'].set_color('none') 
ax.spines['top'].set_color('none')         # 将右边 上边的两条边颜色设置为空 其实就相当于抹掉这两条边
ax.xaxis.set_ticks_position('bottom')   
ax.yaxis.set_ticks_position('left')          # 指定下边的边作为 x 轴   指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0))   #指定 data  设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
plt.plot(x,y1,label='Tanh',linestyle="-", color="green")#label为标签
plt.legend(['Tanh'])
plt.savefig('Tanh.png', dpi=500) #指定分辨
plt.show()

ReLU激活函数

import numpy as np
import matplotlib.pyplot as plt
 
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111) 
x = np.arange(-10, 10)
y = np.where(x<0,0,x)#满足条件(condition),输出x,不满足输出y
plt.xlim(-11,11)
plt.ylim(-11,11)
ax = plt.gca()                                            # get current axis 获得坐标轴对象
ax.spines['right'].set_color('none') 
ax.spines['top'].set_color('none')         # 将右边 上边的两条边颜色设置为空 其实就相当于抹掉这两条边
ax.xaxis.set_ticks_position('bottom')   
ax.yaxis.set_ticks_position('left')          # 指定下边的边作为 x 轴   指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0))   #指定 data  设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))

plt.plot(x,y,label='ReLU',linestyle="-", color="darkviolet")#label为标签
plt.legend(['ReLU'])
plt.savefig('ReLU.png', dpi=500) #指定分辨
plt.show()
#!/usr/bin/python #encoding:utf-8
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus']=False
 
 
def  sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))
 
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111)
 
x = np.linspace(-10, 10)
y = sigmoid(x)
tanh = 2*sigmoid(2*x) - 1
 
plt.xlim(-11,11)
plt.ylim(-1.1,1.1)
 
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
 
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.set_xticks([-10,-5,0,5,10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
ax.set_yticks([-1,-0.5,0.5,1])
 
plt.plot(x,y,label="Sigmoid",color = "blue")
plt.plot(2*x,tanh,label="Tanh", color = "red")
plt.legend()
plt.show()

转自:参考1参考2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值