torch.distributions.categorical(probs)

class torch.distributions.categorical(probs)

其作用是创建以参数probs为标准的类别分布,样本是来自“0,...,K-1”的整数,K是probs参数的长度。也就是说,按照probs的概率,在相应的位置进行采样,采样返回的是该位置的整数索引。

如果probs是长度为K的一维列表,则每个元素是对该索引处的类进行采样的相对概率。

如果probs是二维的,它被视为一批概率向量

例如:

probs = torch.FloatTensor([0.9,0.2])
D = Categorical(probs)
print(D)                  # Categorical(probs: torch.Size([2]))
for i in range(5):
    print(D.sample())         
tensor(0)
tensor(0)
tensor(0)
tensor(0)
tensor(1)



probs = torch.FloatTensor([[0, 0.25, 0.25], [0.25, 0.25, 0.5]])
D = Categorical(probs)
for i in range(5):
    print(D.sample())

tensor([1, 1])
tensor([2, 0])
tensor([2, 2])
tensor([2, 2])
tensor([2, 2])
# 第0维第0个元素的权重是0,在其他元素被取完之前是不会被取到的。

 

 

 

 

 

  • 13
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值