Metrics¶
danling.metrics.metrics
¶
Metrics
¶
Bases: Metric
Metric class wraps around multiple metrics that share the same states.
Typically, there are many metrics that we want to compute for a single task.
For example, we usually needs to compute pearson
and spearman
for a regression task.
Unlike accuracy
, which can uses an average meter to compute the average accuracy,
pearson
and spearman
cannot be computed by averaging the results of multiple batches.
They need access to all the data to compute the correct results.
And saving all intermediate results for each tasks is quite inefficient.
Metrics
solves this problem by maintaining a shared state for multiple metric functions.
Attributes:
Name | Type | Description |
---|---|---|
metrics |
FlatDict[str, Callable]
|
A dictionary of metrics to be computed.A |
ignored_index |
Optional[int]
|
Index to be ignored in the computation. |
val |
NestedDict[str, float | flist]
|
Metric results of current batch on current device. |
avg |
NestedDict[str, float | flist]
|
Metric results of all results on all devices. |
input |
The input tensor of latest batch. |
|
target |
The target tensor of latest batch. |
|
inputs |
All input tensors. |
|
targets |
All target tensors. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
A single mapping of metrics. |
()
|
|
|
Callable
|
Metrics. |
{}
|
Examples:
>>> from danling.metrics.functional import auroc, auprc
>>> metrics = Metrics(auroc=auroc, auprc=auprc)
>>> metrics
Metrics('auroc', 'auprc')
>>> metrics.update([0.2, 0.3, 0.5, 0.7], [0, 1, 0, 1])
>>> metrics.input # predicted values of current batch
tensor([0.2000, 0.3000, 0.5000, 0.7000])
>>> metrics.target # ground truth of current batch
tensor([0, 1, 0, 1])
>>> metrics.inputs # predicted values of all data
tensor([0.2000, 0.3000, 0.5000, 0.7000])
>>> metrics.targets # ground truth of all data
tensor([0, 1, 0, 1])
>>> metrics.val # Metrics of current batch on current device
NestedDict(
('auroc'): 0.75
('auprc'): 0.8333333730697632
)
>>> metrics.avg # Metrics of all data on all devices
NestedDict(
('auroc'): 0.75
('auprc'): 0.8333333730697632
)
>>> metrics.update([0.1, 0.4, 0.6, 0.8], [0, 0, 1, 0])
>>> metrics.input # predicted values of current batch
tensor([0.1000, 0.4000, 0.6000, 0.8000])
>>> metrics.target # ground truth of current batch
tensor([0, 0, 1, 0])
>>> metrics.inputs # predicted values of all data
tensor([0.2000, 0.3000, 0.5000, 0.7000, 0.1000, 0.4000, 0.6000, 0.8000])
>>> metrics.targets # ground truth of all data
tensor([0, 1, 0, 1, 0, 0, 1, 0])
>>> metrics.val # Metrics of current batch on current device
NestedDict(
('auroc'): 0.6666666666666666
('auprc'): 0.5
)
>>> metrics.avg # Metrics of all data on all devices
NestedDict(
('auroc'): 0.6666666666666666
('auprc'): 0.5555555820465088
)
>>> f"{metrics:.4f}"
'auroc: 0.6667 (0.6667)\tauprc: 0.5000 (0.5556)'
>>> metrics = Metrics(auroc=auroc, auprc=auprc, ignored_index=-100)
>>> metrics.update([[0.1, 0.4, 0.6, 0.8], [0.1, 0.4, 0.6]], [[0, -100, 1, 0], [0, -100, 1]])
>>> metrics.input, metrics.target
(tensor([0.1000, 0.6000, 0.8000, 0.1000, 0.6000]), tensor([0, 1, 0, 0, 1]))
Source code in danling/metrics/metrics.py
Python | |
---|---|
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 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
reset
¶
Python | |
---|---|
Reset the metric state variables to their default value.
The tensors in the default values are also moved to the device of
the last self.to(device)
call.
Source code in danling/metrics/metrics.py
ScoreMetrics
¶
Bases: Metrics
ScoreMetrics
is a subclass of Metrics that supports scoring.
Score is a single value that best represents the performance of the model. It is the core metrics that we use to compare different models. For example, in classification, we usually use auroc as the score.
ScoreMetrics
requires two additional arguments: score_name
and best_fn
.
score_name
is the name of the metric that we use to compute the score.
best_fn
is a function that takes a list of values and returns the best value.
best_fn
is only not used by ScoreMetrics
, it is meant to be accessed by other classes.
Attributes:
Name | Type | Description |
---|---|---|
score_name |
str
|
The name of the metric that we use to compute the score. |
best_fn |
Callable
|
A function that takes a list of values and returns the best value. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
A single mapping of metrics. |
()
|
|
|
str | None
|
The name of the metric that we use to compute the score. Defaults to the first metric. |
None
|
|
Callable | None
|
A function that takes a list of values and returns the best value. Defaults to |
max
|
|
NestedDict[str, Callable]
|
Metrics. |
{}
|
Source code in danling/metrics/metrics.py
MultiTaskMetrics
¶
Bases: MultiTaskDict
Examples:
>>> from danling.metrics.functional import auroc, auprc, pearson, spearman, accuracy, mcc
>>> metrics = MultiTaskMetrics()
>>> metrics.dataset1.cls = Metrics(auroc=auroc, auprc=auprc)
>>> metrics.dataset1.reg = Metrics(pearson=pearson, spearman=spearman)
>>> metrics.dataset2 = Metrics(auroc=auroc, auprc=auprc)
>>> metrics
MultiTaskMetrics(<class 'danling.metrics.metrics.MultiTaskMetrics'>,
('dataset1'): MultiTaskMetrics(<class 'danling.metrics.metrics.MultiTaskMetrics'>,
('cls'): Metrics('auroc', 'auprc')
('reg'): Metrics('pearson', 'spearman')
)
('dataset2'): Metrics('auroc', 'auprc')
)
>>> metrics.update({"dataset1.cls": {"input": [0.2, 0.4, 0.5, 0.7], "target": [0, 1, 0, 1]}, "dataset1.reg": {"input": [0.1, 0.4, 0.6, 0.8], "target": [0.2, 0.3, 0.5, 0.7]}, "dataset2": {"input": [0.1, 0.4, 0.6, 0.8], "target": [0, 1, 0, 1]}})
>>> f"{metrics:.4f}"
'dataset1.cls: auroc: 0.7500 (0.7500)\tauprc: 0.8333 (0.8333)\ndataset1.reg: pearson: 0.9691 (0.9691)\tspearman: 1.0000 (1.0000)\ndataset2: auroc: 0.7500 (0.7500)\tauprc: 0.8333 (0.8333)'
>>> metrics.setattr("return_average", True)
>>> metrics.update({"dataset1.cls": {"input": [0.1, 0.4, 0.6, 0.8], "target": [0, 0, 1, 0]}, "dataset1.reg": {"input": [0.2, 0.3, 0.5, 0.7], "target": [0.2, 0.4, 0.6, 0.8]}, "dataset2": {"input": [0.2, 0.3, 0.5, 0.7], "target": [0, 0, 1, 0]}})
>>> f"{metrics:.4f}"
'dataset1.cls: auroc: 0.6667 (0.7000)\tauprc: 0.5000 (0.5556)\ndataset1.reg: pearson: 0.9898 (0.9146)\tspearman: 1.0000 (0.9222)\ndataset2: auroc: 0.6667 (0.7333)\tauprc: 0.5000 (0.7000)'
>>> metrics.update({"dataset1": {"cls": {"input": [0.1, 0.4, 0.6, 0.8], "target": [1, 0, 1, 0]}}})
>>> f"{metrics:.4f}"
'dataset1.cls: auroc: 0.2500 (0.5286)\tauprc: 0.5000 (0.4789)\ndataset1.reg: pearson: 0.9898 (0.9146)\tspearman: 1.0000 (0.9222)\ndataset2: auroc: 0.6667 (0.7333)\tauprc: 0.5000 (0.7000)'
>>> metrics.update(dict(loss=""))
Traceback (most recent call last):
ValueError: Metric loss not found in ...
Source code in danling/metrics/metrics.py
Python | |
---|---|
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
update
¶
Updates the average and current value in all metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Mapping[str, Mapping[str, Tensor | NestedTensor | Sequence]]
|
Dict of values to be added to the average. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the value is not an instance of (Mapping). |