r"""Applies Instance Normalization over a mini-batch of inputs Refer to `Instance Normalization `__ .. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta The mean and standard-deviation are calculat
| 76 | |
| 77 | |
| 78 | class InstanceNorm(Module): |
| 79 | r"""Applies Instance Normalization over a mini-batch of inputs |
| 80 | Refer to `Instance Normalization <https://arxiv.org/abs/1607.08022>`__ |
| 81 | |
| 82 | .. math:: |
| 83 | y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta |
| 84 | |
| 85 | The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. |
| 86 | :math:`\\gamma` and :math:`\\beta` are learnable affine transform parameters of |
| 87 | attr:`num_channels` if :attr:`affine` is ``True``. |
| 88 | Note that InstanceNorm equals using GroupNorm with num_groups = num_channels. |
| 89 | |
| 90 | Args: |
| 91 | num_channels (int): number of channels expected in input |
| 92 | eps: a value added to the denominator for numerical stability. Default: 1e-5 |
| 93 | affine: this module has learnable affine parameters (weight, bias) when affine is set to be True. |
| 94 | |
| 95 | Shape: |
| 96 | - Input: :math:`(N, C, H, W)` (now only support NCHW format tensor) |
| 97 | - Output: :math:`(N, C, H, W)` (same shape as input) |
| 98 | |
| 99 | Examples: |
| 100 | >>> import numpy as np |
| 101 | >>> inp = Tensor(np.arange(2 * 3 * 4 * 4).astype(np.float32).reshape(2, 3, 4, 4)) |
| 102 | >>> m = M.InstanceNorm(3) |
| 103 | >>> out = m(inp) |
| 104 | >>> out.numpy().shape |
| 105 | (2, 3, 4, 4) |
| 106 | """ |
| 107 | |
| 108 | def __init__(self, num_channels, eps=1e-05, affine=True, **kwargs): |
| 109 | super().__init__(**kwargs) |
| 110 | self.num_channels = num_channels |
| 111 | self.eps = eps |
| 112 | self.affine = affine |
| 113 | if self.affine: |
| 114 | self.weight = Parameter(np.ones(num_channels, dtype="float32")) |
| 115 | self.bias = Parameter(np.zeros(num_channels, dtype="float32")) |
| 116 | else: |
| 117 | self.weight = None |
| 118 | self.bias = None |
| 119 | self.reset_parameters() |
| 120 | |
| 121 | def reset_parameters(self): |
| 122 | if self.affine: |
| 123 | ones_(self.weight) |
| 124 | zeros_(self.bias) |
| 125 | |
| 126 | def forward(self, x): |
| 127 | x = F.nn.instance_norm(x, self.affine, self.weight, self.bias, self.eps) |
| 128 | return x |
| 129 | |
| 130 | def _module_info_string(self) -> str: |
| 131 | s = "channels={num_channels}, eps={eps}, affine={affine}" |
| 132 | return s.format(**self.__dict__) |
| 133 | |
| 134 | |
| 135 | class LayerNorm(Module): |
no outgoing calls