explained_variance.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 collections.abc import Sequence
  15. from typing import Union
  16. import torch
  17. from torch import Tensor
  18. from typing_extensions import Literal
  19. from torchmetrics.utilities.checks import _check_same_shape
  20. ALLOWED_MULTIOUTPUT = ("raw_values", "uniform_average", "variance_weighted")
  21. def _explained_variance_update(preds: Tensor, target: Tensor) -> tuple[int, Tensor, Tensor, Tensor, Tensor]:
  22. """Update and returns variables required to compute Explained Variance. Checks for same shape of input tensors.
  23. Args:
  24. preds: Predicted tensor
  25. target: Ground truth tensor
  26. """
  27. _check_same_shape(preds, target)
  28. num_obs = preds.size(0)
  29. sum_error = torch.sum(target - preds, dim=0)
  30. diff = target - preds
  31. sum_squared_error = torch.sum(diff * diff, dim=0)
  32. sum_target = torch.sum(target, dim=0)
  33. sum_squared_target = torch.sum(target * target, dim=0)
  34. return num_obs, sum_error, sum_squared_error, sum_target, sum_squared_target
  35. def _explained_variance_compute(
  36. num_obs: Union[int, Tensor],
  37. sum_error: Tensor,
  38. sum_squared_error: Tensor,
  39. sum_target: Tensor,
  40. sum_squared_target: Tensor,
  41. multioutput: Literal["raw_values", "uniform_average", "variance_weighted"] = "uniform_average",
  42. ) -> Tensor:
  43. """Compute Explained Variance.
  44. Args:
  45. num_obs: Number of predictions or observations
  46. sum_error: Sum of errors over all observations
  47. sum_squared_error: Sum of square of errors over all observations
  48. sum_target: Sum of target values
  49. sum_squared_target: Sum of squares of target values
  50. multioutput: Defines aggregation in the case of multiple output scores. Can be one
  51. of the following strings:
  52. * ``'raw_values'`` returns full set of scores
  53. * ``'uniform_average'`` scores are uniformly averaged
  54. * ``'variance_weighted'`` scores are weighted by their individual variances
  55. Example:
  56. >>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
  57. >>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
  58. >>> num_obs, sum_error, ss_error, sum_target, ss_target = _explained_variance_update(preds, target)
  59. >>> _explained_variance_compute(num_obs, sum_error, ss_error, sum_target, ss_target, multioutput='raw_values')
  60. tensor([0.9677, 1.0000])
  61. """
  62. diff_avg = sum_error / num_obs
  63. numerator = sum_squared_error / num_obs - (diff_avg * diff_avg)
  64. target_avg = sum_target / num_obs
  65. denominator = sum_squared_target / num_obs - (target_avg * target_avg)
  66. # Take care of division by zero
  67. nonzero_numerator = numerator != 0
  68. nonzero_denominator = denominator != 0
  69. valid_score = nonzero_numerator & nonzero_denominator
  70. output_scores = torch.ones_like(diff_avg)
  71. output_scores[valid_score] = 1.0 - (numerator[valid_score] / denominator[valid_score])
  72. output_scores[nonzero_numerator & ~nonzero_denominator] = 0.0
  73. # Decide what to do in multioutput case
  74. # Todo: allow user to pass in tensor with weights
  75. if multioutput == "raw_values":
  76. return output_scores
  77. if multioutput == "uniform_average":
  78. return torch.mean(output_scores)
  79. denom_sum = torch.sum(denominator)
  80. return torch.sum(denominator / denom_sum * output_scores)
  81. def explained_variance(
  82. preds: Tensor,
  83. target: Tensor,
  84. multioutput: Literal["raw_values", "uniform_average", "variance_weighted"] = "uniform_average",
  85. ) -> Union[Tensor, Sequence[Tensor]]:
  86. """Compute explained variance.
  87. Args:
  88. preds: estimated labels
  89. target: ground truth labels
  90. multioutput: Defines aggregation in the case of multiple output scores. Can be one
  91. of the following strings):
  92. * ``'raw_values'`` returns full set of scores
  93. * ``'uniform_average'`` scores are uniformly averaged
  94. * ``'variance_weighted'`` scores are weighted by their individual variances
  95. Example:
  96. >>> from torchmetrics.functional.regression import explained_variance
  97. >>> target = torch.tensor([3, -0.5, 2, 7])
  98. >>> preds = torch.tensor([2.5, 0.0, 2, 8])
  99. >>> explained_variance(preds, target)
  100. tensor(0.9572)
  101. >>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
  102. >>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
  103. >>> explained_variance(preds, target, multioutput='raw_values')
  104. tensor([0.9677, 1.0000])
  105. """
  106. if multioutput not in ALLOWED_MULTIOUTPUT:
  107. raise ValueError(f"Invalid input to argument `multioutput`. Choose one of the following: {ALLOWED_MULTIOUTPUT}")
  108. num_obs, sum_error, sum_squared_error, sum_target, sum_squared_target = _explained_variance_update(preds, target)
  109. return _explained_variance_compute(
  110. num_obs,
  111. sum_error,
  112. sum_squared_error,
  113. sum_target,
  114. sum_squared_target,
  115. multioutput,
  116. )