Skip to content
class

LazyInstanceNorm2d

extends_LazyInstanceNormMixin
LazyInstanceNorm2d(eps: float = 1e-05, momentum: float | None = 0.1, affine: bool = True, track_running_stats: bool = True, device: DeviceLike = None, dtype: DTypeLike = None, bias: bool = True)

InstanceNorm2d with lazy num_features inference.

Identical to InstanceNorm2d except that num_features is inferred from x.shape[1] on the first forward pass.

Parameters

epsfloat= 1e-05
Numerical stability constant. Default: 1e-5.
momentumfloat or None= 0.1
EMA factor for running stats; None never updates them.
affinebool= True
If True, lazily allocates per-channel scale and shift. Default: True.
track_running_statsbool= True
If True, lazily allocates running mean/variance buffers. Default: True.
deviceDeviceLike= None
Device for lazily allocated tensors. Default: None.
dtypeDTypeLike= None
Data type for lazily allocated tensors. Default: None.

Notes

  • Input: (N,C,H,W)(N, C, H, W) — same as InstanceNorm2d. C is inferred on the first forward pass.
  • Output: same shape as the input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> norm = nn.LazyInstanceNorm2d()
>>> x = lucid.randn(2, 64, 128, 128)
>>> out = norm(x)   # num_features=64 inferred here
>>> out.shape
(2, 64, 128, 128)
Commonly used in generator networks where the channel dimension is
determined at runtime by a preceding convolution:
>>> norm_track = nn.LazyInstanceNorm2d(track_running_stats=True)
>>> out2 = norm_track(x)
>>> out2.shape
(2, 64, 128, 128)

Used by 1

Instance methods

5
fn

extra_repr

→strfrom _NormBase
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensorfrom _LazyInstanceNormMixin
forward(input: Tensor)

Normalise input, sizing the layer from it on the first call.

Parameters

inputTensor
(N, C, *spatial), or unbatched (C, *spatial); its C sets num_features the first time.

Returns

Tensor

Normalised tensor of the same shape as input.

fn

initialize_parameters

→Nonefrom _LazyModule
initialize_parameters(input: Tensor)

Size the layer from input and allocate its parameters, if it has not been yet.

The step the first forward takes, exposed so a model can be sized without running it. Does nothing once the layer is sized.

Parameters

inputTensor
An input of the shape the layer will see; only the axis that carries the layer's size is read.
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.