评价此页

GroupNorm#

class torch.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True, device=None, dtype=None)[源代码]#

对输入的小批量应用组归一化。

该层实现了论文 Group Normalization 中描述的操作。

y=xE[x]Var[x]+ϵγ+βy = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta

输入通道被分成 num_groups 组,每组包含 num_channels / num_groups 个通道。 num_channels 必须能被 num_groups 整除。均值和标准差分别在每组内计算。 γ\gammaβ\beta 是可学习的、大小为 num_channels 的逐通道仿射变换参数向量(如果 affineTrue)。方差通过有偏估计量计算,等同于 torch.var(input, unbiased=False)

此层在训练和评估模式下都使用从输入数据计算的统计量。

参数
  • num_groups (int) – 将通道分成多少组的数量。

  • num_channels (int) – 输入通道的数量。

  • eps (float) – 为了数值稳定性添加到分母中的值。默认值:1e-5

  • affine (bool) – 一个布尔值,当设置为 True 时,该模块具有可学习的逐通道仿射参数,初始化为 1(权重)和 0(偏置)。默认值:True

形状
  • 输入:(N,C,)(N, C, *),其中 C=num_channelsC=\text{num\_channels}

  • 输出: (N,C,)(N, C, *)(与输入形状相同)

示例

>>> input = torch.randn(20, 6, 10, 10)
>>> # Separate 6 channels into 3 groups
>>> m = nn.GroupNorm(3, 6)
>>> # Separate 6 channels into 6 groups (equivalent with InstanceNorm)
>>> m = nn.GroupNorm(6, 6)
>>> # Put all 6 channels into a single group (equivalent with LayerNorm)
>>> m = nn.GroupNorm(1, 6)
>>> # Activating the module
>>> output = m(input)