mindspore.mint.nn.GroupNorm

查看源文件
class mindspore.mint.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True, dtype=None)[源代码]

在mini-batch输入上进行组归一化。

Group Normalization被广泛用于递归神经网络中。适用单个训练用例的mini-batch输入归一化,详见论文 Group Normalization

Group Normalization把通道划分为组,然后计算每一组之内的均值和方差,以进行归一化。其中 \(\gamma\) 是通过训练学习出的scale值,\(\beta\) 是通过训练学习出的shift值。

公式如下,

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

其中, \(\gamma\)weight\(\beta\)bias\(\epsilon\)eps

参数:
  • num_groups (int) - 沿通道维度待划分的组数。

  • num_channels (int) - 输入的通道维度数。

  • eps (float, 可选) - 添加到分母中的值,以确保数值稳定。默认值: 1e-05

  • affine (bool, 可选) - 当被设置为 True 时,参数 \(\gamma\)\(\beta\) 是可学习的。默认值: True

  • dtype (mindspore.dtype, 可选) - Parameter的类型。默认值: None

输入:
  • input (Tensor) - 输入特征的shape为 \((N, C, *)\),其中 \(*\) 任意数量的附加维度。

输出:

Tensor,被标准化和缩放偏移后的Tensor,具有与 input 相同的shape和数据类型。

异常:
  • TypeError - num_groupsnum_channels 不是int。

  • TypeError - eps 不是float。

  • TypeError - affine 不是bool。

  • ValueError - num_groupsnum_channels 小于1。

  • ValueError - num_channels 未被 num_groups 整除。

支持平台:

Ascend

样例:

>>> import mindspore as ms
>>> import numpy as np
>>> group_norm_op = ms.mint.nn.GroupNorm(2, 2)
>>> x = ms.Tensor(np.ones([1, 2, 4, 4], np.float32))
>>> output = group_norm_op(x)
>>> print(output)
[[[[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]]]