TanhNormal¶
- class torchrl.modules.TanhNormal(loc: Tensor, scale: Tensor, upscale: torch.Tensor | numbers.Number = 5.0, low: torch.Tensor | numbers.Number = - 1.0, high: torch.Tensor | numbers.Number = 1.0, event_dims: int | None = None, tanh_loc: bool = False, safe_tanh: bool = True)[source]¶
Implements a TanhNormal distribution with location scaling.
Location scaling prevents the location to be “too far” from 0 when a
TanhTransformis applied, but ultimately leads to numerically unstable samples and poor gradient computation (e.g. gradient explosion). In practice, with location scaling the location is computed according to\[loc = tanh(loc / upscale) * upscale.\]- Parameters:
loc (torch.Tensor) – normal distribution location parameter
scale (torch.Tensor) – normal distribution sigma parameter (squared root of variance)
upscale (torch.Tensor or number) –
‘a’ scaling factor in the formula:
\[loc = tanh(loc / upscale) * upscale.\]low (torch.Tensor or number, optional) – minimum value of the distribution. Default is -1.0;
high (torch.Tensor or number, optional) – maximum value of the distribution. Default is 1.0;
event_dims (int, optional) – number of dimensions describing the action. Default is 1. Setting
event_dimsto0will result in a log-probability that has the same shape as the input,1will reduce (sum over) the last dimension,2the last two etc.tanh_loc (bool, optional) – if
True, the above formula is used for the location scaling, otherwise the raw value is kept. Default isFalse;safe_tanh (bool, optional) – if
True, the Tanh transform is done “safely”, to avoid numerical overflows. This will currently break withtorch.compile().
- property mean¶
Returns the mean of the distribution.
- property mode¶
Returns the mode of the distribution.
- property support¶
Returns a
Constraintobject representing this distribution’s support.