You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MetricTracker.best_metric() function returns the step with the highest metric value instead of the best value itself.
To Reproduce
Please see colab for a full example and proposed solution.
Code sample
fromtorchmetricsimportAccuracy, MetricTrackerimporttorch_=torch.manual_seed(42)
tracker=MetricTracker(Accuracy(num_classes=10))
forepochinrange(5):
tracker.increment()
forbatch_idxinrange(5):
preds, target=torch.randint(10, (100,)), torch.randint(10, (100,))
tracker.update(preds, target)
best_acc, which_epoch=tracker.best_metric(return_step=True)
best_acc2=tracker.best_metric(return_step=False)
assertbest_acc==best_acc2# does not hold but shouldassertwhich_epoch==best_acc2# instead this condition holds but shouldn't
Expected behavior
Function best_metric without any parameters should return the best value:
In fact, even with return_step=True the order is swapped, the function should return:
returnidx.item(), best.item()
index first and best value second (source code), but in current version this function return best value first and index second (best_acc, which_epoch = ... from example).
It would be more readable and easier to understand and more prone to error.
If you agree with the proposal I can submit a follow-up pull request.
Proposed test case
Add the following test case:
# Assert that best_metric returns both index and valueval, idx=tracker.best_metric(return_step=True)
# Assert that best_metric returns just value without any parametersval2=tracker.best_metric()
assertval==val2
I think in order not to break backward compatibility this function should return the best value first and index second (as it shown in current examples). And probably the order should be placed in the documentation.
The text was updated successfully, but these errors were encountered:
🐛 Bug
MetricTracker.best_metric()
function returns the step with the highest metric value instead of the best value itself.To Reproduce
Please see colab for a full example and proposed solution.
Code sample
Expected behavior
Function
best_metric
without any parameters should return the best value:Environment
Does not depend on the version.
Additional context
Inconsistent behavior even with
return_step=True
In fact, even with
return_step=True
the order is swapped, the function should return:index first and best value second (source code), but in current version this function return best value first and index second (
best_acc, which_epoch = ...
from example).Proposed solution
Use a namedtuple (values, indices) (pytorch max function doc) instead of implicit unpacking.
Replace the line with the error
from torchmetrics source code
with:
It would be more readable and easier to understand and more prone to error.
If you agree with the proposal I can submit a follow-up pull request.
Proposed test case
Add the following test case:
to the current test.
Backward compatibility
I think in order not to break backward compatibility this function should return the best value first and index second (as it shown in current examples). And probably the order should be placed in the documentation.
The text was updated successfully, but these errors were encountered: