Pytorch 中具有多个值的 Tensor 的 Bool 值是不明确的

问题描述 投票:0回答:5

我想在pytorch中创建一个模型,但我不能 计算损失。 它总是返回 Tensor 的 Bool 值以及更多 一个值不明确 实际上,我运行示例代码,它有效。

loss = CrossEntropyLoss()
input = torch.randn(8, 5)
input
target = torch.empty(8,dtype=torch.long).random_(5)
target
output = loss(input, target)

这是我的代码,

################################################################################
##
##
import torch
from torch.nn import Conv2d, MaxPool2d, Linear, CrossEntropyLoss, MultiLabelSoftMarginLoss
from torch.nn.functional import relu, conv2d, max_pool2d, linear, softmax
from torch.optim import adadelta
##
##
##  Train
Train = {}
Train["Image"]    = torch.rand(2000, 3, 76, 76)
Train["Variable"] = torch.rand(2000, 6)
Train["Label"] = torch.empty(2000, dtype=torch.long).random_(2)
##
##
##  Valid
Valid = {}
Valid["Image"]    = torch.rand(150, 3, 76, 76)
Valid["Variable"] = torch.rand(150, 6)
Valid["Label"]    = torch.empty(150, dtype=torch.long).random_(2)
################################################################################
##
##
##  Model
ImageTerm    = Train["Image"]
VariableTerm = Train["Variable"]
Pip = Conv2d(in_channels=3, out_channels=32, kernel_size=(3,3), stride=1, padding=0)(ImageTerm)
Pip = MaxPool2d(kernel_size=(2,2), stride=None, padding=0)(Pip)
Pip = Conv2d(in_channels=32, out_channels=64, kernel_size=(3,3), stride=1, padding=0)(Pip)
Pip = MaxPool2d(kernel_size=(2,2), stride=None, padding=0)(Pip)
Pip = Pip.view(2000, -1)
Pip = torch.cat([Pip, VariableTerm], 1)
Pip = Linear(in_features=18502, out_features=1000 , bias=True)(Pip)
Pip = Linear(in_features=1000, out_features=2 , bias=True)(Pip)
##
##
##  Loss
Loss = CrossEntropyLoss(Pip, Train["Label"])

错误在于 Loss = CrossEntropyLoss(Pip, Train["Label"]), 谢谢。

python pytorch
5个回答
89
投票

在您的最小示例中,您创建了“CrossEntropyLoss”类的对象“loss”。该对象能够将您的损失计算为

loss(input, target)

但是,在实际代码中,您尝试创建对象“Loss”,同时将 Pip 和标签传递给“CrossEntropyLoss”类构造函数。 相反,请尝试以下操作:

loss = CrossEntropyLoss()
loss(Pip, Train["Label"])

编辑(错误消息的解释):当您尝试将张量转换为布尔值时,会出现错误消息

Bool value of Tensor with more than one value is ambiguous
。当将张量传递给 if 条件时,最常发生这种情况,例如

input = torch.randn(8, 5)
if input:
    some_code()

CrossEntropyLoss
类构造函数的第二个参数需要一个布尔值。因此,在该行中

Loss = CrossEntropyLoss(Pip, Train["Label"])

构造函数会在某个时刻尝试使用传递的张量

Train["Label"]
作为布尔值,这会抛出上述错误消息。


27
投票

您不能直接使用类

CrossEntropyLoss
。你应该在使用这个类之前实例化它。

原代码:

loss = CrossEntropyLoss(Pip, Train["Label"])

应替换为:

loss = CrossEntropyLoss()
loss(Pip, Train["Label"])

13
投票

首先实例化损失

L = CrossEntropyLoss()

然后计算损失

L(y_pred, y_true)

这将修复错误。


0
投票

对我来说,我犯了一个愚蠢的错误,忘记使用括号作为

loss_function = nn.BCEWithLogitsLoss
print(loss_function)

将打印:

<class 'torch.nn.modules.loss.BCEWithLogitsLoss'>

同时

loss_function = nn.BCEWithLogitsLoss()
print(loss_function)

将打印:

BCEWithLogitsLoss()

后者显示损失函数通过其实例正确调用,而不是调用 。这是因为当您使用 loss_function_class(predictions, Targets) 时,预测和目标是具有多个值的张量。然而,loss_function_class 只是类的引用,而不是实例化的对象。


-2
投票

如果您由于 pyplot 无法正确显示张量图像而进入此页面,请使用

plt.imshow()
而不是
plt.show()

例如,而不是

plt.show(images[0].permute(1,2,0))

使用

plt.imshow(images[0].permute(1,2,0))
© www.soinside.com 2019 - 2024. All rights reserved.