Shortcuts

NormalParamWrapper

class torchrl.modules.NormalParamWrapper(operator: Module, scale_mapping: str = 'biased_softplus_1.0', scale_lb: Number = 0.0001)[source]

A wrapper for normal distribution parameters.

Parameters:
  • operator (nn.Module) – operator whose output will be transformed_in in location and scale parameters

  • scale_mapping (str, optional) – positive mapping function to be used with the std. default = “biased_softplus_1.0” (i.e. softplus map with bias such that fn(0.0) = 1.0) choices: “softplus”, “exp”, “relu”, “biased_softplus_1”;

  • scale_lb (Number, optional) – The minimum value that the variance can take. Default is 1e-4.

Examples

>>> from torch import nn
>>> import torch
>>> module = nn.Linear(3, 4)
>>> module_normal = NormalParamWrapper(module)
>>> tensor = torch.randn(3)
>>> loc, scale = module_normal(tensor)
>>> print(loc.shape, scale.shape)
torch.Size([2]) torch.Size([2])
>>> assert (scale > 0).all()
>>> # with modules that return more than one tensor
>>> module = nn.LSTM(3, 4)
>>> module_normal = NormalParamWrapper(module)
>>> tensor = torch.randn(4, 2, 3)
>>> loc, scale, others = module_normal(tensor)
>>> print(loc.shape, scale.shape)
torch.Size([4, 2, 2]) torch.Size([4, 2, 2])
>>> assert (scale > 0).all()
forward(*tensors: Tensor) Tuple[Tensor][source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources