-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from AlexImmer/running-metrics
Running metrics
- Loading branch information
Showing
7 changed files
with
175 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import torch | ||
from torch.nn import functional as F | ||
from torchmetrics import Metric | ||
|
||
|
||
class RunningNLLMetric(Metric): | ||
""" | ||
NLL metrics that | ||
Parameters | ||
---------- | ||
ignore_index: int, default = -100 | ||
which class label to ignore when computing the NLL loss | ||
""" | ||
def __init__(self, ignore_index=-100): | ||
super().__init__() | ||
self.add_state('nll_sum', default=torch.tensor(0.), dist_reduce_fx='sum') | ||
self.add_state('n_valid_labels', default=torch.tensor(0.), dist_reduce_fx='sum') | ||
self.ignore_index = ignore_index | ||
|
||
def update(self, probs: torch.Tensor, targets: torch.Tensor) -> None: | ||
""" | ||
Parameters | ||
---------- | ||
probs: torch.Tensor | ||
probability tensor of shape (..., n_classes) | ||
targets: torch.Tensor | ||
integer tensor of shape (...) | ||
""" | ||
probs = probs.view(-1, probs.shape[-1]) | ||
targets = targets.view(-1) | ||
|
||
self.nll_sum += F.nll_loss( | ||
probs.log(), targets, ignore_index=self.ignore_index, reduction='sum' | ||
) | ||
self.n_valid_labels += (targets != self.ignore_index).sum() | ||
|
||
def compute(self): | ||
return self.nll_sum / self.n_valid_labels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import torch | ||
from torch.nn import functional as F | ||
from laplace.utils import RunningNLLMetric | ||
import math | ||
|
||
|
||
def test_running_nll_metric(): | ||
metric = RunningNLLMetric() | ||
all_probs, all_targets = [], [] | ||
|
||
for _ in range(10): | ||
probs = torch.softmax(torch.randn(3, 5, 10), dim=-1) | ||
targets = torch.randint(10, size=(3, 5)) | ||
metric.update(probs, targets) | ||
all_probs.append(probs) | ||
all_targets.append(targets) | ||
|
||
all_probs, all_targets = torch.cat(all_probs, 0), torch.cat(all_targets, 0) | ||
|
||
nll_running = metric.compute().item() | ||
nll_offline = F.nll_loss(all_probs.log().flatten(end_dim=-2), all_targets.flatten()).item() | ||
|
||
assert math.isclose(nll_running, nll_offline, rel_tol=1e-7) | ||
|
||
|
||
def test_running_nll_metric_ignore_idx(): | ||
ignore_idx = -1232 | ||
metric = RunningNLLMetric(ignore_index=ignore_idx) | ||
all_probs, all_targets = [], [] | ||
|
||
for _ in range(10): | ||
probs = torch.softmax(torch.randn(3, 5, 10), dim=-1) | ||
targets = torch.randint(10, size=(3, 5)) | ||
mask = torch.FloatTensor(*targets.shape).uniform_() > 0.5 # ~50% zeros | ||
targets[mask] = ignore_idx # ~50% changed to ignore_idx | ||
all_probs.append(probs) | ||
all_targets.append(targets) | ||
metric.update(probs, targets) | ||
|
||
all_probs, all_targets = torch.cat(all_probs, 0), torch.cat(all_targets, 0) | ||
|
||
nll_running = metric.compute().item() | ||
nll_offline = F.nll_loss(all_probs.log().flatten(end_dim=-2), all_targets.flatten(), ignore_index=ignore_idx).item() | ||
|
||
print(nll_running, nll_offline) | ||
|
||
assert math.isclose(nll_running, nll_offline, rel_tol=1e-7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters