<class ‘torch.distributions.categorical.Categorical‘>中属性probs和logits的计算方式

distribution = Categorical(probs: torch.Size([2]))的
probs属性是通过代码distribution = Categorical(…)传入的张量,
logits属性是对probs值求的以e为底的对数。

举个例子:
在这里插入图片描述

  1. output是张量:tensor([-0.0496, -0.0827], grad_fn=<AddBackward0>)
  2. 对output做softmax(也就是把-0.0496和-0.0827分别作为z1和z2代入下式)得到:tensor([0.5083, 0.4917], grad_fn=<SoftmaxBackward0>)
    在这里插入图片描述
  3. 将softmax结果用于初始化Categorical类对象,得到的对象属性分别为
    probs = tensor([0.5083, 0.4917], grad_fn=<DivBackward0>)
    logits = tensor([-0.6767, -0.7099], grad_fn=<LogBackward0>)
    其中,ln(0.5083)=-0.6767,ln(0.4917)=-0.7099

另一个例子:
在这里插入图片描述

更多资料:
https://blog.csdn.net/ldy007714/article/details/126143903
https://blog.csdn.net/HaoZiHuang/article/details/126356668

官方链接:
说明:
https://pytorch.org/docs/stable/distributions.html?highlight=categorical#torch.distributions.categorical.Categorical
源码:
https://pytorch.org/docs/stable/_modules/torch/distributions/categorical.html#Categorical

部分函数的chatGPT解释:
entropy函数:
在这里插入图片描述

log_prob函数:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值