MetricMeter¶
danling.metrics.metric_meter
¶
MetricMeter
¶
Bases: AverageMeter
Computes metrics and averages them over time.
Attributes:
Name | Type | Description |
---|---|---|
metric |
Callable
|
Metric function for computing the value. |
ignored_index |
Optional[int]
|
Index to be ignored in the computation. |
val |
Optional[int]
|
Results of current batch on current device. |
bat |
Optional[int]
|
Results of current batch on all devices. |
avg |
Optional[int]
|
Results of all results on all devices. |
sum |
Optional[int]
|
Sum of values. |
count |
Optional[int]
|
Number of values. |
See Also
[AverageMeter
]: Average meter for computed values.
[MetricMeters
]: Manage multiple metric meters in one object.
Examples:
>>> from danling.metrics.functional import accuracy
>>> meter = MetricMeter(accuracy)
>>> meter.update([0.1, 0.8, 0.6, 0.2], [0, 1, 0, 0])
>>> meter.val
0.75
>>> meter.avg
0.75
>>> meter.update([0.1, 0.7, 0.3, 0.2, 0.8, 0.4], [0, 1, 1, 0, 0, 1])
>>> meter.val
0.5
>>> meter.avg
0.6
>>> meter.sum
6.0
>>> meter.count
10
>>> meter.reset()
>>> meter.val
0
>>> meter.avg
nan
Source code in danling/metrics/metric_meter.py
reset
¶
Python | |
---|---|
update
¶
Python | |
---|---|
|
Updates the average and current value in the meter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Value to be added to the average. |
required | |
n |
Number of values to be added. |
required |
Source code in danling/metrics/metric_meter.py
MetricMeters
¶
Bases: AverageMeters
Manages multiple metric meters in one object.
Attributes:
Name | Type | Description |
---|---|---|
ignored_index |
Index to be ignored in the computation. Defaults to None. |
See Also
[MetricMeter
]: Computes metrics and averages them over time.
[AverageMeters
]: Average meters for computed values.
from danling.metrics.functional import accuracy, auroc, auprc meters = MetricMeters(acc=accuracy, auroc=auroc, auprc=auprc) meters.update([0.1, 0.8, 0.6, 0.2], [0, 1, 0, 0]) meters.sum.dict() {‘acc’: 3.0, ‘auroc’: 4.0, ‘auprc’: 4.0} meters.count.dict() {‘acc’: 4, ‘auroc’: 4, ‘auprc’: 4} meters[‘auroc’].update([0.2, 0.8], [0, 1]) meters.sum.dict() {‘acc’: 3.0, ‘auroc’: 6.0, ‘auprc’: 4.0} meters.count.dict() {‘acc’: 4, ‘auroc’: 6, ‘auprc’: 4} meters.update([[0.1, 0.7, 0.3, 0.2], [0.8, 0.4]], [[0, 0, 1, 0], [0, 0]]) meters.sum.dict() {‘acc’: 6.0, ‘auroc’: 8.4, ‘auprc’: 5.5} meters.count.dict() {‘acc’: 10, ‘auroc’: 12, ‘auprc’: 10} meters[‘auroc’].update([0.4, 0.8, 0.6, 0.2], [0, 1, 1, 0]) meters.avg.dict() {‘acc’: 0.6, ‘auroc’: 0.775, ‘auprc’: 0.55} meters.update(dict(loss=”“)) # doctest: +ELLIPSIS Traceback (most recent call last): TypeError: …update() missing 1 required positional argument: ‘target’
Source code in danling/metrics/metric_meter.py
Python | |
---|---|
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
update
¶
Python | |
---|---|
|
Updates the average and current value in all meters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
Tensor | NestedTensor | Sequence
|
Input values to compute the metrics. |
required |
target |
Tensor | NestedTensor | Sequence
|
Target values to compute the metrics. |
required |
Source code in danling/metrics/metric_meter.py
MultiTaskMetricMeters
¶
Bases: MultiTaskAverageMeters
Examples:
>>> from danling.metrics.functional import accuracy
>>> metrics = MultiTaskMetricMeters()
>>> metrics.dataset1.cls = MetricMeters(acc=accuracy)
>>> metrics.dataset2 = MetricMeters(acc=accuracy)
>>> metrics
MultiTaskMetricMeters(<class 'danling.metrics.metric_meter.MultiTaskMetricMeters'>,
('dataset1'): MultiTaskMetricMeters(<class 'danling.metrics.metric_meter.MultiTaskMetricMeters'>,
('cls'): MetricMeters('acc',)
)
('dataset2'): MetricMeters('acc',)
)
>>> metrics.update({"dataset1.cls": {"input": [0.2, 0.4, 0.5, 0.7], "target": [0, 1, 0, 1]}, "dataset2": ([0.1, 0.4, 0.6, 0.8], [1, 0, 0, 0])})
>>> f"{metrics:.4f}"
'dataset1.cls: acc: 0.5000 (0.5000)\ndataset2: acc: 0.2500 (0.2500)'
>>> metrics.setattr("return_average", True)
>>> metrics.update({"dataset1.cls": [[0.1, 0.4, 0.6, 0.8], [0, 0, 1, 0]], "dataset2": {"input": [0.2, 0.3, 0.5, 0.7], "target": [0, 0, 0, 1]}})
>>> f"{metrics:.4f}"
'dataset1.cls: acc: 0.7500 (0.6250)\ndataset2: acc: 0.7500 (0.5000)'
>>> metrics.update(dict(loss=""))
Traceback (most recent call last):
ValueError: Metric loss not found in ...
Source code in danling/metrics/metric_meter.py
Python | |
---|---|
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 |
|
update
¶
Python | |
---|---|
|
Updates the average and current value in all meters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
Input values to compute the metrics. |
required | |
target |
Target values to compute the metrics. |
required |