Skip to content

Commit

Permalink
Add test that functional metrics return the correct compute value
Browse files Browse the repository at this point in the history
  • Loading branch information
golmschenk committed Jul 13, 2024
1 parent a7bc954 commit 7c6e5fb
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/unit_tests/metrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
from torch.nn import MSELoss
from torchmetrics import MeanSquaredError

from qusi.internal.train_session import update_logging_metrics

Expand All @@ -12,3 +13,15 @@ def test_update_logging_metrics_for_functional_metrics():
metric = MSELoss()
update_logging_metrics(predicted_targets, targets, [metric], metric_totals)
assert metric_totals == expected_metric_totals


def test_update_logging_metrics_for_state_based_torchmetrics():
metric = MeanSquaredError()
value = metric(torch.tensor([1.]), torch.tensor([2.])) # Add some already stored metric value.
predicted_targets = torch.tensor([1.])
targets = torch.tensor([3.])
metric_totals = torch.tensor([0.])
expected_computed_metric_value = torch.tensor(2.5) # Average of 1 and 4.
update_logging_metrics(predicted_targets, targets, [metric], metric_totals)
computed_metric_value = metric.compute()
assert computed_metric_value == expected_computed_metric_value

0 comments on commit 7c6e5fb

Please sign in to comment.