Skip to content

Commit

Permalink
Bugfix: reset should set clear _computed (#147)
Browse files Browse the repository at this point in the history
* Bugfix: reset should set clear _computed

* changelog

* Update CHANGELOG.md

Co-authored-by: Nicki Skafte <[email protected]>
  • Loading branch information
maximsch2 and SkafteNicki authored Mar 29, 2021
1 parent 3d08235 commit 19b77cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed when `_stable_1d_sort` to work when n >= N ([#6177](https://github.com/PyTorchLightning/pytorch-lightning/pull/6177))


- Fixed `_computed` attribute not being correctly reset ([#147](https://github.com/PyTorchLightning/metrics/pull/147))


## [0.2.0] - 2021-03-12


Expand Down
9 changes: 9 additions & 0 deletions tests/bases/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class B(DummyListMetric):
assert isinstance(b.x, list) and len(b.x) == 0


def test_reset_compute():
a = DummyMetricSum()
assert a.x == 0
a.update(tensor(5))
assert a.compute() == 5
a.reset()
assert a.compute() == 0


def test_update():

class A(DummyMetric):
Expand Down
2 changes: 2 additions & 0 deletions torchmetrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ def reset(self):
"""
This method automatically resets the metric state variables to their default value.
"""
self._computed = None

for attr, default in self._defaults.items():
current_val = getattr(self, attr)
if isinstance(default, Tensor):
Expand Down

0 comments on commit 19b77cc

Please sign in to comment.