Skip to content
fn

noisy_linear

→Tensor
noisy_linear(x: Tensor, weight_mu: Tensor, weight_sigma: Tensor, bias_mu: Tensor | None = None, bias_sigma: Tensor | None = None, epsilon_in: Tensor | None = None, epsilon_out: Tensor | None = None)

A linear layer whose parameters carry learnable noise.

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

Parameters

Input, (*, in_features).
weight_muTensor
Both (out_features, in_features) — the mean weight and the scale of the noise on it.
weight_sigmaTensor
Both (out_features, in_features) — the mean weight and the scale of the noise on it.
bias_muTensor or None= None
Both (out_features,), or None for an unbiased layer.
bias_sigmaTensor or None= None
Both (out_features,), or None for an unbiased layer.
epsilon_inTensor or None= None
(in_features,) unit Gaussian samples. None draws them.
epsilon_outTensor or None= None
(out_features,) unit Gaussian samples. None draws them.

Returns

Tensor

(*, out_features).

Raises

ValueError
If exactly one of bias_mu and bias_sigma is given.

Notes

Reference: Fortunato, Azar, Piot, Menick, Hessel, Osband, Graves, Mnih, Munos, Hassabis, Pietquin, Blundell, and Legg, "Noisy Networks for Exploration", ICLR, 2018 (arXiv:1706.10295), equations 9-11.

Factorised noise, which is the variant the paper uses for the single-threaded agents: rather than one sample per weight, it draws pp for the inputs and qq for the outputs and forms

εi,jw=f(εi)f(εj),εjb=f(εj),\varepsilon^w_{i,j} = f(\varepsilon_i) f(\varepsilon_j), \qquad \varepsilon^b_j = f(\varepsilon_j),

so a layer costs p+qp + q random numbers instead of pqpq. The paper's stated reason is compute time, and it is the reason to prefer this form here too.

The two noise vectors are arguments rather than always drawn, because a sample has to be held fixed across a whole forward pass — and, for an agent, often across a whole episode. Drawing inside the function would make every call a different network.

Gradients reach σ\sigma through the noise, which is the point: the scale of the exploration is learned rather than annealed.

Examples

>>> import lucid
>>> from lucid.nn.functional import noisy_linear
>>> x = lucid.zeros((2, 4))
>>> mu, sigma = lucid.zeros((3, 4)), lucid.ones((3, 4))
>>> noisy_linear(x, mu, sigma).shape
(2, 3)

See Also

  • lucid.nn.NoisyLinear—nn.Module wrapper that owns the parameters.
  • linear—the deterministic layer this perturbs.

Used by 2