neuraltrain.metrics.metrics.GroupedMetric¶
- class neuraltrain.metrics.metrics.GroupedMetric(metric_name: str, kwargs: dict[str, Any] | None = None, reduction: Literal['mean', 'std'] | None = None)[source][source]¶
A wrapper around a torchmetrics.Metric that allows for computing metrics per group.
With
reductionset,computereduces the per-group values to a scalar ("std"is the sample standard deviation), which LightningModule.log accepts. Without it,computereturns a dict of group id to value, which self.log() does not support; log it from the on_val_epoch_end and on_test_epoch_end methods:metric_dict = {metric_name + “/” + k: v for k, v in grouped_metric.compute().items()} self.log_dict(metric_dict) grouped_metric.reset()