explained_variance.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Any, Optional, Union
  16. from torch import Tensor, tensor
  17. from typing_extensions import Literal
  18. from torchmetrics.functional.regression.explained_variance import (
  19. ALLOWED_MULTIOUTPUT,
  20. _explained_variance_compute,
  21. _explained_variance_update,
  22. )
  23. from torchmetrics.metric import Metric
  24. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  25. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  26. if not _MATPLOTLIB_AVAILABLE:
  27. __doctest_skip__ = ["ExplainedVariance.plot"]
  28. class ExplainedVariance(Metric):
  29. r"""Compute `explained variance`_.
  30. .. math:: \text{ExplainedVariance} = 1 - \frac{\text{Var}(y - \hat{y})}{\text{Var}(y)}
  31. Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions.
  32. As input to ``forward`` and ``update`` the metric accepts the following input:
  33. - ``preds`` (:class:`~torch.Tensor`): Predictions from model in float tensor
  34. with shape ``(N,)`` or ``(N, ...)`` (multioutput)
  35. - ``target`` (:class:`~torch.Tensor`): Ground truth values in long tensor
  36. with shape ``(N,)`` or ``(N, ...)`` (multioutput)
  37. As output of ``forward`` and ``compute`` the metric returns the following output:
  38. - ``explained_variance`` (:class:`~torch.Tensor`): A tensor with the explained variance(s)
  39. In the case of multioutput, as default the variances will be uniformly averaged over the additional dimensions.
  40. Please see argument ``multioutput`` for changing this behavior.
  41. Args:
  42. multioutput:
  43. Defines aggregation in the case of multiple output scores. Can be one
  44. of the following strings (default is ``'uniform_average'``.):
  45. * ``'raw_values'`` returns full set of scores
  46. * ``'uniform_average'`` scores are uniformly averaged
  47. * ``'variance_weighted'`` scores are weighted by their individual variances
  48. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  49. Raises:
  50. ValueError:
  51. If ``multioutput`` is not one of ``"raw_values"``, ``"uniform_average"`` or ``"variance_weighted"``.
  52. Example:
  53. >>> from torch import tensor
  54. >>> from torchmetrics.regression import ExplainedVariance
  55. >>> target = tensor([3, -0.5, 2, 7])
  56. >>> preds = tensor([2.5, 0.0, 2, 8])
  57. >>> explained_variance = ExplainedVariance()
  58. >>> explained_variance(preds, target)
  59. tensor(0.9572)
  60. >>> target = tensor([[0.5, 1], [-1, 1], [7, -6]])
  61. >>> preds = tensor([[0, 2], [-1, 2], [8, -5]])
  62. >>> explained_variance = ExplainedVariance(multioutput='raw_values')
  63. >>> explained_variance(preds, target)
  64. tensor([0.9677, 1.0000])
  65. """
  66. is_differentiable: bool = True
  67. higher_is_better: bool = True
  68. full_state_update: bool = False
  69. plot_lower_bound: float = 0.0
  70. plot_upper_bound: float = 1.0
  71. num_obs: Tensor
  72. sum_error: Tensor
  73. sum_squared_error: Tensor
  74. sum_target: Tensor
  75. sum_squared_target: Tensor
  76. def __init__(
  77. self,
  78. multioutput: Literal["raw_values", "uniform_average", "variance_weighted"] = "uniform_average",
  79. **kwargs: Any,
  80. ) -> None:
  81. super().__init__(**kwargs)
  82. if multioutput not in ALLOWED_MULTIOUTPUT:
  83. raise ValueError(
  84. f"Invalid input to argument `multioutput`. Choose one of the following: {ALLOWED_MULTIOUTPUT}"
  85. )
  86. self.multioutput = multioutput
  87. self.add_state("sum_error", default=tensor(0.0), dist_reduce_fx="sum")
  88. self.add_state("sum_squared_error", default=tensor(0.0), dist_reduce_fx="sum")
  89. self.add_state("sum_target", default=tensor(0.0), dist_reduce_fx="sum")
  90. self.add_state("sum_squared_target", default=tensor(0.0), dist_reduce_fx="sum")
  91. self.add_state("num_obs", default=tensor(0.0), dist_reduce_fx="sum")
  92. def update(self, preds: Tensor, target: Tensor) -> None:
  93. """Update state with predictions and targets."""
  94. num_obs, sum_error, sum_squared_error, sum_target, sum_squared_target = _explained_variance_update(
  95. preds, target
  96. )
  97. self.num_obs = self.num_obs + num_obs
  98. self.sum_error = self.sum_error + sum_error
  99. self.sum_squared_error = self.sum_squared_error + sum_squared_error
  100. self.sum_target = self.sum_target + sum_target
  101. self.sum_squared_target = self.sum_squared_target + sum_squared_target
  102. def compute(self) -> Union[Tensor, Sequence[Tensor]]:
  103. """Compute explained variance over state."""
  104. return _explained_variance_compute(
  105. self.num_obs,
  106. self.sum_error,
  107. self.sum_squared_error,
  108. self.sum_target,
  109. self.sum_squared_target,
  110. self.multioutput,
  111. )
  112. def plot(
  113. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  114. ) -> _PLOT_OUT_TYPE:
  115. """Plot a single or multiple values from the metric.
  116. Args:
  117. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  118. If no value is provided, will automatically call `metric.compute` and plot that result.
  119. ax: An matplotlib axis object. If provided will add plot to that axis
  120. Returns:
  121. Figure and Axes object
  122. Raises:
  123. ModuleNotFoundError:
  124. If `matplotlib` is not installed
  125. .. plot::
  126. :scale: 75
  127. >>> from torch import randn
  128. >>> # Example plotting a single value
  129. >>> from torchmetrics.regression import ExplainedVariance
  130. >>> metric = ExplainedVariance()
  131. >>> metric.update(randn(10,), randn(10,))
  132. >>> fig_, ax_ = metric.plot()
  133. .. plot::
  134. :scale: 75
  135. >>> from torch import randn
  136. >>> # Example plotting multiple values
  137. >>> from torchmetrics.regression import ExplainedVariance
  138. >>> metric = ExplainedVariance()
  139. >>> values = []
  140. >>> for _ in range(10):
  141. ... values.append(metric(randn(10,), randn(10,)))
  142. >>> fig, ax = metric.plot(values)
  143. """
  144. return self._plot(val, ax)