本文簡要介紹python語言中 torch.nn.LayerNorm 的用法。
- 用法:- class torch.nn.LayerNorm(normalized_shape, eps=1e-05, elementwise_affine=True, device=None, dtype=None)
- ~LayerNorm.weight-當 - elementwise_affine設置為- True時,形狀為 的模塊的可學習權重。這些值被初始化為 1。
- ~LayerNorm.bias-當 - elementwise_affine設置為- True時,形狀為 的模塊的可學習偏差。這些值被初始化為 0。
 
- 如論文 Layer Normalization 中所述,對小批量輸入應用層規範化 - 均值和標準差是在最後 - D維度上計算的,其中- D是- normalized_shape的維度。例如,如果- normalized_shape是- (3, 5)(二維形狀),則在輸入的最後 2 維(即- input.mean((-2, -1)))上計算平均值和標準差。 和 是- normalized_shape的可學習仿射變換參數,如果- elementwise_affine是- True。標準差是通過有偏估計器計算的,相當於- torch.var(input, unbiased=False)。- 注意 - 與使用 - affine選項為每個整個通道/平麵應用標量比例和偏差的批量歸一化和實例歸一化不同,層歸一化通過- elementwise_affine應用 per-element 比例和偏差。- 該層使用從訓練和評估模式中的輸入數據計算的統計數據。 - 形狀:
- 輸入: 
- 輸出:(與輸入的形狀相同) 
 
 - 例子:- >>> # NLP Example >>> batch, sentence_length, embedding_dim = 20, 5, 10 >>> embedding = torch.randn(batch, sentence_length, embedding_dim) >>> layer_norm = nn.LayerNorm(embedding_dim) >>> # Activate module >>> layer_norm(embedding) >>> >>> # Image Example >>> N, C, H, W = 20, 5, 10, 10 >>> input = torch.randn(N, C, H, W) >>> # Normalize over the last three dimensions (i.e. the channel and spatial dimensions) >>> # as shown in the image below >>> layer_norm = nn.LayerNorm([C, H, W]) >>> output = layer_norm(input)  
參數:
變量:
相關用法
- Python PyTorch LazyModuleMixin用法及代碼示例
- Python PyTorch LambdaLR用法及代碼示例
- Python PyTorch Laplace用法及代碼示例
- Python PyTorch LazyModuleExtensionMixin.apply用法及代碼示例
- Python PyTorch LinearLR用法及代碼示例
- Python PyTorch LKJCholesky用法及代碼示例
- Python PyTorch L1Loss用法及代碼示例
- Python PyTorch LPPool2d用法及代碼示例
- Python PyTorch LeakyReLU用法及代碼示例
- Python PyTorch LogSigmoid用法及代碼示例
- Python PyTorch LowRankMixtureCrossNet用法及代碼示例
- Python PyTorch LineReader用法及代碼示例
- Python PyTorch LogNormal用法及代碼示例
- Python PyTorch LocalResponseNorm用法及代碼示例
- Python PyTorch LSTM用法及代碼示例
- Python PyTorch LowRankMultivariateNormal用法及代碼示例
- Python PyTorch Linear用法及代碼示例
- Python torchrec.modules.crossnet.LowRankCrossNet用法及代碼示例
- Python PyTorch LogSoftmax用法及代碼示例
- Python PyTorch LSTMCell用法及代碼示例
- Python PyTorch LocalElasticAgent用法及代碼示例
- Python PyTorch LPPool1d用法及代碼示例
- Python PyTorch LinearReLU用法及代碼示例
- Python PyTorch frexp用法及代碼示例
- Python PyTorch jvp用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.nn.LayerNorm。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
