InstanceNorm2d
_InstanceNormBaseInstanceNorm2d(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:
where and are computed over the spatial positions of sample , channel .
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_ featuresintepsfloat= 1e-051e-5.momentumfloat or None= 0.1None never updates them.affinebool= FalseTrue, learns per-channel and .
Default: False.track_ running_ statsbool= FalseTrue, maintains running mean/variance buffers.
Default: False.deviceDeviceLike= NoneNone.dtypeDTypeLike= NoneNone.Attributes
weightParameter or None(num_features,).
None when affine=False.biasParameter or None(num_features,).
None when affine=False.running_ meanTensor or None(num_features,).
None when track_running_stats=False.running_ varTensor or None(num_features,).
None when track_running_stats=False.Notes
- Input:
- Output: — 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,
weightandbiascan 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
4Return a string representation of the layer's configuration.
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)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.