Skip to content
class

NoisyLinear

extendsModule
NoisyLinear(in_features: int, out_features: int, bias: bool = True, sigma_zero: float = 0.5, device: DeviceLike = None, dtype: DTypeLike = None)

A linear layer whose weights carry learnable exploration noise.

Parameters

in_featuresint
Widths, as Linear.
out_featuresint
Widths, as Linear.
biasbool= True
Whether to carry a noisy bias.
sigma_zerofloat= 0.5
σ0\sigma_0, the constant the noise scales are initialised from. The paper's value, and it notes it was not tuned.
deviceoptional= None
As elsewhere.
dtypeoptional= None
As elsewhere.

Attributes

weight_mu, weight_sigmaParameter
(out_features, in_features).
bias_mu, bias_sigmaParameter or None
(out_features,).
epsilon_in, epsilon_outTensor
The current noise sample, held as buffers so that a forward pass — and, for an agent, a whole episode — sees one fixed network. Refreshed by resample.

Raises

ValueError
If sigma_zero is not positive.

Notes

Reference: Fortunato et al., "Noisy Networks for Exploration", ICLR, 2018 (arXiv:1706.10295).

y=(μw+σw⊙εw) x+(μb+σb⊙εb)y = (\mu^w + \sigma^w \odot \varepsilon^w)\,x + (\mu^b + \sigma^b \odot \varepsilon^b)

with factorised noise, so the layer draws in_features + out_features random numbers rather than their product.

Initialised as the paper's factorised case specifies: "each element μi,j\mu_{i,j} was initialised by a sample from an independent uniform distribution U[−1/p,+1/p]U[-1/\sqrt{p}, +1/\sqrt{p}] and each element σi,j\sigma_{i,j} was initialised to a constant σ0/p\sigma_0/\sqrt{p}", with pp the input width.

The noise is a buffer, not a per-call draw. Redrawing inside forward would make two calls on the same input two different networks, which breaks both the Monte-Carlo gradient the paper derives — one sample per optimisation step, its equation 13 — and any agent that needs a fixed policy for an episode. Call resample when a new sample is wanted. In eval() the noise is not used at all, so a deterministic policy is just the mean network.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> layer = nn.NoisyLinear(4, 3)
>>> layer(lucid.zeros((2, 4))).shape
(2, 3)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(in_features: int, out_features: int, bias: bool = True, sigma_zero: float = 0.5, device: DeviceLike = None, dtype: DTypeLike = None)

Initialise the layer. See the class docstring for parameters.

Properties

2
prop

noise_in

→Tensor
noise_in: Tensor

The input noise sample, narrowed from the buffer registry.

prop

noise_out

→Tensor
noise_out: Tensor

The output noise sample, narrowed from the buffer registry.

Instance methods

3
fn

forward

→Tensor
forward(x: Tensor)

Apply the layer — noisy while training, the mean network in eval.

Parameters

(*, in_features).

Returns

Tensor

(*, out_features).

fn

resample

→None
resample()

Draw a fresh noise sample — one per optimisation step, or episode.

Notes

Writes the buffers in place with [:]. Assigning .data writes to a copy and silently does nothing, which is the mistake this codebase has made before.

fn

reset_parameters

→None
reset_parameters()

The paper's factorised initialisation.

mu ~ U[-1/sqrt(p), 1/sqrt(p)] and sigma = sigma_zero / sqrt(p), with p the input width.