distribution = Categorical(probs: torch.Size([2]))的
probs属性是通过代码distribution = Categorical(…)传入的张量,
logits属性是对probs值求的以e为底的对数。
举个例子:
- output是张量:tensor([-0.0496, -0.0827], grad_fn=<AddBackward0>)
- 对output做softmax(也就是把-0.0496和-0.0827分别作为z1和z2代入下式)得到:tensor([0.5083, 0.4917], grad_fn=<SoftmaxBackward0>)
- 将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函数: