R2 Score¶
Module Interface¶
- class torchmetrics.R2Score(num_outputs=1, adjusted=0, multioutput='uniform_average', compute_on_step=None, **kwargs)[source]
Computes r2 score also known as R2 Score_Coefficient Determination:

where
is the sum of residual squares, and
is total sum of squares. Can also calculate
adjusted r2 score given by
where the parameter
(the number of independent regressors) should be provided as the adjusted argument.Forward accepts
preds(float tensor):(N,)or(N, M)(multioutput)target(float tensor):(N,)or(N, M)(multioutput)
In the case of multioutput, as default the variances will be uniformly averaged over the additional dimensions. Please see argument
multioutputfor changing this behavior.- Parameters
num_outputs¶ (
int) – Number of outputs in multioutput setting (default is 1)adjusted¶ (
int) – number of independent regressors for calculating adjusted r2 score.Defines aggregation in the case of multiple output scores. Can be one of the following strings (default is
'uniform_average'.):'raw_values'returns full set of scores'uniform_average'scores are uniformly averaged'variance_weighted'scores are weighted by their individual variances
compute_on_step¶ (
Optional[bool]) –Forward only calls
update()and returns None if this is set to False.Deprecated since version v0.8: Argument has no use anymore and will be removed v0.9.
kwargs¶ (
Dict[str,Any]) – Additional keyword arguments, see Advanced metric settings for more info.
- Raises
ValueError – If
adjustedparameter is not an integer larger or equal to 0.ValueError – If
multioutputis not one of"raw_values","uniform_average"or"variance_weighted".
Example
>>> from torchmetrics import R2Score >>> target = torch.tensor([3, -0.5, 2, 7]) >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> r2score = R2Score() >>> r2score(preds, target) tensor(0.9486)
>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]]) >>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]]) >>> r2score = R2Score(num_outputs=2, multioutput='raw_values') >>> r2score(preds, target) tensor([0.9654, 0.9082])
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Functional Interface¶
- torchmetrics.functional.r2_score(preds, target, adjusted=0, multioutput='uniform_average')[source]
Computes r2 score also known as R2 Score_Coefficient Determination:

where
is the sum of residual squares, and
is total sum of squares. Can also calculate
adjusted r2 score given by
where the parameter
(the number of independent regressors) should
be provided as the adjustedargument.- Parameters
adjusted¶ (
int) – number of independent regressors for calculating adjusted r2 score.Defines aggregation in the case of multiple output scores. Can be one of the following strings:
'raw_values'returns full set of scores'uniform_average'scores are uniformly averaged'variance_weighted'scores are weighted by their individual variances
- Raises
ValueError – If both
predsandtargetsare not1Dor2Dtensors.ValueError – If
len(preds)is less than2since at least2sampels are needed to calculate r2 score.ValueError – If
multioutputis not one ofraw_values,uniform_averageorvariance_weighted.ValueError – If
adjustedis not anintegergreater than0.
Example
>>> from torchmetrics.functional import r2_score >>> target = torch.tensor([3, -0.5, 2, 7]) >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> r2_score(preds, target) tensor(0.9486)
>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]]) >>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]]) >>> r2_score(preds, target, multioutput='raw_values') tensor([0.9654, 0.9082])
- Return type