mindspore.nn.GroupNorm
- class mindspore.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True, gamma_init='ones', beta_init='zeros', dtype=mstype.float32)[源代码]
- 在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\]- 参数:
- num_groups (int) - 沿通道维度待划分的组数。 
- num_channels (int) - 输入的通道数。 
- eps (float) - 添加到分母中的值,以确保数值稳定。默认值: - 1e-05。
- affine (bool) - 指定是否需要添加仿射变换参数。当设置为True时,给该层添加可学习的仿射变换参数gamma与beta。默认值: - True。
- gamma_init (Union[Tensor, str, Initializer, numbers.Number]) - gamma参数的初始化方法。str的值引用自函数 - mindspore.common.initializer.initializer(),包括- 'zeros'、- 'ones'、- 'xavier_uniform'、- 'he_uniform'等。默认值:- 'ones'。如果gamma_init是Tensor,则shape必须为 \((num\_channels)\) 。
- beta_init (Union[Tensor, str, Initializer, numbers.Number]) - beta参数的初始化方法。str的值引用自函数 - mindspore.common.initializer.initializer(),包括- 'zeros'、- 'ones'、- 'xavier_uniform'、- 'he_uniform'等。默认值:- 'zeros'如果gamma_init是Tensor,则shape必须为 \((num\_channels)\) 。
- dtype ( - mindspore.dtype) - Parameters的dtype。默认值:- mstype.float32。
 
- 输入:
- x (Tensor) - shape为 \((N, C, *)\) 的特征输入,其中 \(*\) 表示任意的附加维度。 
 
- 输出:
- Tensor,标准化和缩放的偏移Tensor,具有与 x 相同的shape和数据类型。 
- 异常:
- TypeError - num_groups 或 num_channels 不是int。 
- TypeError - eps 不是float。 
- TypeError - affine 不是bool。 
- ValueError - num_groups 或 num_channels 小于1。 
- ValueError - num_channels 未被 num_groups 整除。 
 
- 支持平台:
- Ascend- GPU- CPU
 - 样例: - >>> import mindspore as ms >>> import numpy as np >>> group_norm_op = ms.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.]]]]