-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Closed
Labels
help wantedOpen to be worked onOpen to be worked onquestionFurther information is requestedFurther information is requested
Description
🐛 Bug
When trainer run_test() called, the results from test cannot properly handle a 1D tensor in the results dictionary.
Such error will happen:
/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/trainer.py in run_test(self)
708 for k, v in result.items():
709 if isinstance(v, torch.Tensor):
--> 710 result[k] = v.cpu().item()
711
712 return eval_loop_results
ValueError: only one element tensors can be converted to Python scalars
Please reproduce using the BoringModel
To Reproduce
To reproduce with BoringModel, only need to replace the test_epoch_end.
def test_epoch_end(self, outputs) -> None:
torch.stack([x["y"] for x in outputs]).mean()
f1_score = torch.tensor([1,1,1,1])
return {'f1_score': f1_score}Expected behavior
def run_test(self):
# remove the tensors from the eval results
for i, result in enumerate(eval_loop_results):
if isinstance(result, dict):
for k, v in result.items():
if isinstance(v, torch.Tensor):
# should check if you can call .item()
result[k] = v.cpu().item()Environment
- PyTorch Version (e.g., 1.0): 1.1.8
Additional context
awaelchli
Metadata
Metadata
Assignees
Labels
help wantedOpen to be worked onOpen to be worked onquestionFurther information is requestedFurther information is requested