【发布时间】:2022-01-09 03:21:29
【问题描述】:
我正在尝试在 Pytorch 中为编码器-解码器模型手动计算 cross_entropy 损失。
我使用此处发布的代码来计算它:Cross Entropy in PyTorch
我更新了代码以丢弃填充标记 (-100)。最后的代码是这样的:
class compute_crossentropyloss_manual:
"""
y0 is the vector with shape (batch_size,C)
x shape is the same (batch_size), whose entries are integers from 0 to C-1
"""
def __init__(self, ignore_index=-100) -> None:
self.ignore_index=ignore_index
def __call__(self, y0, x):
loss = 0.
n_batch, n_class = y0.shape
# print(n_class)
for y1, x1 in zip(y0, x):
class_index = int(x1.item())
if class_index == self.ignore_index: # <------ I added this if-statement
continue
loss = loss + torch.log(torch.exp(y1[class_index])/(torch.exp(y1).sum()))
loss = - loss/n_batch
return loss
为了验证它是否正常工作,我在文本生成任务中对其进行了测试,并使用 pytorch.nn 实现并使用此代码计算了损失。
损失值不相同:
使用nn.CrossEntropyLoss:
使用上面链接中的代码:
我错过了什么吗?
我试图获取nn.CrossEntropyLoss 的源代码,但我无法获得。在此链接 nn/functional.py 第 2955 行,您将看到该函数指向另一个名为 torch._C._nn.cross_entropy_loss 的 cross_entropy 损失;我在 repo 中找不到这个函数。
编辑:
我注意到只有当我在黄金中有-100 令牌时才会出现差异。
演示示例:
y = torch.randint(1, 50, (100, 50), dtype=float)
x = torch.randint(1, 50, (100,))
x[40:] = -100
print(criterion(y, x).item())
print(criterion2(y, x).item())
> 25.55788695847976
> 10.223154783391905
当我们没有-100:
x[40:] = 30 # any positive number
print(criterion(y, x).item())
print(criterion2(y, x).item())
> 24.684453267596453
> 24.684453267596453
【问题讨论】:
-
torch._C是 C 源代码。所以你可以看看here -
谢谢@Chrispresso。我无法理解 C 中的任何内容。
标签: python pytorch loss-function cross-entropy