Search code examples
pythonpytorch

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


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 ambiguous Actually, I run example code, it work.

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.


Solution

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

    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 = CrossEntropyLoss()
    loss(Pip, Train["Label"])
    

    Edit (explanation of the error message): The error Message Bool value of Tensor with more than one value is ambiguous appears when you try to cast a tensor into a bool value. This happens most commonly when passing the tensor to an if condition, e.g.

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

    The second argument of the CrossEntropyLoss class constructor expects a boolean. Thus, in the line

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

    the constructor will at some point try to use the passed tensor Train["Label"] as a boolean, which throws the mentioned error message.