mae.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Union
  15. import torch
  16. from torch import Tensor
  17. from torchmetrics.utilities.checks import _check_same_shape
  18. def _mean_absolute_error_update(preds: Tensor, target: Tensor, num_outputs: int) -> tuple[Tensor, int]:
  19. """Update and returns variables required to compute Mean Absolute Error.
  20. Check for same shape of input tensors.
  21. Args:
  22. preds: Predicted tensor
  23. target: Ground truth tensor
  24. num_outputs: Number of outputs in multioutput setting
  25. """
  26. _check_same_shape(preds, target)
  27. if num_outputs == 1:
  28. preds = preds.view(-1)
  29. target = target.view(-1)
  30. preds = preds if preds.is_floating_point else preds.float() # type: ignore[truthy-function] # todo
  31. target = target if target.is_floating_point else target.float() # type: ignore[truthy-function] # todo
  32. sum_abs_error = torch.sum(torch.abs(preds - target), dim=0)
  33. return sum_abs_error, target.shape[0]
  34. def _mean_absolute_error_compute(sum_abs_error: Tensor, num_obs: Union[int, Tensor]) -> Tensor:
  35. """Compute Mean Absolute Error.
  36. Args:
  37. sum_abs_error: Sum of absolute value of errors over all observations
  38. num_obs: Number of predictions or observations
  39. Example:
  40. >>> preds = torch.tensor([0., 1, 2, 3])
  41. >>> target = torch.tensor([0., 1, 2, 2])
  42. >>> sum_abs_error, num_obs = _mean_absolute_error_update(preds, target, num_outputs=1)
  43. >>> _mean_absolute_error_compute(sum_abs_error, num_obs)
  44. tensor(0.2500)
  45. """
  46. return sum_abs_error / num_obs
  47. def mean_absolute_error(preds: Tensor, target: Tensor, num_outputs: int = 1) -> Tensor:
  48. """Compute mean absolute error.
  49. Args:
  50. preds: estimated labels
  51. target: ground truth labels
  52. num_outputs: Number of outputs in multioutput setting
  53. Return:
  54. Tensor with MAE
  55. Example:
  56. >>> from torchmetrics.functional.regression import mean_absolute_error
  57. >>> x = torch.tensor([0., 1, 2, 3])
  58. >>> y = torch.tensor([0., 1, 2, 2])
  59. >>> mean_absolute_error(x, y)
  60. tensor(0.2500)
  61. """
  62. sum_abs_error, num_obs = _mean_absolute_error_update(preds, target, num_outputs=num_outputs)
  63. return _mean_absolute_error_compute(sum_abs_error, num_obs)