tensordict.nn.distributions.AddStateIndependentNormalScale¶
- class tensordict.nn.distributions.AddStateIndependentNormalScale(scale_shape: Optional[Union[Size, int, tuple]] = None, *, scale_mapping: str = 'exp', scale_lb: Number = 0.0001, device: Optional[device] = None, make_param: bool = True)¶
一个 nn.Module,用于添加可训练的状态独立尺度参数。
尺度参数使用指定的
scale_mapping
映射到正值。- 参数:
scale_shape (torch.Size 或 等效项, 可选) – 尺度参数的形状。默认为
torch.Size(())
。- 关键词参数:
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
。device (torch.device, 可选) – 模块的设备。
make_param (bool, 可选) – 尺度应该是参数 (
True
) 还是缓冲区 (False
)。默认为True
。
示例
>>> 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()