为什么张量和pytorch crossentropy损失返回同一示例的不同值
我尝试获得Tensorflow和Pytorch Crossentropyloss,但它返回不同的值,我不知道为什么。请找到以下代码和结果。感谢您的投入和帮助。
import tensorflow as tf
import numpy as np
y_true = [3, 3, 1]
y_pred = [
[0.3377, 0.4867, 0.8842, 0.0854, 0.2147],
[0.4853, 0.0468, 0.6769, 0.5482, 0.1570],
[0.0976, 0.9899, 0.6903, 0.0828, 0.0647]
]
scce3 = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.AUTO)
loss3 = scce3(y_true, y_pred).numpy()
print(loss3)
上述结果是:1.69
Pytorch损失:
from torch import nn
import torch
loss = nn.CrossEntropyLoss()
y_true = torch.Tensor([3, 3, 1]).long()
y_pred = torch.Tensor([
[0.3377, 0.4867, 0.8842, 0.0854, 0.2147],
[0.4853, 0.0468, 0.6769, 0.5482, 0.1570],
[0.0976, 0.9899, 0.6903, 0.0828, 0.0647]
])
loss2 = loss(y_pred, y_true)
print(loss2)
上述损失值为:1.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tensorflow的CrossentRopy期望概率为输入(即
tf.nn.softmax
操作之后的值),而Pytorch的Crossentropyloss期望原始输入或更常见的logitts。如果使用SoftMax操作,则值应该相同:通常建议使用原始输入(logits),因为 logsumexp 数值稳定性的技巧。如果您使用的是TensorFlow,建议您使用 函数,或其。编辑:
sparsecatecoricalCrossentRopy
类还具有一个关键字参数from_logits = false
,可以将其设置为true> True
,以达到相同的效果。Tensorflow's CrossEntropy expects probabilities as inputs (i.e. values after a
tf.nn.softmax
operation), whereas PyTorch's CrossEntropyLoss expects raw inputs, or more commonly named, logits. If you use the softmax operation, the values should be the same:Using the raw inputs (logits) is usually advised due to the LogSumExp trick for numerical stability. If you are using Tensorflow, I'd suggest using the
tf.nn.softmax_cross_entropy_with_logits
function instead, or its sparse counterpart. Edit: TheSparseCategoricalCrossentropy
class also has a keyword argumentfrom_logits=False
that can be set toTrue
to the same effect.