-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantile_loss.py
1183 lines (942 loc) · 40.9 KB
/
quantile_loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Implementation of metrics for (mulit-horizon) timeseries forecasting.
"""
from typing import Dict, List, Tuple, Union
import warnings
import scipy.stats
from sklearn.base import BaseEstimator
import torch
from torch import distributions
import torch.nn.functional as F
from torch.nn.utils import rnn
from torchmetrics import Metric as LightningMetric
from pytorch_forecasting.utils import create_mask, unpack_sequence, unsqueeze_like
class Metric(LightningMetric):
"""
Base metric class that has basic functions that can handle predicting quantiles and operate in log space.
See the `Lightning documentation <https://pytorch-lightning.readthedocs.io/en/latest/metrics.html>`_
for details of how to implement a new metric
Other metrics should inherit from this base class
"""
def __init__(self, name: str = None, quantiles: List[float] = None, reduction="mean"):
"""
Initialize metric
Args:
name (str): metric name. Defaults to class name.
quantiles (List[float], optional): quantiles for probability range. Defaults to None.
reduction (str, optional): Reduction, "none", "mean" or "sqrt-mean". Defaults to "mean".
"""
self.quantiles = quantiles
self.reduction = reduction
if name is None:
name = self.__class__.__name__
self.name = name
super().__init__()
def update(y_pred: torch.Tensor, y_actual: torch.Tensor):
raise NotImplementedError()
def compute(self) -> torch.Tensor:
"""
Abstract method that calcualtes metric
Should be overriden in derived classes
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: metric value on which backpropagation can be applied
"""
raise NotImplementedError()
def rescale_parameters(
self, parameters: torch.Tensor, target_scale: torch.Tensor, encoder: BaseEstimator
) -> torch.Tensor:
"""
Rescale normalized parameters into the scale required for the output.
Args:
parameters (torch.Tensor): normalized parameters (indexed by last dimension)
target_scale (torch.Tensor): scale of parameters (n_batch_samples x (center, scale))
encoder (BaseEstimator): original encoder that normalized the target in the first place
Returns:
torch.Tensor: parameters in real/not normalized space
"""
return encoder(dict(prediction=parameters, target_scale=target_scale))
def to_prediction(self, y_pred: torch.Tensor) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Args:
y_pred: prediction output of network
Returns:
torch.Tensor: point prediction
"""
if y_pred.ndim == 3:
if self.quantiles is None:
assert y_pred.size(-1) == 1, "Prediction should only have one extra dimension"
y_pred = y_pred[..., 0]
else:
y_pred = y_pred.mean(-1)
return y_pred
def to_quantiles(self, y_pred: torch.Tensor, quantiles: List[float] = None) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.
Args:
y_pred: prediction output of network
quantiles (List[float], optional): quantiles for probability range. Defaults to quantiles as
as defined in the class initialization.
Returns:
torch.Tensor: prediction quantiles
"""
if quantiles is None:
quantiles = self.quantiles
if y_pred.ndim == 2:
return y_pred.unsqueeze(-1)
elif y_pred.ndim == 3:
if y_pred.size(2) > 1: # single dimension means all quantiles are the same
assert quantiles is not None, "quantiles are not defined"
y_pred = torch.quantile(y_pred, torch.tensor(quantiles, device=y_pred.device), dim=2).permute(1, 2, 0)
return y_pred
else:
raise ValueError(f"prediction has 1 or more than 3 dimensions: {y_pred.ndim}")
def __add__(self, metric: LightningMetric):
composite_metric = CompositeMetric(metrics=[self])
new_metric = composite_metric + metric
return new_metric
def __mul__(self, multiplier: float):
new_metric = CompositeMetric(metrics=[self], weights=[multiplier])
return new_metric
__rmul__ = __mul__
class MultiLoss(LightningMetric):
"""
Metric that can be used with muliple metrics.
"""
def __init__(self, metrics: List[LightningMetric], weights: List[float] = None):
"""
Args:
metrics (List[LightningMetric], optional): list of metrics to combine.
weights (List[float], optional): list of weights / multipliers for weights. Defaults to 1.0 for all metrics.
"""
assert len(metrics) > 0, "at least one metric has to be specified"
if weights is None:
weights = [1.0 for _ in metrics]
assert len(weights) == len(metrics), "Number of weights has to match number of metrics"
self.metrics = metrics
self.weights = weights
super().__init__()
def __repr__(self):
name = (
f"{self.__class__.__name__}("
+ ", ".join([f"{w:.3g} * {repr(m)}" if w != 1.0 else repr(m) for w, m in zip(self.weights, self.metrics)])
+ ")"
)
return name
def __iter__(self):
"""
Iterate over metrics.
"""
return iter(self.metrics)
def __len__(self) -> int:
"""
Number of metrics.
Returns:
int: number of metrics
"""
return len(self.metrics)
def update(self, y_pred: torch.Tensor, y_actual: torch.Tensor):
"""
Update composite metric
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: metric value on which backpropagation can be applied
"""
for idx, metric in enumerate(self.metrics):
metric.update(y_pred[idx], (y_actual[0][idx], y_actual[1]))
def compute(self) -> torch.Tensor:
"""
Get metric
Returns:
torch.Tensor: metric
"""
results = []
for weight, metric in zip(self.weights, self.metrics):
results.append(metric.compute() * weight)
if len(results) == 1:
results = results[0]
else:
results = torch.stack(results, dim=0).sum(0)
return results
def to_prediction(self, y_pred: torch.Tensor, **kwargs) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Will use first metric in ``metrics`` attribute to calculate result.
Args:
y_pred: prediction output of network
**kwargs: arguments for metrics
Returns:
torch.Tensor: point prediction
"""
result = []
for idx, metric in enumerate(self.metrics):
try:
result.append(metric.to_prediction(y_pred[idx], **kwargs))
except TypeError:
result.append(metric.to_prediction(y_pred[idx]))
return result
def to_quantiles(self, y_pred: torch.Tensor, **kwargs) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.
Will use first metric in ``metrics`` attribute to calculate result.
Args:
y_pred: prediction output of network
**kwargs: parameters to each metric's ``to_quantiles()`` method
Returns:
torch.Tensor: prediction quantiles
"""
result = []
for idx, metric in enumerate(self.metrics):
try:
result.append(metric.to_quantiles(y_pred[idx], **kwargs))
except TypeError:
result.append(metric.to_quantiles(y_pred[idx]))
return result
def __getitem__(self, idx: int):
"""
Return metric.
Args:
idx (int): metric index
"""
return self.metrics[idx]
def __getattr__(self, name: str):
"""
Return dynamically attributes.
Return attributes if defined in this class. If not, create dynamically attributes based on
attributes of underlying metrics that are lists. Create functions if necessary.
Arguments to functions are distributed to the functions if they are lists and their length
matches the number of metrics. Otherwise, they are directly passed to each callable of the
metrics
Args:
name (str): name of attribute
Returns:
attributes of this class or list of attributes of underlying class
"""
try:
return super().__getattr__(name)
except AttributeError as e:
attribute_exists = all([hasattr(metric, name) for metric in self.metrics])
if attribute_exists:
# check if to return callable or not and return function if yes
if callable(getattr(self.metrics[0], name)):
n = len(self.metrics)
def func(*args, **kwargs):
# if arg/kwarg is list and of length metric, then apply each part to a metric. otherwise
# pass it directly to all metrics
results = []
for idx, m in enumerate(self.metrics):
new_args = [
arg[idx]
if isinstance(arg, (list, tuple))
and not isinstance(arg, rnn.PackedSequence)
and len(arg) == n
else arg
for arg in args
]
new_kwargs = {
key: val[idx]
if isinstance(val, list) and not isinstance(val, rnn.PackedSequence) and len(val) == n
else val
for key, val in kwargs.items()
}
results.append(getattr(m, name)(*new_args, **new_kwargs))
return results
return func
else:
# else return list of attributes
return [getattr(metric, name) for metric in self.metrics]
else: # attribute does not exist for all metrics
raise e
class CompositeMetric(LightningMetric):
"""
Metric that combines multiple metrics.
Metric does not have to be called explicitly but is automatically created when adding and multiplying metrics
with each other.
Example:
.. code-block:: python
composite_metric = SMAPE() + 0.4 * MAE()
"""
def __init__(self, metrics: List[LightningMetric] = [], weights: List[float] = None):
"""
Args:
metrics (List[LightningMetric], optional): list of metrics to combine. Defaults to [].
weights (List[float], optional): list of weights / multipliers for weights. Defaults to 1.0 for all metrics.
"""
if weights is None:
weights = [1.0 for _ in metrics]
assert len(weights) == len(metrics), "Number of weights has to match number of metrics"
self.metrics = metrics
self.weights = weights
super().__init__()
def __repr__(self):
name = " + ".join([f"{w:.3g} * {repr(m)}" if w != 1.0 else repr(m) for w, m in zip(self.weights, self.metrics)])
return name
def update(self, y_pred: torch.Tensor, y_actual: torch.Tensor):
"""
Update composite metric
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: metric value on which backpropagation can be applied
"""
for metric in self.metrics:
metric.update(y_pred, y_actual)
def compute(self) -> torch.Tensor:
"""
Get metric
Returns:
torch.Tensor: metric
"""
results = []
for weight, metric in zip(self.weights, self.metrics):
results.append(metric.compute() * weight)
if len(results) == 1:
results = results[0]
else:
results = torch.stack(results, dim=0).sum(0)
return results
def to_prediction(self, y_pred: torch.Tensor, **kwargs) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Will use first metric in ``metrics`` attribute to calculate result.
Args:
y_pred: prediction output of network
**kwargs: parameters to first metric `to_prediction` method
Returns:
torch.Tensor: point prediction
"""
return self.metrics[0].to_prediction(y_pred, **kwargs)
def to_quantiles(self, y_pred: torch.Tensor, **kwargs) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.
Will use first metric in ``metrics`` attribute to calculate result.
Args:
y_pred: prediction output of network
**kwargs: parameters to first metric's ``to_quantiles()`` method
Returns:
torch.Tensor: prediction quantiles
"""
return self.metrics[0].to_quantiles(y_pred, **kwargs)
def __add__(self, metric: LightningMetric):
if isinstance(metric, self.__class__):
self.metrics.extend(metric.metrics)
self.weights.extend(metric.weights)
else:
self.metrics.append(metric)
self.weights.append(1.0)
return self
def __mul__(self, multiplier: float):
self.weights = [w * multiplier for w in self.weights]
return self
__rmul__ = __mul__
class AggregationMetric(Metric):
"""
Calculate metric on mean prediction and actuals.
"""
def __init__(self, metric: Metric, **kwargs):
"""
Args:
metric (Metric): metric which to calculate on aggreation.
"""
super().__init__(**kwargs)
self.metric = metric
def update(self, y_pred: torch.Tensor, y_actual: torch.Tensor) -> torch.Tensor:
"""
Calculate composite metric
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: metric value on which backpropagation can be applied
"""
# extract target and weight
if isinstance(y_actual, (tuple, list)) and not isinstance(y_actual, rnn.PackedSequence):
target, weight = y_actual
else:
target = y_actual
weight = None
# handle rnn sequence as target
if isinstance(target, rnn.PackedSequence):
target, lengths = rnn.pad_packed_sequence(target, batch_first=True)
# batch sizes reside on the CPU by default -> we need to bring them to GPU
lengths = lengths.to(target.device)
# calculate mask for time steps
length_mask = create_mask(target.size(1), lengths, inverse=True)
# modify weight
if weight is None:
weight = length_mask
else:
weight = weight * length_mask
if weight is None:
y_mean = target.mean(0)
y_pred_mean = y_pred.mean(0)
else:
# calculate weighted sums
y_mean = (target * unsqueeze_like(weight, y_pred)).sum(0) / weight.sum(0)
y_pred_sum = (y_pred * unsqueeze_like(weight, y_pred)).sum(0)
y_pred_mean = y_pred_sum / unsqueeze_like(weight.sum(0), y_pred_sum)
# update metric. unsqueeze first batch dimension (as batches are collapsed)
self.metric.update(y_pred_mean.unsqueeze(0), y_mean.unsqueeze(0))
def compute(self):
return self.metric.compute()
class MultiHorizonMetric(Metric):
"""
Abstract class for defining metric for a multihorizon forecast
"""
def __init__(self, reduction: str = "mean", **kwargs) -> None:
super().__init__(reduction=reduction, **kwargs)
self.add_state("losses", default=torch.tensor(0.0), dist_reduce_fx="sum" if reduction != "none" else "cat")
self.add_state("lengths", default=torch.tensor(0), dist_reduce_fx="sum" if reduction != "none" else "mean")
def loss(self, y_pred: Dict[str, torch.Tensor], target: torch.Tensor) -> torch.Tensor:
"""
Calculate loss without reduction. Override in derived classes
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: loss/metric as a single number for backpropagation
"""
raise NotImplementedError()
def update(self, y_pred, target):
"""
Update method of metric that handles masking of values.
Do not override this method but :py:meth:`~loss` instead
Args:
y_pred (Dict[str, torch.Tensor]): network output
target (Union[torch.Tensor, rnn.PackedSequence]): actual values
Returns:
torch.Tensor: loss as a single number for backpropagation
"""
# unpack weight
if isinstance(target, (list, tuple)) and not isinstance(target, rnn.PackedSequence):
target, weight = target
else:
weight = None
# unpack target
if isinstance(target, rnn.PackedSequence):
target, lengths = unpack_sequence(target)
else:
lengths = torch.full((target.size(0),), fill_value=target.size(1), dtype=torch.long, device=target.device)
losses = self.loss(y_pred, target)
# weight samples
if weight is not None:
losses = losses * unsqueeze_like(weight, losses)
self._update_losses_and_lengths(losses, lengths)
def _update_losses_and_lengths(self, losses: torch.Tensor, lengths: torch.Tensor):
losses = self.mask_losses(losses, lengths)
if self.reduction == "none":
if self.losses.ndim == 0:
self.losses = losses
self.lengths = lengths
else:
self.losses = torch.cat([self.losses, losses], dim=0)
self.lengths = torch.cat([self.lengths, lengths], dim=0)
else:
losses = losses.sum()
if not torch.isfinite(losses):
losses = torch.tensor(1e9, device=losses.device)
warnings.warn("Loss is not finite. Resetting it to 1e9")
self.losses = self.losses + losses
self.lengths = self.lengths + lengths.sum()
def compute(self):
loss = self.reduce_loss(self.losses, lengths=self.lengths)
return loss
def mask_losses(self, losses: torch.Tensor, lengths: torch.Tensor, reduction: str = None) -> torch.Tensor:
"""
Mask losses.
Args:
losses (torch.Tensor): total loss. first dimenion are samples, second timesteps
lengths (torch.Tensor): total length
reduction (str, optional): type of reduction. Defaults to ``self.reduction``.
Returns:
torch.Tensor: masked losses
"""
if reduction is None:
reduction = self.reduction
if losses.ndim > 0:
# mask loss
mask = torch.arange(losses.size(1), device=losses.device).unsqueeze(0) >= lengths.unsqueeze(-1)
if losses.ndim > 2:
mask = mask.unsqueeze(-1)
dim_normalizer = losses.size(-1)
else:
dim_normalizer = 1.0
# reduce to one number
if reduction == "none":
losses = losses.masked_fill(mask, float("nan"))
else:
losses = losses.masked_fill(mask, 0.0) / dim_normalizer
return losses
def reduce_loss(self, losses: torch.Tensor, lengths: torch.Tensor, reduction: str = None) -> torch.Tensor:
"""
Reduce loss.
Args:
losses (torch.Tensor): total loss. first dimenion are samples, second timesteps
lengths (torch.Tensor): total length
reduction (str, optional): type of reduction. Defaults to ``self.reduction``.
Returns:
torch.Tensor: reduced loss
"""
if reduction is None:
reduction = self.reduction
if reduction == "none":
return losses # return immediately, no checks
elif reduction == "mean":
loss = losses.sum() / lengths.sum()
elif reduction == "sqrt-mean":
loss = losses.sum() / lengths.sum()
loss = loss.sqrt()
else:
raise ValueError(f"reduction {reduction} unknown")
assert not torch.isnan(loss), (
"Loss should not be nan - i.e. something went wrong "
"in calculating the loss (e.g. log of a negative number)"
)
assert torch.isfinite(
loss
), "Loss should not be infinite - i.e. something went wrong (e.g. input is not in log space)"
return loss
class PoissonLoss(MultiHorizonMetric):
"""
Poisson loss for count data
"""
def loss(self, y_pred: Dict[str, torch.Tensor], target: torch.Tensor) -> torch.Tensor:
return F.poisson_nll_loss(
super().to_prediction(y_pred), target, log_input=True, full=False, eps=1e-6, reduction="none"
)
def to_prediction(self, out: Dict[str, torch.Tensor]):
rate = torch.exp(super().to_prediction(out))
return rate
def to_quantiles(self, out: Dict[str, torch.Tensor], quantiles=None):
if quantiles is None:
if self.quantiles is None:
quantiles = [0.5]
else:
quantiles = self.quantiles
predictions = super().to_prediction(out)
return torch.stack([torch.tensor(scipy.stats.poisson(predictions.cpu()).ppf(q)) for q in quantiles], dim=-1).to(
predictions.device
)
class QuantileLoss(MultiHorizonMetric):
"""
Quantile loss, i.e. a quantile of ``q=0.5`` will give half of the mean absolute error as it is calcualted as
Defined as ``max(q * (y-y_pred), (1-q) * (y_pred-y))``
"""
def __init__(
self,
quantiles: List[float] = [0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98],
**kwargs,
):
"""
Quantile loss
Args:
quantiles: quantiles for metric
"""
super().__init__(quantiles=quantiles, **kwargs)
def loss(self, y_pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
# calculate quantile loss
losses = []
for i, q in enumerate(self.quantiles):
errors = target - y_pred[..., i]
losses.append(torch.max((q - 1) * errors, q * errors).unsqueeze(-1))
losses = torch.cat(losses, dim=2)
return losses
def to_prediction(self, y_pred: torch.Tensor) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Args:
y_pred: prediction output of network
Returns:
torch.Tensor: point prediction
"""
if y_pred.ndim == 3:
idx = self.quantiles.index(0.5)
y_pred = y_pred[..., idx]
return y_pred
def to_quantiles(self, y_pred: torch.Tensor) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.
Args:
y_pred: prediction output of network
Returns:
torch.Tensor: prediction quantiles
"""
return y_pred
class SMAPE(MultiHorizonMetric):
"""
Symmetric mean absolute percentage. Assumes ``y >= 0``.
Defined as ``2*(y - y_pred).abs() / (y.abs() + y_pred.abs())``
"""
def loss(self, y_pred, target):
y_pred = self.to_prediction(y_pred)
loss = 2 * (y_pred - target).abs() / (y_pred.abs() + target.abs() + 1e-8)
return loss
class MAPE(MultiHorizonMetric):
"""
Mean absolute percentage. Assumes ``y >= 0``.
Defined as ``(y - y_pred).abs() / y.abs()``
"""
def loss(self, y_pred, target):
loss = (self.to_prediction(y_pred) - target).abs() / (target.abs() + 1e-8)
return loss
class MAE(MultiHorizonMetric):
"""
Mean average absolute error.
Defined as ``(y_pred - target).abs()``
"""
def loss(self, y_pred, target):
loss = (self.to_prediction(y_pred) - target).abs()
return loss
class CrossEntropy(MultiHorizonMetric):
"""
Cross entropy loss for classification.
"""
def loss(self, y_pred, target):
loss = F.cross_entropy(y_pred.view(-1, y_pred.size(-1)), target.view(-1), reduction="none").view(
-1, target.size(-1)
)
return loss
def to_prediction(self, y_pred: torch.Tensor) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Returns best label
Args:
y_pred: prediction output of network
Returns:
torch.Tensor: point prediction
"""
return y_pred.argmax(dim=-1)
def to_quantiles(self, y_pred: torch.Tensor, quantiles: List[float] = None) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.
Args:
y_pred: prediction output of network
quantiles (List[float], optional): quantiles for probability range. Defaults to quantiles as
as defined in the class initialization.
Returns:
torch.Tensor: prediction quantiles
"""
return y_pred
class RMSE(MultiHorizonMetric):
"""
Root mean square error
Defined as ``(y_pred - target)**2``
"""
def __init__(self, reduction="sqrt-mean", **kwargs):
super().__init__(reduction=reduction, **kwargs)
def loss(self, y_pred: Dict[str, torch.Tensor], target):
loss = torch.pow(self.to_prediction(y_pred) - target, 2)
return loss
class MASE(MultiHorizonMetric):
"""
Mean absolute scaled error
Defined as ``(y_pred - target).abs() / all_targets[:, :-1] - all_targets[:, 1:]).mean(1)``.
``all_targets`` are here the concatenated encoder and decoder targets
"""
def update(
self,
y_pred,
target,
encoder_target,
encoder_lengths=None,
) -> torch.Tensor:
"""
Update metric that handles masking of values.
Args:
y_pred (Dict[str, torch.Tensor]): network output
target (Tuple[Union[torch.Tensor, rnn.PackedSequence], torch.Tensor]): tuple of actual values and weights
encoder_target (Union[torch.Tensor, rnn.PackedSequence]): historic actual values
encoder_lengths (torch.Tensor): optional encoder lengths, not necessary if encoder_target
is rnn.PackedSequence. Assumed encoder_target is torch.Tensor
Returns:
torch.Tensor: loss as a single number for backpropagation
"""
# unpack weight
if isinstance(target, (list, tuple)):
weight = target[1]
target = target[0]
else:
weight = None
# unpack target
if isinstance(target, rnn.PackedSequence):
target, lengths = unpack_sequence(target)
else:
lengths = torch.full((target.size(0),), fill_value=target.size(1), dtype=torch.long, device=target.device)
# determine lengths for encoder
if encoder_lengths is None:
encoder_target, encoder_lengths = unpack_sequence(target)
else:
assert isinstance(encoder_target, torch.Tensor)
assert not target.requires_grad
# calculate loss with "none" reduction
scaling = self.calculate_scaling(target, lengths, encoder_target, encoder_lengths)
losses = self.loss(y_pred, target, scaling)
# weight samples
if weight is not None:
losses = losses * weight.unsqueeze(-1)
self._update_losses_and_lengths(losses, lengths)
def loss(self, y_pred, target, scaling):
return (y_pred - target).abs() / scaling.unsqueeze(-1)
def calculate_scaling(self, target, lengths, encoder_target, encoder_lengths):
# calcualte mean(abs(diff(targets)))
eps = 1e-6
batch_size = target.size(0)
total_lengths = lengths + encoder_lengths
assert (total_lengths > 1).all(), "Need at least 2 target values to be able to calculate MASE"
max_length = target.size(1) + encoder_target.size(1)
if (total_lengths != max_length).any(): # if decoder or encoder targets have sequences of different lengths
targets = torch.cat(
[
encoder_target,
torch.zeros(batch_size, target.size(1), device=target.device, dtype=encoder_target.dtype),
],
dim=1,
)
target_index = torch.arange(target.size(1), device=target.device, dtype=torch.long).unsqueeze(0).expand(
batch_size, -1
) + encoder_lengths.unsqueeze(-1)
targets.scatter_(dim=1, src=target, index=target_index)
else:
targets = torch.cat([encoder_target, target], dim=1)
# take absolute difference
diffs = (targets[:, :-1] - targets[:, 1:]).abs()
# set last difference to 0
not_maximum_length = total_lengths != max_length
zero_correction_indices = total_lengths[not_maximum_length] - 1
if len(zero_correction_indices) > 0:
diffs[
torch.arange(batch_size, dtype=torch.long, device=diffs.device)[not_maximum_length],
zero_correction_indices,
] = 0.0
# calculate mean over differences
scaling = diffs.sum(1) / total_lengths + eps
return scaling
class DistributionLoss(MultiHorizonMetric):
"""
DistributionLoss base class.
Class should be inherited for all distribution losses, i.e. if a network predicts
the parameters of a probability distribution, DistributionLoss can be used to
score those parameters and calculate loss for given true values.
Define two class attributes in a child class:
Attributes:
distribution_class (distributions.Distribution): torch probability distribution
distribution_arguments (List[str]): list of parameter names for the distribution
Further, implement the methods :py:meth:`~map_x_to_distribution` and :py:meth:`~rescale_parameters`.
"""
distribution_class: distributions.Distribution
distribution_arguments: List[str]
def __init__(
self, name: str = None, quantiles: List[float] = [0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98], reduction="mean"
):
"""
Initialize metric
Args:
name (str): metric name. Defaults to class name.
quantiles (List[float], optional): quantiles for probability range.
Defaults to [0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98].
reduction (str, optional): Reduction, "none", "mean" or "sqrt-mean". Defaults to "mean".
"""
super().__init__(name=name, quantiles=quantiles, reduction=reduction)
def map_x_to_distribution(self, x: torch.Tensor) -> distributions.Distribution:
"""
Map the a tensor of parameters to a probability distribution.
Args:
x (torch.Tensor): parameters for probability distribution. Last dimension will index the parameters
Returns:
distributions.Distribution: torch probability distribution as defined in the
class attribute ``distribution_class``
"""
raise NotImplementedError("implement this method")
def loss(self, y_pred: torch.Tensor, y_actual: torch.Tensor) -> torch.Tensor:
"""
Calculate negative likelihood
Args:
y_pred: network output
y_actual: actual values
Returns:
torch.Tensor: metric value on which backpropagation can be applied
"""
distribution = self.map_x_to_distribution(y_pred)
loss = -distribution.log_prob(y_actual)
return loss
def to_prediction(self, y_pred: torch.Tensor) -> torch.Tensor:
"""
Convert network prediction into a point prediction.
Args:
y_pred: prediction output of network
Returns:
torch.Tensor: mean prediction
"""
distribution = self.map_x_to_distribution(y_pred)
return distribution.mean
def sample(self, y_pred, n_samples: int) -> torch.Tensor:
"""
Sample from distribution.
Args:
y_pred: prediction output of network (shape batch_size x n_timesteps x n_paramters)
n_samples (int): number of samples to draw
Returns:
torch.Tensor: tensor with samples (shape batch_size x n_timesteps x n_samples)
"""
dist = self.map_x_to_distribution(y_pred)
samples = dist.sample((n_samples,))
if samples.ndim == 3:
samples = samples.permute(1, 2, 0)
elif samples.ndim == 2:
samples = samples.transpose(0, 1)
return samples
def to_quantiles(self, y_pred: torch.Tensor, quantiles: List[float] = None, n_samples: int = 100) -> torch.Tensor:
"""
Convert network prediction into a quantile prediction.