Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rsquare loss is added #1313

Merged
merged 2 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions catalyst/contrib/nn/criterion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
HuberLossV0,
CategoricalRegressionLoss,
QuantileRegressionLoss,
RSquareLoss,
)
from catalyst.contrib.nn.criterion.supervised_contrastive import SupervisedContrastiveLoss
from catalyst.contrib.nn.criterion.trevsky import FocalTrevskyLoss, TrevskyLoss
Expand Down
20 changes: 19 additions & 1 deletion catalyst/contrib/nn/criterion/regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
from torch import nn
from torch.nn import functional as F


def _ce_with_logits(logits, target):
Expand Down Expand Up @@ -99,4 +100,21 @@ def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
return loss


__all__ = ["HuberLossV0", "CategoricalRegressionLoss", "QuantileRegressionLoss"]
class RSquareLoss(nn.Module):
"""RSquareLoss"""

def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
"""Compute the loss.

Args:
outputs (torch.Tensor): model outputs
targets (torch.Tensor): targets

Returns:
torch.Tensor: computed loss
"""
var_y = torch.var(targets, unbiased=False)
return 1.0 - F.mse_loss(outputs, targets, reduction="mean") / var_y


__all__ = ["HuberLossV0", "CategoricalRegressionLoss", "QuantileRegressionLoss", "RSquareLoss"]
7 changes: 7 additions & 0 deletions docs/api/contrib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ QuantileRegressionLoss
:undoc-members:
:show-inheritance:

RSquareLoss
*************************
.. autoclass:: catalyst.contrib.nn.criterion.regression.RSquareLoss
:members:
:undoc-members:
:show-inheritance:

RecSys
""""""

Expand Down