python matplotlib 画常用的三种激活函数

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. / (1. + np.exp(-x))
fig = plt.figure(figsize=(8,2))
ax1 = fig.add_subplot(131)
x = np.linspace(-10,10)
y= sigmoid(x)

ax1.spines['top'].set_color('none')
ax1.spines['right'].set_color('none')

ax1.xaxis.set_ticks_position('bottom')
ax1.spines['bottom'].set_position(('data',0))
ax1.set_xticks([-10,-5,0,5,10])
ax1.yaxis.set_ticks_position('left')
ax1.spines['left'].set_position(('data',0))
ax1.set_yticks([-1,-0.5,0.5,1])

plt.plot(x,y,label = 'Softmax',linestyle='-',color='blue')
plt.title('Sigmoid',fontsize=10)
# plt.legend(['Sigmoid'])
plt.savefig('Sigmoid.png')
############################################

################################
ax3 = fig.add_subplot(132)
x = np.arange(-10,10,0.1)
a = np.array(x)
y = (math.e**(x) - math.e**(-x)) / (math.e**(x) + math.e**(-x))

plt.xlim(-11,11)
ax3 = plt.gca()
ax3.spines['right'].set_color('none')
ax3.spines['top'].set_color('none')

ax3.xaxis.set_ticks_position('bottom')
ax3.yaxis.set_ticks_position('left')

ax3.spines['bottom'].set_position(('data',0))
ax3.spines['left'].set_position(('data',0))

plt.plot(x,y,label='Tanh',linestyle='-',color='green')
plt.title('Tanh',fontsize=10)#y=-0.2,可以控制标题在图片下方
# plt.legend(['Tanh'])
plt.savefig('Tanh.png',dpi=500) # 指定分辨率
##################################################
# fig =  plt.figure(figsize=(6,4))
ax2 = fig.add_subplot(133)
x = np.arange(-10,10)
y = np.where(x<0,0,x) # 小于0输出0,大于0输出y
plt.xlim(-11,11)
plt.ylim(-11,11)

ax2 = plt.gca() # 获得当前axis坐标轴对象
ax2.spines['right'].set_color('none') # 去除右边界线
ax2.spines['top'].set_color('none') # 去除上边界线

ax2.xaxis.set_ticks_position('bottom') # 指定下边的边作为x轴
ax2.yaxis.set_ticks_position('left') # 指定左边的边为y轴

ax2.spines['bottom'].set_position(('data',0)) # 指定data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax2.spines['left'].set_position(('data',0))  # 指定y轴绑定到x轴的0这个点上

plt.plot(x,y,label = 'ReLU',linestyle='-',color='darkviolet')
plt.title('ReLU',fontsize=10)
# plt.legend(['ReLU'])
plt.savefig('relu.png')
plt.show()

python matplotlib 画常用的三种激活函数_第1张图片

你可能感兴趣的:(python matplotlib 画常用的三种激活函数)