OneHotCategorical¶
- 類 torchrl.modules.OneHotCategorical(logits: Optional[Tensor] = None, probs: Optional[Tensor] = None, grad_method: ReparamGradientStrategy = ReparamGradientStrategy.PassThrough, **kwargs)[source]¶
獨熱分類分佈。
此類行為與 torch.distributions.Categorical 完全一致,區別在於它讀取並生成離散張量的獨熱編碼。
- 引數:
logits (torch.Tensor) – 事件的對數機率(未歸一化)
probs (torch.Tensor) – 事件機率
grad_method (ReparamGradientStrategy, 可選) –
收集重引數化樣本的策略。
ReparamGradientStrategy.PassThrough將透過使用 softmax 值的對數機率作為樣本梯度的代理來計算樣本梯度。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.]])