Shortcuts

# torcheval.metrics.functional.r2_score¶

torcheval.metrics.functional.r2_score(input: Tensor, target: Tensor, *, multioutput: str = 'uniform_average', num_regressors: int = 0)

Compute R-squared score, which is the proportion of variance in the dependent variable that can be explained by the independent variable. Its class version is torcheval.metrics.R2Score.

Parameters:
• input – Tensor of predicted values with shape of (n_sample, n_output).

• target – Tensor of ground truth values with shape of (n_sample, n_output).

• multioutput (Optional) –

• 'uniform_average' [default]:

Return scores of all outputs are averaged with uniform weight.

• 'raw_values':

Return a full set of scores.

• variance_weighted:

Return scores of all outputs are averaged with weighted by the variances of each individual output.

• num_regressors (Optional) – Number of independent variables used, applied to adjusted R-squared score. Defaults to zero (standard R-squared score).

Raises:

ValueError

• If value of multioutput does not exist in (raw_values, uniform_average, variance_weighted). - If value of num_regressors is not an integer in the range of [0, n_samples - 1].

Examples:

>>> import torch
>>> from torcheval.metrics.functional import r2_score
>>> input = torch.tensor([0, 2, 1, 3])
>>> target = torch.tensor([0, 1, 2, 3])
>>> r2_score(input, target)
tensor(0.6)

>>> input = torch.tensor([[0, 2], [1, 6]])
>>> target = torch.tensor([[0, 1], [2, 5]])
>>> r2_score(input, target)
tensor(0.6250)

>>> input = torch.tensor([[0, 2], [1, 6]])
>>> target = torch.tensor([[0, 1], [2, 5]])
>>> r2_score(input, target, multioutput="raw_values")
tensor([0.5000, 0.7500])

>>> input = torch.tensor([[0, 2], [1, 6]])
>>> target = torch.tensor([[0, 1], [2, 5]])
>>> r2_score(input, target, multioutput="variance_weighted")
tensor(0.7000)

>>> input = torch.tensor([1.2, 2.5, 3.6, 4.5, 6])
>>> target = torch.tensor([1, 2, 3, 4, 5])
>>> r2_score(input, target, multioutput="raw_values", num_regressors=2)
tensor(0.6200)


## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials