Skip to content
class

InstanceNorm2d

extends_InstanceNormBase
InstanceNorm2d(num_features: int, eps: float = 1e-05, momentum: float | None = 0.1, affine: bool = False, track_running_stats: bool = False, device: DeviceLike = None, dtype: DTypeLike = None, bias: bool = True)

Instance normalization for 4-D input (N, C, H, W).

Normalises each (n, c) slice over its spatial height and width independently:

yn,c=xn,c−μn,cσn,c2+ε⋅γc+βcy_{n,c} = \frac{x_{n,c} - \mu_{n,c}} {\sqrt{\sigma_{n,c}^2 + \varepsilon}} \cdot \gamma_c + \beta_c

where μn,c\mu_{n,c} and σn,c2\sigma_{n,c}^2 are computed over the H×WH \times W spatial positions of sample nn, channel cc.

Unlike BatchNorm2d, the statistics do not mix across the batch, making the output of each image entirely independent of the other images in the batch. This property makes InstanceNorm2d well-suited to image style transfer, generative models, and any scenario where per-image normalization is desirable.

Parameters

num_featuresint
Number of feature channels CC.
epsfloat= 1e-05
Numerical stability constant. Default: 1e-5.
momentumfloat or None= 0.1
EMA factor for running stats; None never updates them.
affinebool= False
If True, learns per-channel γ\gamma and β\beta. Default: False.
track_running_statsbool= False
If True, maintains running mean/variance buffers. Default: False.
deviceDeviceLike= None
Device for parameters/buffers. Default: None.
dtypeDTypeLike= None
Data type for parameters/buffers. Default: None.

Attributes

weightParameter or None
Per-channel scale γ\gamma, shape (num_features,). None when affine=False.
biasParameter or None
Per-channel shift β\beta, shape (num_features,). None when affine=False.
running_meanTensor or None
Running mean per channel, shape (num_features,). None when track_running_stats=False.
running_varTensor or None
Running variance per channel, shape (num_features,). None when track_running_stats=False.

Notes

  • Input: (N,C,H,W)(N, C, H, W)
  • Output: (N,C,H,W)(N, C, H, W) — same shape.
  • Instance Norm was introduced for style transfer, where each image should be normalized independently so that stylistic statistics from other images do not bleed through.
  • At inference time, weight and bias can be replaced by style-conditioned tensors to implement Adaptive Instance Normalization (AdaIN).

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> norm = nn.InstanceNorm2d(64)
>>> x = lucid.randn(4, 64, 128, 128)
>>> out = norm(x)
>>> out.shape
(4, 64, 128, 128)
With affine parameters for conditional style transfer:
>>> norm_affine = nn.InstanceNorm2d(64, affine=True)
>>> out2 = norm_affine(x)
>>> out2.shape
(4, 64, 128, 128)

Used by 1

Instance methods

4
fn

extra_repr

→strfrom _NormBase
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensorfrom _InstanceNormBase
forward(input: Tensor)

Apply normalisation to the input tensor.

Parameters

inputTensor
Input tensor whose shape is documented in the class docstring.

Returns

Tensor

Normalised tensor of the same shape as input.

fn

reset_parameters

→Nonefrom _NormBase
reset_parameters()

Reset the running statistics, then weight to ones and bias to zeros.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> bn = nn.BatchNorm1d(2)
>>> _ = bn(lucid.randn(4, 2))
>>> bn.reset_parameters()
>>> bn.running_mean.tolist(), int(bn.num_batches_tracked.item())
([0.0, 0.0], 0)
fn

reset_running_stats

→Nonefrom _NormBase
reset_running_stats()

Put running_mean to zeros, running_var to ones and the count to zero.

In place, so anything holding a buffer sees the reset. Nothing to do without track_running_stats, or on a lazy layer before its first input.