PyTorch 中负对数似然损失函数的约简参数的直观解释是什么?该参数可以采用“平均值”或“总和”等值。它是对批处理的元素求和吗?
torch.nn.functional.nll_loss(outputs.mean(0), target, reduction="sum")
最佳答案
来自文档:
指定应用于输出的缩减:'none' | '意思' | '和'。 'none':不应用减少,'mean':输出的总和将除以输出中的元素数量,'sum':输出将被求和。注意:size_average和reduce正在被弃用,同时,指定这两个参数中的任何一个都会覆盖reduction。默认值:“平均值”
如果不使用,输出将与批量大小相同,
如果使用平均值,则为平均值(总和除以批处理)
如果使用 sum,它将是所有元素的总和。
您还可以使用以下代码验证这一点:
import torch
logit = torch.rand(100,10)
target = torch.randint(10, size=(100,))
m = torch.nn.functional.nll_loss(logit, target)
s = torch.nn.functional.nll_loss(logit, target, reduction="sum")
l = torch.nn.functional.nll_loss(logit, target, reduction="none")
print(torch.abs(m-s/100))
print(torch.abs(l.mean()-m))
输出应该为 0 或非常接近 0。
关于python - 负对数似然的减少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415707/