2020-10-06 21:03:24 +00:00
|
|
|
import torch
|
|
|
|
from typing import Any, Callable, Optional, Union
|
|
|
|
|
|
|
|
from pytorch_lightning.metrics.metric import Metric
|
|
|
|
|
|
|
|
|
|
|
|
class MeanAbsoluteError(Metric):
|
|
|
|
"""
|
|
|
|
Computes mean absolute error.
|
|
|
|
|
2020-10-09 01:26:39 +00:00
|
|
|
Args:
|
|
|
|
compute_on_step:
|
|
|
|
Forward only calls ``update()`` and return None if this is set to False. default: True
|
|
|
|
ddp_sync_on_step:
|
|
|
|
Synchronize metric state across processes at each ``forward()``
|
|
|
|
before returning the value at the step. default: False
|
|
|
|
process_group:
|
|
|
|
Specify the process group on which synchronization is called. default: None (which selects the entire world)
|
|
|
|
|
2020-10-06 21:03:24 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
>>> from pytorch_lightning.metrics import MeanAbsoluteError
|
|
|
|
>>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
|
|
|
|
>>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
|
|
|
|
>>> mean_absolute_error = MeanAbsoluteError()
|
|
|
|
>>> mean_absolute_error(preds, target)
|
|
|
|
tensor(0.5000)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
compute_on_step: bool = True,
|
|
|
|
ddp_sync_on_step: bool = False,
|
|
|
|
process_group: Optional[Any] = None,
|
|
|
|
):
|
|
|
|
super().__init__(
|
|
|
|
compute_on_step=compute_on_step,
|
|
|
|
ddp_sync_on_step=ddp_sync_on_step,
|
|
|
|
process_group=process_group,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.add_state("sum_abs_error", default=torch.tensor(0.0), dist_reduce_fx="sum")
|
|
|
|
self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum")
|
|
|
|
|
|
|
|
def update(self, preds: torch.Tensor, target: torch.Tensor):
|
|
|
|
"""
|
|
|
|
Update state with predictions and targets.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
preds: Predictions from model
|
|
|
|
target: Ground truth values
|
|
|
|
"""
|
2020-10-09 23:23:12 +00:00
|
|
|
self._check_same_shape(preds, target)
|
2020-10-06 21:03:24 +00:00
|
|
|
abs_error = torch.abs(preds - target)
|
|
|
|
|
|
|
|
self.sum_abs_error += torch.sum(abs_error)
|
|
|
|
self.total += target.numel()
|
|
|
|
|
|
|
|
def compute(self):
|
|
|
|
"""
|
|
|
|
Computes mean absolute error over state.
|
|
|
|
"""
|
|
|
|
return self.sum_abs_error / self.total
|