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, optional) – 與 std 搭配使用的正向映射函數。預設為
"biased_softplus_1.0"
(即帶有偏差的 softplus 映射,使得 fn(0.0) = 1.0) 。可選值:"softplus"
、"exp"
、"relu"
、"biased_softplus_1"
。scale_lb (Number, optional) – 變異數可取的最小值。預設為
1e-4
。device ( torch.device, optional) – 模組的裝置。
make_param ( bool, optional) – scale 是否應為參數 (
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()