快捷方式

tensordict.nn.distributions.NormalParamExtractor

tensordict.nn.distributions.NormalParamExtractor(scale_mapping: str = 'biased_softplus_1.0', scale_lb: Number = 0.0001)

一个非参数化的 nn.Module,它将其输入拆分为 loc 和 scale 参数。

使用指定的 scale_mapping 将 scale 参数映射到正值。

参数:
  • scale_mapping (str, 可选) – 用于标准差的正向映射函数。默认值 = "biased_softplus_1.0" (即带有偏置的 softplus 映射,使得 fn(0.0) = 1.0) 选项:"softplus""exp""relu""biased_softplus_1""none" (无映射)。有关更多详细信息,请参阅 mappings()

  • scale_lb (Number, 可选) – 方差可以取的最小值。默认值为 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()

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源