NormalParamWrapper¶
- class torchrl.modules.NormalParamWrapper(operator: Module, scale_mapping: str = 'biased_softplus_1.0', scale_lb: Number = 0.0001)[源代码]¶
正态分布参数的包装器。
- 参数:
operator (nn.Module) – 其输出将在位置和尺度参数中转换的运算符
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 >>> 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()