MetricMeter¶
danling.metrics.MetricMeter
¶
Bases: AverageMeter
A memory-efficient metric tracker that computes and averages metrics across batches.
MetricMeter applies a metric function to each batch and maintains running averages without storing the complete history of predictions and labels. This makes it ideal for metrics that can be meaningfully averaged across batches (like accuracy or loss).
Attributes:
| Name | Type | Description |
|---|---|---|
metric |
Callable | MetricFunc
|
The metric function to compute on each batch |
preprocess |
Optional preprocessing function applied before the metric |
|
val |
float | Tensor
|
Result from the most recent batch on the current rank |
bat |
float | Tensor
|
Synchronized metric result for the current step |
avg |
float | Tensor
|
Weighted average of all results so far |
sum |
float | Tensor
|
Running sum of (metric × batch_size) values |
count |
int
|
Running sum of batch sizes |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable | MetricFunc
|
Function that computes a metric given input and target tensors |
required |
|
Callable | None
|
Optional preprocessing function to apply before computing the metric |
None
|
Examples:
Notes
- MetricMeter is more memory-efficient than
GlobalMetricsbecause it only stores running statistics - Only suitable for metrics that can be meaningfully averaged batch-by-batch
- Not suitable for metrics like AUROC that need the entire dataset
- Metrics are evaluated once per update; batch-vs-sample semantics are determined by the metric itself
- Stream metrics may return tensors; tensor outputs are averaged elementwise across batches
MetricFuncdescriptors receive [MetricState][danling.metrics.MetricState]- Plain callables receive preprocessed
input/targettensors - For multiple metrics, use
StreamMetrics
See Also
AverageMeter: A lightweight utility to compute and store running averages of values.
Source code in danling/metrics/stream_metrics.py
| Python | |
|---|---|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
update
¶
update(
input: Tensor | NestedTensor,
target: Tensor | NestedTensor,
*,
n: int | None = None
) -> None
Updates the average and current value in the meter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor | NestedTensor
|
Prediction tensor or nested tensor. |
required |
|
Tensor | NestedTensor
|
Ground-truth tensor or nested tensor. |
required |
|
int | None
|
Optional number of samples represented by this update. When omitted, the batch size is inferred from the inputs. |
None
|