tensordict.nn.distributions.AddStateIndependentNormalScale¶
- class tensordict.nn.distributions.AddStateIndependentNormalScale(scale_shape: Union[Size, int, tuple], scale_mapping: str = 'exp', scale_lb: Number = 0.0001)¶
一个添加可训练状态无关尺度参数的 nn.Module。
尺度参数使用指定的
scale_mapping
映射到正值。- 参数:
scale_mapping (str, 可选) – 用于 std 的正映射函数。默认值为“biased_softplus_1.0”(即带偏差的 softplus 映射,使得 fn(0.0) = 1.0)选项:“softplus”、“exp”、“relu”、“biased_softplus_1”;
scale_lb (Number, 可选) – 方差可以取的最小值。默认为 1e-4。
示例
>>> from torch import nn >>> import torch >>> num_outputs = 4 >>> module = nn.Linear(3, num_outputs) >>> module_normal = AddStateIndependentNormalScale(num_outputs) >>> tensor = torch.randn(3) >>> loc, scale = module_normal(module(tensor)) >>> print(loc.shape, scale.shape) torch.Size([4]) torch.Size([4]) >>> assert (scale > 0).all() >>> # with modules that return more than one tensor >>> module = nn.LSTM(3, num_outputs) >>> module_normal = AddStateIndependentNormalScale(num_outputs) >>> tensor = torch.randn(4, 2, 3) >>> loc, scale, others = module_normal(*module(tensor)) >>> print(loc.shape, scale.shape) torch.Size([4, 2, 4]) torch.Size([4, 2, 4]) >>> assert (scale > 0).all()