PyTorch RuntimeError: Boolean value of Tensor with more than one value is ambiguous - How to Fix
The error you are encountering is because you are using a tensor (top1_probs) in a comparison operation (<) with a scalar value (0.6). The result of this comparison operation is a tensor of boolean values, but the boolean value of a tensor with more than one value is ambiguous.
To fix this issue, you need to compare each element of top1_probs individually with 0.6. You can use the torch.any function to check if any element of top1_probs is less than 0.6.
Here's the modified code:
for pred, prob in zip(predicted_list, top1_probs):
if pred in [0, 1, 2, 3, 4, 5] or torch.any(prob < 0.6):
preds.append(4)
else:
preds.append(pred)
This code uses the zip function to iterate over both predicted_list and top1_probs simultaneously. Inside the loop, each prob value is compared individually with 0.6 using torch.any. If any element of prob is less than 0.6, the condition is satisfied and preds is appended with 4. Otherwise, preds is appended with the original pred value.
Make sure to import the torch module at the beginning of your code:
import torch
原文地址: https://www.cveoy.top/t/topic/quIs 著作权归作者所有。请勿转载和采集!