softmax_cross_entropy_with_logits 的 PyTorch 等价 [英] PyTorch equivalence for softmax_cross_entropy_with_logits
问题描述
我想知道 TensorFlow 的 softmax_cross_entropy_with_logits
是否有等效的 PyTorch 损失函数?
I was wondering is there an equivalent PyTorch loss function for TensorFlow's softmax_cross_entropy_with_logits
?
推荐答案
TensorFlow 的
softmax_cross_entropy_with_logits
是否有等效的 PyTorch 损失函数?
is there an equivalent PyTorch loss function for TensorFlow's
softmax_cross_entropy_with_logits
?
torch.nn.functional.cross_entropy
这将 logits 作为输入(在内部执行 log_softmax
).这里的logits"只是一些不是概率的值(即不一定在区间 [0,1]
中).
torch.nn.functional.cross_entropy
This takes logits as inputs (performing log_softmax
internally). Here "logits" are just some values that are not probabilities (i.e. not necessarily in the interval [0,1]
).
但是,logits 也是将转换为概率的值.如果您考虑 tensorflow 函数的名称,您就会明白它是 pleonasm(因为 with_logits
部分假设 softmax
将被调用).
But, logits are also the values that will be converted to probabilities.
If you consider the name of the tensorflow function you will understand it is pleonasm (since the with_logits
part assumes softmax
will be called).
在 PyTorch 实现中是这样的:
In the PyTorch implementation looks like this:
loss = F.cross_entropy(x, target)
相当于:
lp = F.log_softmax(x, dim=-1)
loss = F.nll_loss(lp, target)
它不是 F.binary_cross_entropy_with_logits
因为这个函数假设多标签分类:
It is not F.binary_cross_entropy_with_logits
because this function assumes multi label classification:
F.sigmoid + F.binary_cross_entropy = F.binary_cross_entropy_with_logits
它不是 torch.nn.functional.nll_loss
也不是因为这个函数采用对数概率(在 log_softmax()
之后)而不是 logits.
It is not torch.nn.functional.nll_loss
either because this function takes log-probabilities (after log_softmax()
) not logits.
这篇关于softmax_cross_entropy_with_logits 的 PyTorch 等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!