Tensorflow&Python matplotlib pylot: 激活函数Sigmoid、ReLU

1. Sigmoid

import tensorflow as tf 
from matplotlib import pyplot as plt
# 设置x坐标的间隔
x = tf.linspace(-6.,6.,100) 
# 定义函数
f = 1/(1+tf.exp(-x))
# 绘制函数曲线,设置颜色,线宽,线型
plt.plot(x,f,color='blue',linewidth=1.0,linestyle='-',label='sigmoid')
# 设置横轴的上下限
plt.xlim(-6.0,6.0)
# 设置纵轴的上下限
plt.ylim(0.0,1.0)
# 设置脊柱,将脊柱放在图的中间
# 每幅图有四条脊柱(上下左右),将上和右脊柱设置为无色
# 调整剩下的脊柱到合适的位置
ax = plt.gca()
# 设置右侧脊柱为无色
ax.spines['right'].set_color('none')
# 设置上侧脊柱为无色
ax.spines['top'].set_color('none')
# 将下边的脊柱设置为x轴
ax.xaxis.set_ticks_position('bottom')
# 将下边的脊柱位置放在0处
ax.spines['bottom'].set_position(('data',0))
# 将左边的脊柱设置为y轴
ax.yaxis.set_ticks_position('left')
# 将左边的脊柱位置放在0处
ax.spines['left'].set_position(('data',0))

# 添加图例
plt.legend(loc='upper left')
plt.show()

2. ReLU

import tensorflow as tf 
from matplotlib import pyplot as plt
# 设置x坐标的间隔
x = tf.linspace(-6.,6.,1000) 
# 定义函数
#自定义ReLU函数 ReLU = tf.maximum(x,0)
ReLU = tf.nn.relu(x)
# 绘制函数曲线,设置颜色,线宽,线型
plt.plot(x,ReLU,color='purple',linewidth=1.0,linestyle='-',label='ReLU')
# 设置横轴的上下限
plt.xlim(-6.0,6.0)
# 设置纵轴的上下限
plt.ylim(0.0,6.0)
# 设置脊柱,将脊柱放在图的中间
# 每幅图有四条脊柱(上下左右),将上和右脊柱设置为无色
# 调整剩下的脊柱到合适的位置
ax = plt.gca()
# 设置右侧脊柱为无色
ax.spines['right'].set_color('none')
# 设置上侧脊柱为无色
ax.spines['top'].set_color('none')
# 将下边的脊柱设置为x轴
ax.xaxis.set_ticks_position('bottom')
# 将下边的脊柱位置放在0处
ax.spines['bottom'].set_position(('data',0))
# 将左边的脊柱设置为y轴
ax.yaxis.set_ticks_position('left')
# 将左边的脊柱位置放在0处
ax.spines['left'].set_position(('data',0))

# 添加图例
plt.legend(loc='upper left')
plt.show()

3. LeakyReLU

LeakyReLU = tf.nn.leaky_relu(x)
# 绘制函数曲线,设置颜色,线宽,线型
plt.plot(x,ReLU,color='purple',linewidth=1.0,linestyle='-',label='LeakyReLU')
# 设置横轴的上下限
plt.xlim(-6.0,6.0)
# 设置纵轴的上下限
plt.ylim(-2.0,6.0)

4. Tanh

import tensorflow as tf 
from matplotlib import pyplot as plt
# 设置x坐标的间隔
x = tf.linspace(-6.,6.,100) 
# 定义函数
#自定义ReLU函数 ReLU = tf.maximum(x,0)
tanh = tf.nn.tanh(x)
# 绘制函数曲线,设置颜色,线宽,线型
plt.plot(x,tanh,color='purple',linewidth=1.0,linestyle='-',label='Tanh')
# 设置横轴的上下限
plt.xlim(-6.0,6.0)
# 设置纵轴的上下限
plt.ylim(-1.0,1.0)
# 设置纵轴的记号
plt.yticks([-1,0,1])
# 设置脊柱,将脊柱放在图的中间
# 每幅图有四条脊柱(上下左右),将上和右脊柱设置为无色
# 调整剩下的脊柱到合适的位置
ax = plt.gca()
# 设置右侧脊柱为无色
ax.spines['right'].set_color('none')
# 设置上侧脊柱为无色
ax.spines['top'].set_color('none')
# 将下边的脊柱设置为x轴
ax.xaxis.set_ticks_position('bottom')
# 将下边的脊柱位置放在0处
ax.spines['bottom'].set_position(('data',0))
# 将左边的脊柱设置为y轴
ax.yaxis.set_ticks_position('left')
# 将左边的脊柱位置放在0处
ax.spines['left'].set_position(('data',0))

# 添加图例
plt.legend(loc='upper left')
plt.show()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪易

给我来点鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值