Trainer cannot handle 1d tensor when return results from test_epoch_end #5979
-
🐛 BugWhen 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) ValueError: only one element tensors can be converted to Python scalars Please reproduce using the BoringModelTo ReproduceTo 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 behaviordef 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
Additional context |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Hi You should see somewhere a warning:
Only scalar tensors are supported. |
Beta Was this translation helpful? Give feedback.
-
I think returning from testing_epoch_end is a very useful functionality when you want to do any post-processing with the results. |
Beta Was this translation helpful? Give feedback.
-
Because for example in multi gpu mode, if we would allow the user to return, then we're missing the information what to do with the data, how the data is collected and synced, or reduced or whatever. |
Beta Was this translation helpful? Give feedback.
-
Thank for the explaination. I will try the |
Beta Was this translation helpful? Give feedback.
-
Sorry, it's not called "write", the warning seems to have the wrong name. There is currently |
Beta Was this translation helpful? Give feedback.
-
Hey @lzrpotato, From 1.2, Trainer and Lightning will have a predict function. It is still in Best, |
Beta Was this translation helpful? Give feedback.
Because for example in multi gpu mode, if we would allow the user to return, then we're missing the information what to do with the data, how the data is collected and synced, or reduced or whatever.
The logging api offers reduction and sync, by specifying the custom arguments how to do so.
On the other hand,
self.write
offers a way to collect all results.There will also be a prediction api in 1.2. #5752
cc @tchaton