Skip to content

Trainer cannot handle 1d tensor when return results from test_epoch_end #5972

@zhiruiluo

Description

@zhiruiluo

🐛 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedOpen to be worked onquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions