激勵(lì)函數(shù)通常是非線性的眼刃,其通過(guò)對(duì)線性矩陣運(yùn)算結(jié)果的非線性變化摇肌,使用神經(jīng)網(wǎng)絡(luò)得以擬合任意函數(shù)
1. 數(shù)據(jù)準(zhǔn)備
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
# fake data
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x = Variable(x)
x_np = x.data.numpy() # numpy array for plotting
2. 常見(jiàn)非線性激勵(lì)函數(shù)
# following are popular activation functions
y_relu = torch.relu(x).data.numpy()
y_sigmoid = torch.sigmoid(x).data.numpy()
y_tanh = torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy() # there's no softplus in torch
y_softmax = torch.softmax(x, dim=0).data.numpy()
#softmax is a special kind of activation function, it is about probability
- Relu , Tanh , Sigmoid 是最常見(jiàn)的激勵(lì)函數(shù)围小,常用與 CNN(Relu),RNN(Tanh)等連用
- Softmax 函數(shù)亦是常見(jiàn)的激勵(lì)函數(shù)变秦,多用于預(yù)測(cè)與分類問(wèn)題蹦玫,其可將任意輸出轉(zhuǎn)換為概率
- Softplus 函數(shù)在Torch 中不存在,不常用到
3. 圖像可視化——Matplolib
# plt to visualize these activation function
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')
plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')
plt.subplot(223)
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')
plt.subplot(224)
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='best')
plt.show()
1.plt.figure ('title','figsize'):初始化plt框圖樱溉,figsize表示該框圖的結(jié)構(gòu)
2.plt.subplot('num-1','num-2','num-3'):用于在plt框圖中構(gòu)建子圖,num-1與num-2表示子圖的二維位置歧焦,num-3表示框圖順序
plt.show():顯示plt圖像