NoisyLinear#
- class torchrl.modules.NoisyLinear(in_features: int, out_features: int, bias: bool = True, device: device | str | int | None = None, dtype: dtype | None = None, std_init: float = 0.5, use_exploration_type: bool | None = True)[source]#
Noisy Linear Layer.
Presented in “Noisy Networks for Exploration” (Fortunato et al., 2017), https://arxiv.org/abs/1706.10295v3
A Noisy Linear Layer is a linear layer with parametric noise added to the weights. This induced stochasticity can be used in RL networks for the agent’s policy to aid efficient exploration. The parameters of the noise are learned with gradient descent along with any other remaining network weights. Factorized Gaussian noise is the type of noise usually employed.
Note
The noise is controlled by the exploration mode set via
set_exploration_type(). When exploration type isRANDOM, noise is added to the weights. When exploration type isDETERMINISTIC,MODE, orMEAN, only the mean weights are used.This behavior is controlled by the
use_exploration_typeargument. When set toTrue, the exploration type is used. When set toFalse, the legacy behavior of usingself.training(i.e.,model.train()/model.eval()) is used instead.Note
Factorized noise is sampled only in
reset_noise(). The forward pass does not resample. The sameweight_epsilon/bias_epsilonbuffers are reused until the caller resamples them withlayer.reset_noise()ormodule.apply(reset_noise).The paper samples a new set of parameters after every optimization step (section 3.1). Callers that want that cadence should apply
reset_noise()after each optimizer step:module.apply(reset_noise)
make_trainer()uses a coarser cadence: whencfg.noisyis set it registersloss_module.apply(reset_noise)on the trainer’spre_optim_stepshook, which runs once peroptim_steps()call, not after every inner optimizer step, and only onloss_module. Do not resample on every forward: that would change the behavior of every NoisyNet user, including data collection, where a fixed sample of the noisy weights is intended.- Parameters:
in_features (int) – input features dimension
out_features (int) – out features dimension
bias (bool, optional) – if
True, a bias term will be added to the matrix multiplication: Ax + b. Defaults toTruedevice (DEVICE_TYPING, optional) – device of the layer. Defaults to
"cpu"dtype (torch.dtype, optional) – dtype of the parameters. Defaults to
None(default pytorch dtype)std_init (scalar, optional) – initial value of the Gaussian standard deviation before optimization. Defaults to
0.5as per the original paper.use_exploration_type (bool or None, optional) – if
True, noise is controlled byexploration_type(). IfFalse, noise is controlled byself.training(legacy behavior). IfNone, it is treated asTrue. Defaults toTrue.
Examples
>>> import torch >>> from torch import nn >>> from torchrl.envs import ExplorationType, set_exploration_type >>> from torchrl.modules import NoisyLinear, reset_noise >>> _ = torch.manual_seed(0) >>> layer = NoisyLinear(4, 2) >>> x = torch.ones(4) >>> with set_exploration_type(ExplorationType.RANDOM): ... y0 = layer(x) ... y1 = layer(x) >>> torch.equal(y0, y1) True >>> with set_exploration_type(ExplorationType.RANDOM): ... layer.reset_noise() ... y2 = layer(x) >>> torch.equal(y0, y2) False >>> net = nn.Sequential(NoisyLinear(4, 8), nn.ReLU(), NoisyLinear(8, 2)) >>> _ = net.apply(reset_noise)