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()