PyTorch 激活函數(shù)的實(shí)現(xiàn)示例
激活函數(shù)是神經(jīng)網(wǎng)絡(luò)中至關(guān)重要的組成部分,它們?yōu)榫W(wǎng)絡(luò)引入了非線性特性,使得神經(jīng)網(wǎng)絡(luò)能夠?qū)W習(xí)復(fù)雜模式。PyTorch 提供了多種常用的激活函數(shù)實(shí)現(xiàn)。
常用激活函數(shù)
1. ReLU (Rectified Linear Unit)
數(shù)學(xué)表達(dá)式:

PyTorch實(shí)現(xiàn):
torch.nn.ReLU(inplace=False)
特點(diǎn):
- 計算簡單高效
- 解決梯度消失問題(正區(qū)間)
- 可能導(dǎo)致"神經(jīng)元死亡"(負(fù)區(qū)間梯度為0),ReLU 在輸入為負(fù)時輸出恒為 0,導(dǎo)致反向傳播中梯度消失,相關(guān)權(quán)重?zé)o法更新14。若神經(jīng)元長期處于負(fù)輸入狀態(tài),則會永久“死亡”,失去學(xué)習(xí)能力。
示例:
relu = nn.ReLU() input = torch.tensor([-1.0, 0.0, 1.0, 2.0]) output = relu(input) # tensor([0., 0., 1., 2.])
2. LeakyReLU
數(shù)學(xué)表達(dá)式:

PyTorch實(shí)現(xiàn):
torch.nn.LeakyReLU(negative_slope=0.01, inplace=False)
特點(diǎn):
- 解決了ReLU的"神經(jīng)元死亡"問題,通過引入負(fù)區(qū)間的微小斜率(如 torch.nn.LeakyReLU(negative_slope=0.01)),保留負(fù)輸入的梯度傳播,避免神經(jīng)元死亡。
- negative_slope通常設(shè)為0.01
示例
leaky_relu = nn.LeakyReLU(negative_slope=0.1) input = torch.tensor([-1.0, 0.0, 1.0, 2.0]) output = leaky_relu(input) # tensor([-0.1000, 0.0000, 1.0000, 2.0000])
3. Sigmoid
數(shù)學(xué)表達(dá)式:

PyTorch實(shí)現(xiàn):
torch.nn.Sigmoid()
特點(diǎn):
- 輸出范圍(0,1),適合二分類問題
- 容易出現(xiàn)梯度消失問題
- 輸出不以0為中心
示例:
sigmoid = nn.Sigmoid() input = torch.tensor([-1.0, 0.0, 1.0, 2.0]) output = sigmoid(input) # tensor([0.2689, 0.5000, 0.7311, 0.8808])
4. Tanh (Hyperbolic Tangent)
數(shù)學(xué)表達(dá)式:

PyTorch實(shí)現(xiàn):
torch.nn.Tanh()
特點(diǎn):
- 輸出范圍(-1,1),以0為中心
- 比sigmoid梯度更強(qiáng)
- 仍存在梯度消失問題
示例:
tanh = nn.Tanh() input = torch.tensor([-1.0, 0.0, 1.0, 2.0]) output = tanh(input) # tensor([-0.7616, 0.0000, 0.7616, 0.9640])
5. Softmax
數(shù)學(xué)表達(dá)式:

PyTorch實(shí)現(xiàn):
torch.nn.Softmax(dim=None)
特點(diǎn):
- 輸出為概率分布(和為1)
- 常用于多分類問題的輸出層
- dim參數(shù)指定計算維度
示例:
softmax = nn.Softmax(dim=1) input = torch.tensor([[1.0, 2.0, 3.0]]) output = softmax(input) # tensor([[0.0900, 0.2447, 0.6652]])
其他激活函數(shù)
6. ELU (Exponential Linear Unit)
torch.nn.ELU(alpha=1.0, inplace=False)
7. GELU (Gaussian Error Linear Unit)
torch.nn.GELU()
8. Swish
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)選擇指南
- 隱藏層:通常首選ReLU及其變體(LeakyReLU、ELU等)
- 二分類輸出層:Sigmoid
- 多分類輸出層:Softmax
- 需要負(fù)輸出的情況:Tanh或LeakyReLU
- Transformer模型:常用GELU
自定義激活函數(shù)
PyTorch可以輕松實(shí)現(xiàn)自定義激活函數(shù):
class CustomActivation(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return torch.where(x > 0, x, torch.exp(x) - 1)注意事項(xiàng)
- 梯度消失/爆炸問題
- 死亡神經(jīng)元問題(特別是ReLU)
- 計算效率考慮
- 初始化方法應(yīng)與激活函數(shù)匹配
相關(guān)文章
Python動態(tài)導(dǎo)入模塊的方法實(shí)例分析
這篇文章主要介紹了Python動態(tài)導(dǎo)入模塊的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python動態(tài)導(dǎo)入系統(tǒng)模塊、自定義模塊以及模塊列表的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
基于Python實(shí)現(xiàn)簡易的植物識別小系統(tǒng)
這篇文章主要介紹了利用Python實(shí)現(xiàn)一個簡易的植物識別系統(tǒng),文中的示例代碼簡潔易懂,對我們學(xué)習(xí)Python有一定的幫助,需要的小伙伴可以參考一下2021-12-12
用Python進(jìn)行TCP網(wǎng)絡(luò)編程的教程
這篇文章主要介紹了用Python進(jìn)行TCP網(wǎng)絡(luò)編程的教程,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識,代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
ndarray的轉(zhuǎn)置(numpy.transpose()與A.T命令對比分析)
這篇文章主要介紹了ndarray的轉(zhuǎn)置(numpy.transpose()與A.T命令對比分析),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄
這篇文章主要介紹了python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄,本文給大家通過實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

