tensordict.nn.distributions.NormalParamExtractor¶
- class tensordict.nn.distributions.NormalParamExtractor(scale_mapping: str = 'biased_softplus_1.0', scale_lb: Number = 0.0001)¶
一個非參數的 nn.Module,將其輸入分割成 loc 和 scale 參數。
scale 參數使用指定的
scale_mapping
映射到正值。- 參數:
scale_mapping (str, optional) – 與 std 一起使用的正向映射函數。預設值 =
"biased_softplus_1.0"
(即具有偏差的 softplus 映射,使得 fn(0.0) = 1.0)選項:"softplus"
、"exp"
、"relu"
、"biased_softplus_1"
或"none"
(無映射)。 詳見mappings()
以取得更多資訊。scale_lb (Number, optional) – 變異數可以採用的最小值。預設值為 1e-4。
範例
>>> import torch >>> from tensordict.nn.distributions import NormalParamExtractor >>> from torch import nn >>> module = nn.Linear(3, 4) >>> normal_params = NormalParamExtractor() >>> tensor = torch.randn(3) >>> loc, scale = normal_params(module(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) >>> tensor = torch.randn(4, 2, 3) >>> loc, scale, others = normal_params(*module(tensor)) >>> print(loc.shape, scale.shape) torch.Size([4, 2, 2]) torch.Size([4, 2, 2]) >>> assert (scale > 0).all()