OneHotCategorical¶
- class torchrl.modules.OneHotCategorical(logits: Optional[Tensor] = None, probs: Optional[Tensor] = None, grad_method: ReparamGradientStrategy = ReparamGradientStrategy.PassThrough, **kwargs)[source]¶
One-hot 分类分布。
此类行为与 torch.distributions.Categorical 完全相同,除了它读取和生成离散张量的一次性编码。
- 参数:
logits (torch.Tensor) – 事件对数概率(未归一化)
probs (torch.Tensor) – 事件概率
grad_method (ReparamGradientStrategy, 可选) –
收集重参数化样本的策略。
ReparamGradientStrategy.PassThrough
将使用 softmax 估值的对数概率作为样本梯度的代理来计算样本梯度。by using the softmax valued log-probability as a proxy to the samples gradients.
ReparamGradientStrategy.RelaxedOneHot
将使用torch.distributions.RelaxedOneHot
从分布中采样。
示例
>>> torch.manual_seed(0) >>> logits = torch.randn(4) >>> dist = OneHotCategorical(logits=logits) >>> print(dist.rsample((3,))) tensor([[1., 0., 0., 0.], [0., 0., 0., 1.], [1., 0., 0., 0.]])