评价此页

GroupNorm#

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

将Group Normalization应用于输入的mini-batch。

此层实现了论文 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 整除。均值和标准差在每个组内独立计算。如果 affineTrue,则 γ\gammaβ\beta 是可学习的、大小为 num_channels 的逐通道仿射变换参数向量。方差使用有偏估计量计算,等同于 torch.var(input, unbiased=False)

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

参数
  • num_groups (int) – 要将通道分离成的组数

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

  • eps (float) – 添加到分母的一个值,用于数值稳定性。默认值:1e-5

  • affine (bool) – 布尔值,如果设置为 True,则此模块具有可学习的逐通道仿射参数,初始化为一(权重)和零(偏置)。默认为 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)