Loss R2#
r2
#
| CLASS | DESCRIPTION |
|---|---|
R2Loss |
Compute \(R^2\) loss with optional weighting. |
| FUNCTION | DESCRIPTION |
|---|---|
r2_loss |
Compute \(R^2\) loss with optional weighting. |
Classes#
R2Loss(reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, context_prefix: str | None = None)
#
Bases: Loss
Compute \(R^2\) loss with optional weighting.
Computes \(R^2 = 1 - \frac{\sum w_i (y_i - \hat{y_i})^2}{\sum w_i (y_i - \bar{y})^2}\), where \(y_i\) are the target values, \(\hat{y_i}\) are the predicted values, \(\bar{y}\) is the mean of the predicted values, and \(w_i\) are optional weights.
For multidimensional targets, computes per-feature means and aggregates the total sum of squares across all features.
| PARAMETER | DESCRIPTION |
|---|---|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute \(R^2\) loss. |
Source code in spectre/loss/r2.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute \(R^2\) loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Context dictionary containing
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
\(R^2\) loss. |
Source code in spectre/loss/r2.py
Functions#
r2_loss(target: torch.Tensor, target_pred: torch.Tensor, weights: torch.Tensor | None = None, reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0) -> torch.Tensor
#
Compute \(R^2\) loss with optional weighting.
Computes \(R^2 = 1 - \frac{\sum w_i (y_i - \hat{y_i})^2}{\sum w_i (y_i - \bar{y})^2}\), where \(y_i\) are the target values, \(\hat{y_i}\) are the predicted values, \(\bar{y}\) is the mean of the predicted values, and \(w_i\) are optional weights.
For multidimensional targets, computes per-feature means and aggregates the total sum of squares across all features.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
Target values.
TYPE:
|
target_pred
|
Predicted values.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Loss value. |