評價此頁

RMSNorm#

class torch.nn.modules.normalization.RMSNorm(normalized_shape, eps=None, elementwise_affine=True, device=None, dtype=None)[原始碼]#

對輸入的小批次應用均方根層歸一化。

該層實現了論文 Root Mean Square Layer Normalization 中描述的操作。

yi=xiRMS(x)γi,whereRMS(x)=ϵ+1ni=1nxi2y_i = \frac{x_i}{\mathrm{RMS}(x)} * \gamma_i, \quad \text{where} \quad \text{RMS}(x) = \sqrt{\epsilon + \frac{1}{n} \sum_{i=1}^{n} x_i^2}

RMS 是在最後 D 個維度上計算的,其中 Dnormalized_shape 的維度。例如,如果 normalized_shape(3, 5) (一個二維形狀),則 RMS 是在輸入的最後 2 個維度上計算的。

引數
  • normalized_shape (intlisttorch.Size) –

    input shape from an expected input of size

    [×normalized_shape[0]×normalized_shape[1]××normalized_shape[1]][* \times \text{normalized\_shape}[0] \times \text{normalized\_shape}[1] \times \ldots \times \text{normalized\_shape}[-1]]

    If a single integer is used, it is treated as a singleton list, and this module will normalize over the last dimension which is expected to be of that specific size.

  • eps (Optional[float]) – 用於數值穩定性的加法項。預設值:torch.finfo(x.dtype).eps

  • elementwise_affine (bool) – 當設定為 True 時,此模組將具有可學習的逐元素仿射引數,初始化為 1(對於權重)。預設值:True

形狀
  • 輸入:(N,)(N, *)

  • 輸出:(N,)(N, *)(與輸入形狀相同)

示例

>>> rms_norm = nn.RMSNorm([2, 3])
>>> input = torch.randn(2, 2, 3)
>>> rms_norm(input)
extra_repr()[原始碼]#

返回模組的額外表示。

返回型別

str

forward(x)[原始碼]#

執行前向傳播。

返回型別

張量

reset_parameters()[原始碼]#

根據 __init__ 中的初始化重置引數。