matplotlib绘制Sigmoid函数,Tanh函数,ReLU函数
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return1./(1+ np.exp(-x))
def tanh(x):
p(x)- np.exp(-x))/(np.exp(x)+ np.exp(-x))
def relu(x):
return np.where(x <0,0, x)
def plot_sigmoid():
x = np.linspace(-10,10,1000)  # 这个表⽰在-10到10之间⽣成1000个x值
y =sigmoid(x)  # 对上述⽣成的1000个数循环⽤sigmoid公式求对应的y
plt.xlim((-10,10))
plt.ylim((0.00,1.00))
plt.plot(x, y, color="black")  # ⽤上述⽣成的1000个xy值对⽣成1000个点
ax = a()
ax.spines['right'].set_color('none')  # 删除右边框设为⽆
ax.spines['top'].set_color('none')  # 删除上边框设为⽆
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))  # 调整x轴位置
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))  # 调整y轴位置
plt.savefig("sigmoid.png")
plt.legend(['Sigmoid'])
plt.show()
def plot_tanh():
x = np.arange(-10,10,0.1)
y =tanh(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# ax.spines['bottom'].set_color('none')
# ax.spines['left'].set_color('none')
ax.spines['left'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0))
ax.plot(x, y, color ="black")
plt.xlim([-10.05,10.05])
plt.ylim([-1.02,1.02])
ax.set_yticks([-1.0,-0.5,0.5,1.0])
matplotlib中subplot
ax.set_xticks([-10,-5,5,10])
plt.tight_layout()
plt.legend(['Tanh'])
plt.savefig("Tanh.png")
plt.show()
def plot_relu():
x = np.arange(-10,10,0.1)
y =relu(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# ax.spines['bottom'].set_color('none')
# ax.spines['left'].set_color('none')
ax.spines['left'].set_position(('data',0))
ax.plot(x, y, color ="black")
plt.xlim([-10.05,10.05])
plt.ylim([0,10.02])
plt.ylim([0,10.02])
ax.set_yticks([2,4,6,8,10])    plt.tight_layout()
plt.legend(['Relu'])
plt.savefig("Relu.png")
plt.show()
if __name__ =="__main__": plot_sigmoid()
plot_tanh()
plot_relu()
运⾏结果如下Sigmoid
Tanh
Relu

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。