InstanceNorm2d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False, device=None, dtype=None)¶
Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization.
The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. and are learnable parameter vectors of size C (where C is the input size) if
True. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
track_running_statsis set to
True, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentumargument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where is the estimated statistic and is the new observed value.
LayerNormare very similar, but have some subtle differences.
InstanceNorm2dis applied on each channel of channeled data like RGB images, but
LayerNormis usually applied on entire sample and often in NLP tasks. Additionally,
LayerNormapplies elementwise affine transform, while
InstanceNorm2dusually don’t apply affine transform.
num_features – from an expected input of size
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Default: 0.1
affine – a boolean value that when set to
True, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default:
track_running_stats – a boolean value that when set to
True, this module tracks the running mean and variance, and when set to
False, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default:
Output: (same shape as input)
>>> # Without Learnable Parameters >>> m = nn.InstanceNorm2d(100) >>> # With Learnable Parameters >>> m = nn.InstanceNorm2d(100, affine=True) >>> input = torch.randn(20, 100, 35, 45) >>> output = m(input)