1、nn.ELU
基本語法:
class torch.nn.ELU(alpha=1.0, inplace=False)
按元素應用 Exponential Linear Unit (ELU) 函數。
論文中描述的方法:通過指數線性單元 (ELU) 進行快速準確的深度網絡學習。
ELU 定義為:
E L U ( x ) = { x , i f x > 0 α ? ( e x p ( x ) ? 1 ) , i f x ≤ 0 ELU(x)=\{\begin{array}{c} x, & \mathrm{if~x > 0}\\ \alpha * (exp(x)-1), & \mathrm{if x \le 0} \end{array} ELU(x)={x,α?(exp(x)?1),?if?x>0ifx≤0?
Parameters 參數:
- alpha (float) – ELU 公式的 α α α值。默認值:1.0
- Inplace(bool) – 可以選擇就地執行作。默認值: False
Shape: 形狀:
- Input::(?),其中 ?表示任意數量的維度。
- Output:(?),與輸入的形狀相同。
Examples: 例子:
>>> m = nn.ELU()
>>> input = torch.randn(2)
>>> output = m(input)
2、ReLU
基本語法:
class torch.nn.ReLU(inplace=False)
按元素應用修正的線性單元函數。
R e L U ( x ) = ( x ) + = m a x ( 0 , x ) ReLU(x)=(x)^+=max(0,x) ReLU(x)=(x)+=max(0,x)
Parameters 參數:
- Inplace (bool) – 可以選擇就地執行作。默認值: False
參數說明:
- inplace=False
import torch
from torch import nnm = nn.ReLU(inplace=False)
input = torch.randn(2)
print(input)
output = m(input)
print(input)
print(output)
此時,輸入Input并未改變,而是復制了一份原始輸入并在該復制上進行非線性激活:
tensor([ 1.6213, -0.0794])
tensor([ 1.6213, -0.0794])
tensor([1.6213, 0.0000])
- inplace=True
import torch
from torch import nnm = nn.ReLU(inplace=True)
input = torch.randn(2)
print(input)
output = m(input)
print(input)
print(output)
此時,直接對原始輸入數據進行非線性激活:
tensor([-0.3541, -0.6384])
tensor([0., 0.])
tensor([0., 0.])
Shape: 形狀:
- Input: (?) ,其中 ? 表示任意數量的維度。
- Output: (?),與輸入的形狀相同。
Examples: 例子:
>>> m = nn.ReLU()>>> input = torch.randn(2)>>> output = m(input)An implementation of CReLU - https://arxiv.org/abs/1603.05201>>> m = nn.ReLU()>>> input = torch.randn(2).unsqueeze(0)>>> output = torch.cat((m(input), m(-input)))
3、Sigmoid
基本語法:
class torch.nn.Sigmoid(*args, **kwargs)
按元素應用 Sigmoid 函數。
S i g m o i d ( x ) = σ ( x ) = 1 1 + e x p ( ? x ) Sigmoid(x)=\sigma(x)=\frac{1}{1+exp(-x)} Sigmoid(x)=σ(x)=1+exp(?x)1?
Shape: 形狀:
- Input: (?),其中 ? 表示任意數量的維度。
- Output: (?),與輸入的形狀相同。
Examples: 例子:
>>> m = nn.Sigmoid()
>>> input = torch.randn(2)
>>> output = m(input)
4、Tanh
基本語法:
class torch.nn.Tanh(*args, **kwargs)
按元素應用 Hyperbolic Tangent (Tanh) 函數。
T a n h ( x ) = t a n h ( x ) = e x p ( x ) ? e x p ( ? x ) e x p ( x ) + e x p ( ? x ) Tanh(x)=tanh(x)=\frac{exp(x)-exp(-x)}{exp(x)+exp(-x)} Tanh(x)=tanh(x)=exp(x)+exp(?x)exp(x)?exp(?x)?
Shape: 形狀:
- Input: (?),其中 ? 表示任意數量的維度。
- Output: (?),與輸入的形狀相同。
Examples: 例子:
>>> m = nn.Tanh()
>>> input = torch.randn(2)
>>> output = m(input)
5、LeakyReLU
基本語法:
class torch.nn.LeakyReLU(negative_slope=0.01, inplace=False)
按元素應用 LeakyReLU 函數。
L e a k y R e L U ( x ) = m a x ( 0 , x ) + n e g a t i v e s l o p e ? m i n ( 0 , x ) LeakyReLU(x)=max(0,x)+negative_slope*min(0,x) LeakyReLU(x)=max(0,x)+negatives?lope?min(0,x)
or
L e a k y R e L U ( x ) = { x , i f x ≥ 0 n e g a t i v e s l o p e × x , o t h e r w i s e LeakyReLU(x)=\{\begin{array}{c}x, & \mathrm{if~x\ge0}\\ negative_slope \times x, & \mathrm{otherwise}\end{array} LeakyReLU(x)={x,negatives?lope×x,?if?x≥0otherwise?
Parameters 參數
- negative_slope(float)– 控制負斜率的角度 (用于負輸入值)。默認值:1e-2
- Inplace (bool)– 可以選擇就地執行作。默認值: False
Shape: 形狀:
- 輸入: (?) 其中 * 表示任意數量的附加維度
- 輸出: (?),與輸入形狀相同
Examples: 例子:
>>> m = nn.LeakyReLU(0.1)
>>> input = torch.randn(2)
>>> output = m(input)
5、Softplus
基本語法:
class torch.nn.Softplus(beta=1.0, threshold=20.0)
按元素應用 Softplus 函數。
S o f t p l u s ( x ) = 1 β ? log ? ( 1 + e x p ( β ? x ) ) Softplus(x)=\frac{1}{\beta}*\log(1+exp(\beta*x)) Softplus(x)=β1??log(1+exp(β?x))
SoftPlus 是 ReLU 函數的平滑近似值,可用于將機器的輸出限制為始終為正。
為了數值穩定性,當 時 i n p u t × β > t h r e s h o l d input×β>threshold input×β>threshold,實現恢復為線性函數。
Parameters 參數
- beta(float) – Softplus 公式的值 β \beta β。默認值:1
- threshold(float)– 高于此值的值將恢復為線性函數。默認值:20
參數說明:
- threshold( β \beta β=1)
當 i n p u t × β ≤ t h r e s h o l d input×β \le threshold input×β≤threshold時:
import torch
from torch import nnm = nn.Softplus()
input = torch.randn(2)
print(input)output = m(input)
print(output)
tensor([-0.2053, 0.3776])
tensor([0.5958, 0.8997])
可以驗證: S o f t p l u s ( ? 0.2053 ) = 1 1 ? log ? ( 1 + e x p ( 1 ? ( ? 0.2053 ) ) ) = 0.595756... Softplus(-0.2053)=\frac{1}{1}*\log(1+exp(1*(-0.2053)))=0.595756... Softplus(?0.2053)=11??log(1+exp(1?(?0.2053)))=0.595756...
S o f t p l u s ( 0.3776 ) = 1 1 ? log ? ( 1 + e x p ( 1 ? ( 0.3776 ) ) ) = 0.899665... Softplus(0.3776)=\frac{1}{1}*\log(1+exp(1*(0.3776)))=0.899665... Softplus(0.3776)=11??log(1+exp(1?(0.3776)))=0.899665...
當 i n p u t × β > t h r e s h o l d input×β > threshold input×β>threshold時:
import torch
from torch import nnm = nn.Softplus()
input = torch.tensor([30.])
print(input)output = m(input)
print(output)
tensor([30.])
tensor([30.])
此時恢復為線性函數
Shape: 形狀:
- Input: (?) ,其中 ? 表示任意數量的維度。
- Output: (?) ,與輸入的形狀相同。
Examples: 例子:
>>> m = nn.Softplus()
>>> input = torch.randn(2)
>>> output = m(input)