Python 在 Pytorch 中,具有多个值的 Tensor 的布尔值是不明确的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52946920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:13:29  来源:igfitidea点击:

Bool value of Tensor with more than one value is ambiguous in Pytorch

pythontorch

提问by Greg

I want to create a model in pytorch, but I can't compute the loss. It's always return Bool value of Tensor with more than one value is ambiguousActually, I run example code, it work.

我想在 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)

Here is my code,

这是我的代码,

################################################################################
##
##
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"])

The error is on Loss = CrossEntropyLoss(Pip, Train["Label"]), thanks.

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

回答by randomwalker

In your minimal example, you create an object "loss" of the class "CrossEntropyLoss". This object is able to compute your loss as

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

loss(input, target)

However, in your actual code, you try to create the object "Loss", while passing Pip and the labels to the "CrossEntropyLoss" class constructor. Instead, try the following:

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

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