r"""Applies Group Normalization over a mini-batch of inputs Refer to `Group Normalization `__ .. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta The mean and standard-deviation are ca
| 12 | |
| 13 | |
| 14 | class GroupNorm(Module): |
| 15 | r"""Applies Group Normalization over a mini-batch of inputs |
| 16 | Refer to `Group Normalization <https://arxiv.org/abs/1803.08494>`__ |
| 17 | |
| 18 | .. math:: |
| 19 | y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta |
| 20 | |
| 21 | The mean and standard-deviation are calculated separately over the each group. |
| 22 | :math:`\\gamma` and :math:`\\beta` are learnable affine transform parameters of |
| 23 | attr:`num_channels` if :attr:`affine` is ``True``. |
| 24 | |
| 25 | Args: |
| 26 | num_groups (int): number of groups that divided from channels. |
| 27 | num_channels (int): number of channels expected in input |
| 28 | eps: a value added to the denominator for numerical stability. Default: 1e-5 |
| 29 | affine: this module has learnable affine parameters (weight, bias) when affine is set to be True. |
| 30 | |
| 31 | Shape: |
| 32 | - Input: :math:`(N, C, H, W)` (now only support NCHW format tensor) |
| 33 | - Output: :math:`(N, C, H, W)` (same shape as input) |
| 34 | |
| 35 | Examples: |
| 36 | >>> import numpy as np |
| 37 | >>> inp = Tensor(np.arange(2 * 3 * 4 * 4).astype(np.float32).reshape(2, 3, 4, 4)) |
| 38 | >>> m = M.GroupNorm(3, 3) |
| 39 | >>> out = m(inp) |
| 40 | >>> out.numpy().shape |
| 41 | (2, 3, 4, 4) |
| 42 | """ |
| 43 | |
| 44 | def __init__(self, num_groups, num_channels, eps=1e-5, affine=True, **kwargs): |
| 45 | super().__init__(**kwargs) |
| 46 | assert num_channels % num_groups == 0 |
| 47 | self.num_groups = num_groups |
| 48 | self.num_channels = num_channels |
| 49 | self.eps = eps |
| 50 | self.affine = affine |
| 51 | if self.affine: |
| 52 | self.weight = Parameter(np.ones(num_channels, dtype=np.float32)) |
| 53 | self.bias = Parameter(np.zeros(num_channels, dtype=np.float32)) |
| 54 | else: |
| 55 | self.weight = None |
| 56 | self.bias = None |
| 57 | self.reset_parameters() |
| 58 | |
| 59 | def reset_parameters(self): |
| 60 | if self.affine: |
| 61 | ones_(self.weight) |
| 62 | zeros_(self.bias) |
| 63 | |
| 64 | def forward(self, x): |
| 65 | x = F.nn.group_norm( |
| 66 | x, self.num_groups, self.affine, self.weight, self.bias, self.eps |
| 67 | ) |
| 68 | return x |
| 69 | |
| 70 | def _module_info_string(self) -> str: |
| 71 | s = ( |
no outgoing calls