|
| 1 | +# Copyright The PyTorch 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 Any, Callable, Optional |
| 15 | + |
| 16 | +import torch |
| 17 | + |
| 18 | +from pytorch_lightning.metrics.metric import Metric |
| 19 | +from pytorch_lightning.metrics.functional.r2score import ( |
| 20 | + _r2score_update, |
| 21 | + _r2score_compute |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class R2Score(Metric): |
| 26 | + r""" |
| 27 | + Computes r2 score also known as `coefficient of determination |
| 28 | + <https://en.wikipedia.org/wiki/Coefficient_of_determination>`_: |
| 29 | +
|
| 30 | + .. math:: R^2 = 1 - \frac{SS_res}{SS_tot} |
| 31 | +
|
| 32 | + where :math:`SS_res=\sum_i (y_i - f(x_i))^2` is the sum of residual squares, and |
| 33 | + :math:`SS_tot=\sum_i (y_i - \bar{y})^2` is total sum of squares. Can also calculate |
| 34 | + adjusted r2 score given by |
| 35 | +
|
| 36 | + .. math:: R^2_adj = 1 - \frac{(1-R^2)(n-1)}{n-k-1} |
| 37 | +
|
| 38 | + where the parameter :math:`k` (the number of independent regressors) should |
| 39 | + be provided as the `adjusted` argument. |
| 40 | +
|
| 41 | + Forward accepts |
| 42 | +
|
| 43 | + - ``preds`` (float tensor): ``(N,)`` or ``(N, M)`` (multioutput) |
| 44 | + - ``target`` (float tensor): ``(N,)`` or ``(N, M)`` (multioutput) |
| 45 | +
|
| 46 | + In the case of multioutput, as default the variances will be uniformly |
| 47 | + averaged over the additional dimensions. Please see argument `multioutput` |
| 48 | + for changing this behavior. |
| 49 | +
|
| 50 | + Args: |
| 51 | + num_outputs: |
| 52 | + Number of outputs in multioutput setting (default is 1) |
| 53 | + adjusted: |
| 54 | + number of independent regressors for calculating adjusted r2 score. |
| 55 | + Default 0 (standard r2 score). |
| 56 | + multioutput: |
| 57 | + Defines aggregation in the case of multiple output scores. Can be one |
| 58 | + of the following strings (default is ``'uniform_average'``.): |
| 59 | +
|
| 60 | + * ``'raw_values'`` returns full set of scores |
| 61 | + * ``'uniform_average'`` scores are uniformly averaged |
| 62 | + * ``'variance_weighted'`` scores are weighted by their individual variances |
| 63 | +
|
| 64 | + compute_on_step: |
| 65 | + Forward only calls ``update()`` and return None if this is set to False. default: True |
| 66 | + dist_sync_on_step: |
| 67 | + Synchronize metric state across processes at each ``forward()`` |
| 68 | + before returning the value at the step. default: False |
| 69 | + process_group: |
| 70 | + Specify the process group on which synchronization is called. default: None (which selects the entire world) |
| 71 | +
|
| 72 | + Example: |
| 73 | +
|
| 74 | + >>> from pytorch_lightning.metrics import R2Score |
| 75 | + >>> target = torch.tensor([3, -0.5, 2, 7]) |
| 76 | + >>> preds = torch.tensor([2.5, 0.0, 2, 8]) |
| 77 | + >>> r2score = R2Score() |
| 78 | + >>> r2score(preds, target) |
| 79 | + tensor(0.9486) |
| 80 | +
|
| 81 | + >>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]]) |
| 82 | + >>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]]) |
| 83 | + >>> r2score = R2Score(num_outputs=2, multioutput='raw_values') |
| 84 | + >>> r2score(preds, target) |
| 85 | + tensor([0.9654, 0.9082]) |
| 86 | + """ |
| 87 | + def __init__( |
| 88 | + self, |
| 89 | + num_outputs: int = 1, |
| 90 | + adjusted: int = 0, |
| 91 | + multioutput: str = "uniform_average", |
| 92 | + compute_on_step: bool = True, |
| 93 | + dist_sync_on_step: bool = False, |
| 94 | + process_group: Optional[Any] = None, |
| 95 | + dist_sync_fn: Callable = None, |
| 96 | + ): |
| 97 | + super().__init__( |
| 98 | + compute_on_step=compute_on_step, |
| 99 | + dist_sync_on_step=dist_sync_on_step, |
| 100 | + process_group=process_group, |
| 101 | + dist_sync_fn=dist_sync_fn, |
| 102 | + ) |
| 103 | + |
| 104 | + self.num_outputs = num_outputs |
| 105 | + |
| 106 | + if adjusted < 0 or not isinstance(adjusted, int): |
| 107 | + raise ValueError('`adjusted` parameter should be an integer larger or' |
| 108 | + ' equal to 0.') |
| 109 | + self.adjusted = adjusted |
| 110 | + |
| 111 | + allowed_multioutput = ('raw_values', 'uniform_average', 'variance_weighted') |
| 112 | + if multioutput not in allowed_multioutput: |
| 113 | + raise ValueError( |
| 114 | + f'Invalid input to argument `multioutput`. Choose one of the following: {allowed_multioutput}' |
| 115 | + ) |
| 116 | + self.multioutput = multioutput |
| 117 | + |
| 118 | + self.add_state("sum_squared_error", default=torch.zeros(self.num_outputs), dist_reduce_fx="sum") |
| 119 | + self.add_state("sum_error", default=torch.zeros(self.num_outputs), dist_reduce_fx="sum") |
| 120 | + self.add_state("residual", default=torch.zeros(self.num_outputs), dist_reduce_fx="sum") |
| 121 | + self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum") |
| 122 | + |
| 123 | + def update(self, preds: torch.Tensor, target: torch.Tensor): |
| 124 | + """ |
| 125 | + Update state with predictions and targets. |
| 126 | +
|
| 127 | + Args: |
| 128 | + preds: Predictions from model |
| 129 | + target: Ground truth values |
| 130 | + """ |
| 131 | + sum_squared_error, sum_error, residual, total = _r2score_update(preds, target) |
| 132 | + |
| 133 | + self.sum_squared_error += sum_squared_error |
| 134 | + self.sum_error += sum_error |
| 135 | + self.residual += residual |
| 136 | + self.total += total |
| 137 | + |
| 138 | + def compute(self) -> torch.Tensor: |
| 139 | + """ |
| 140 | + Computes r2 score over the metric states. |
| 141 | + """ |
| 142 | + return _r2score_compute(self.sum_squared_error, self.sum_error, self.residual, |
| 143 | + self.total, self.adjusted, self.multioutput) |
0 commit comments