MetricMeter¶
danling.metrics.metric_meter
¶
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
|
The metric function to compute on each batch |
preprocess |
Optional preprocessing function applied before the metric |
|
val |
float
|
Result from the most recent batch |
bat |
float
|
Result from the most recent batch, synchronized across devices |
avg |
float
|
Weighted average of all results so far |
sum |
float
|
Running sum of (metric × batch_size) values |
count |
int
|
Running sum of batch sizes |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable
|
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
Metricsbecause 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
- The metric function should accept input and target tensors and return a scalar value
- For multiple metrics, use
MetricMeters
See Also
AverageMeter: A lightweight utility to compute and store running averages of values.
Source code in danling/metrics/metric_meter.py
| Python | |
|---|---|
34 35 36 37 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 | |
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
|
Source code in danling/metrics/metric_meter.py
MetricMeters
¶
Bases: AverageMeters
A container for managing multiple MetricMeter instances with shared preprocessing.
MetricMeters allows you to organize and track multiple metrics in a unified interface, with consistent preprocessing applied to all inputs before computing each metric. This is particularly useful when you want to track several metrics that can be meaningfully averaged across batches.
Attributes:
| Name | Type | Description |
|---|---|---|
preprocess |
Shared preprocessing function for all meters |
|
val |
RoundDict[str, float]
|
Dictionary of current values from all meters |
avg |
RoundDict[str, float]
|
Dictionary of running averages from all meters |
sum |
RoundDict[str, float]
|
Dictionary of sums from all meters |
count |
RoundDict[str, int]
|
Dictionary of counts from all meters |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Either metric functions or a Metrics instance to extract metrics from |
()
|
|
|
Callable
|
Preprocessing function to apply to inputs before computing metrics |
base_preprocess
|
|
Named MetricMeter instances or metric functions |
{}
|
Examples:
Notes
MetricMetersmanages multipleMetricMeterinstances with shared preprocessing- Each metric is computed independently but uses the same inputs
- All meters are updated simultaneously when you call
update() - Individual meters can be accessed like dictionary items or attributes
See Also
AverageMeters: A container for managing multiple average meters in one object.Metrics: Metric tracker that stores the complete prediction and target history.
Source code in danling/metrics/metric_meter.py
| Python | |
|---|---|
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
update
¶
update(input: Tensor | NestedTensor | Sequence, target: Tensor | NestedTensor | Sequence, *, n: int | None = None) -> None
Updates the average and current value in all meters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor | NestedTensor | Sequence
|
Input values to compute the metrics. |
required |
|
Tensor | NestedTensor | Sequence
|
Target values to compute the metrics. |
required |
|
int | None
|
Optional number of samples represented by this update. Defaults to the inferred batch size. |
None
|