Bool value of Tensor with more than one value is ambiguous in Pytorch
You can not use the class CrossEntropyLoss
directly. You should instantiate this class before using it.
original code:
loss = CrossEntropyLoss(Pip, Train["Label"])
should be replaced by:
loss = CrossEntropyLoss()
loss(Pip, Train["Label"])
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.
First Instantiate loss
L = CrossEntropyLoss()
Then compute loss
L(y_pred, y_true)
This will fix the error.