MetricMeter¶
danling.metric.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 |
Callable
|
Optional preprocessing function to apply to inputs and targets |
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 |
|
Function to preprocess inputs before computing the metric |
required |
Examples:
Notes
- MetricMeter is more memory-efficient than
Metrics
because 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/metric/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 |
|
update
¶
update(input: Tensor | NestedTensor, target: Tensor | NestedTensor) -> None
Updates the average and current value in the meter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Value to be added to the average. |
required | |
|
Number of values to be added. |
required |
Source code in danling/metric/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
MetricMeters
manages multipleMetricMeter
instances 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/metric/metric_meter.py
Python | |
---|---|
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 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 |
|
update
¶
update(input: Tensor | NestedTensor | Sequence, target: Tensor | NestedTensor | Sequence) -> 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 |