MaskedCategorical¶
- class torchrl.modules.MaskedCategorical(logits: Optional[Tensor] = None, probs: Optional[Tensor] = None, *, mask: Optional[Tensor] = None, indices: Optional[Tensor] = None, neg_inf: float = - inf, padding_value: Optional[int] = None)[source]¶
MaskedCategorical 分布。
参考: https://tensorflowcn.cn/agents/api_docs/python/tf_agents/distributions/masked/MaskedCategorical
- 参数:
logits (torch.Tensor) – 事件对数概率(未归一化)
probs (torch.Tensor) – 事件概率。如果提供此参数,则对应于被遮蔽项的概率将归零,并在其最后一个维度上重新归一化概率。
- 关键字参数:
mask (torch.Tensor) – 一个与
logits
/probs
同形的布尔掩码,其中False
条目是被遮蔽的。或者,如果sparse_mask
为 True,则它表示分布中有效索引的列表。与indices
互斥。indices (torch.Tensor) – 一个表示哪些动作必须考虑在内的密集索引张量。与
mask
互斥。neg_inf (
float
, optional) – 分配给无效(超出掩码范围)索引的对数概率值。默认为 -inf。padding_value – 掩码张量中的填充值。当 sparse_mask == True 时,将忽略 padding_value。
torch.manual_seed (>>>) –
torch.randn (>>> logits =) –
torch.tensor (>>> mask =) –
MaskedCategorical (>>> dist =) –
dist.sample (>>> sample =) –
print (>>>) –
tensor ([-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf]) –
print –
-1.0831, (tensor([-1.1203, -1.0928, -1.0831, -1.1203, -1.1203, -1.0831, -1.1203,) – -1.1203, -1.1203])
print –
tensor –
probabilities (>>> # 使用概率) –
torch.ones (>>> prob =) –
prob.sum() (>>> prob = prob /) –
torch.tensor –
MaskedCategorical –
print –
-2.1972, (tensor([ -inf, -2.1972, -2.1972, -2.1972, -2.1972, -2.1972, -2.1972,) – -2.1972, -2.1972])