Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken clone method for classification metrics #1250

Merged
merged 9 commits into from
Oct 6, 2022
3 changes: 3 additions & 0 deletions src/torchmetrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,9 @@ def __pos__(self) -> "Metric":
def __getitem__(self, idx: int) -> "Metric":
return CompositionalMetric(lambda x: x[idx], self, None)

def __getnewargs__(self):
return (Metric.__str__(self),)


def _neg(x: Tensor) -> Tensor:
return -torch.abs(x)
Expand Down
5 changes: 5 additions & 0 deletions tests/unittests/helpers/testers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ def _class_test(
if check_scriptable:
torch.jit.script(metric)

# check that metric can be cloned
clone = metric.clone()
assert clone is not metric, "Clone is not a different object than the metric"
assert type(clone) == type(metric), "Type of clone did not match metric type"

# move to device
metric = metric.to(device)
preds = apply_to_collection(preds, Tensor, lambda x: x.to(device))
Expand Down