问题描述:如何使用Python绘制常见的几种激活函数?(sigmoid、Tanh、Relu、Leaky Relu、
ELU、Softplus、Softmax、Smish)
解答:这里八种不同的激活函数,使用了不同的颜色进行了绘制。
#import pandas as pd
#from scipy import stats
import math
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10,10)
##### 绘制sigmoid图像
fig = plt.figure(figsize=(10,5))
y_sigmoid = 1/(1+np.exp(-x))
ax = fig.add_subplot(241)
ax.plot(x,y_sigmoid,color='black')
ax.grid()
ax.set_title('(1) Sigmoid')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Tanh图像
ax = fig.add_subplot(242)
y_tanh = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))
ax.plot(x,y_tanh,color='red')
ax.grid()
ax.set_title('(2) Tanh')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Relu图像
ax = fig.add_subplot(243)
y_relu = np.array([0*item if item<0 else item for item in x ])
ax.plot(x,y_relu,color='blue')
ax.grid()
ax.set_title('(3) ReLu')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Leaky Relu图像
ax = fig.add_subplot(244)
y_relu = np.array([0.1*item if item<0 else item for item in x ])
ax.plot(x,y_relu,color='green')
ax.grid()
ax.set_title('(4) Leaky Relu (a = 0.1)')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制ELU图像
ax = fig.add_subplot(245)
y_elu = np.array([2.0*(np.exp(item)-1) if item<0 else item for item in x ])
ax.plot(x,y_elu,color='orange')
ax.grid()
ax.set_title('(5) ELU (α=2.0)')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Softplus图像
ax = fig.add_subplot(246)
y_softplus = np.array([np.math.log(1-np.exp(item), np.e) if item < 0 else item for item in x ])
ax.plot(x,y_softplus,color='yellow')
ax.grid()
ax.set_title('(6) Softplus')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Softmax图像
ax = fig.add_subplot(247)
z = np.linspace(-10,10) #可以单独调节softmax的输入取值
zero = 0
sumz = np.sum(np.array([np.exp(item) for item in z]))
print(sumz)
y_softmax = np.array([np.exp(item)/sumz for item in z ])
print(y_softmax)
ax.plot(z,y_softmax,color='brown')
ax.grid()
ax.set_title('(7) Softmax')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
##### 绘制Smish图像
ax = fig.add_subplot(248)
y_softplus = x * y_sigmoid
ax.plot(x,y_softplus,color='purple')
ax.grid()
ax.set_title('(6) Swish')
ax.spines['right'].set_color('none') # 去除右边界线
ax.spines['top'].set_color('none') # 去除上边界线
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.tight_layout()
plt.savefig('Activation.png')
plt.show()