Pytorch 对数归一化计算

问题描述 投票:0回答:1

我不明白 Pytorch 如何进行日志规范化并且四处搜索我找不到很好的示例/解释。有人可以解释一下吗?

例如

input_tensor =torch.tensor([0.6,0.4])
m = dist.Categorical(logits = input_tensor)
print(np.log(input_tensor))
print(m.logits)

gives: 
tensor([-0.5981, -0.7981])
tensor([-0.5108, -0.9163])

我的概率总和为 1,因此无需标准化,但 Pytorch 正在转换我的输入。

Pytorch 的文档说:

logits 参数将被解释为非标准化对数概率,因此可以是任何实数。它同样会被标准化,以便沿最后一个维度得到的概率总和为 1。 logits 将返回这个标准化值。

https://pytorch.org/docs/stable/distributions.html

python pytorch probability
1个回答
0
投票

np.log
函数通过表达式计算值

,

因此,如果输入值为

[0.6, 0.4]
,则结果输出将为
tensor([-0.5108, -0.9163])

处理

Categorical
时,您可以在此处探索 PyTorch
categorical.py
源代码:https://github.com/pytorch/pytorch/blob/main/torch/distributions/categorical.py)。

你会发现一行代码:

self.logits = logits - logits.logsumexp(dim=-1, keepdim=True)

将您的输入张量视为

[0.6, 0.4]
,因此
logits.logsumexp(dim=-1, keepdim=True)
的值为

因此,由于

logits
是您的输入张量,这就是为什么
m.logits
产生
tensor([-0.5981, -0.7981])

© www.soinside.com 2019 - 2024. All rights reserved.