评价此页

RMSNorm#

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

对输入的小批量应用均方根层归一化。

This layer implements the operation as described in the paper 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}

The RMS is taken over the last D dimensions, where D is the dimension of normalized_shape. For example, if normalized_shape is (3, 5) (a 2-dimensional shape), the RMS is computed over the last 2 dimensions of the input.

参数
  • normalized_shape (intlisttorch.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]]

    如果使用单个整数,则将其视为单例列表,并且此模块将在最后一个维度上进行归一化,该维度预期具有该特定大小。

  • 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__ 中的初始化重置参数。