fn

dirac_

Tensor
dirac_(tensor: Tensor, groups: int = 1)
source

Initialise a 3/4/5-D convolution weight in-place as a Dirac delta.

The kernel is filled with zeros except for a single 1 at the spatial centre of each (out_channel, in_channel) matched pair, so that — at initialisation — the convolution acts as the identity function on its input channels (subject to channel-count matching per group). Useful for residual networks where one wants gradients to flow unchanged through deep stacks at step 0.

Parameters

tensorTensor
Convolution weight of shape (out_channels, in_channels // groups, *K) where K is the 1-D, 2-D, or 3-D spatial kernel shape.
groupsint= 1
Number of groups in the convolution. out_channels must be divisible by groups. Default 1.

Returns

Tensor

tensor (mutated) for chaining.

Raises

ValueError
If tensor.ndim is not in {3, 4, 5} or if out_channels is not divisible by groups.

Notes

For convolution input xx with the resulting weight, (Wx):n=x:n(W \star x)_{:n} = x_{:n} where n=min(in_channels/groups,out_channels/groups)n = \min(\text{in\_channels}/\text{groups},\, \text{out\_channels}/\text{groups}).

Examples

>>> import lucid
>>> from lucid.nn.init import dirac_
>>> w = lucid.empty(16, 16, 3, 3)  # (out, in, kH, kW)
>>> dirac_(w)