我试图运行以下代码:
l = torch.tensor([0, 1, 1, 1], requires_grad=False)
r = torch.rand(4, 2)
torch.nn.CrossEntropyLoss(r, l)
我的错误是:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
我在这里看了看:Bool value of Tensor with more than one value is ambiguous in Pytorch,但不明白答案。
为了运行代码,我需要更改什么?
发布于 2022-06-21 19:38:35
您正在操作的对象torch.nn.CrossEntropyLoss
是一个PyTorch模块类,而不是一个函数。
因此,您应该事先取消它:
>>> ce_loss = nn.CrossEntropyLoss()
>>> cel_loss(r, l)
或者使用函数接口,即torch.nn.functional.cross_entropy
>>> F.cross_entropy(r, l)
https://stackoverflow.com/questions/72706218
复制相似问题