question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Contrib metrics] Add R2 score metric

See original GitHub issue

Maybe adding R2 metric to ignite.contrib.metrics.regression could make sense. Thoughts ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vfdev-5commented, Nov 17, 2020

@silas2471 in ignite R2 is implemented as a metric to compute in online fashion (accumulation batch by batch). This is not reusable as a loss function. However, R2 definition is not difficult to reimplement as a loss function for a single batch:

import numpy as np
from sklearn.metrics import r2_score
import torch
import torch.nn as nn
from torch.nn import functional as F


class R2Loss(nn.Module):
    
    def forward(self, y_pred, y):
        var_y = torch.var(y, unbiased=False)
        return 1.0 - F.mse_loss(y_pred, y, reduction="mean") / var_y


size = 51
np_y_pred = np.random.rand(size,)
np_y = np.random.rand(size,)

criterion = R2Loss()
y_pred = torch.from_numpy(np_y_pred)
y = torch.from_numpy(np_y)

res = criterion(y_pred, y)
print(r2_score(np_y, np_y_pred), res.item())
0reactions
silas2471commented, Nov 17, 2020

Hey, is it also possible to use this for deep learning as loss function? I couldnt find any documentation on an implementation yet. Is there an easy way to implement it for such a use case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

R2 Score — PyTorch-Metrics 0.11.0 documentation
Initializes internal Module state, shared by both nn. Module and ScriptModule. Computes r2 score over the metric states. Update state with predictions and ......
Read more >
KerasRegressor Coefficient of Determination R^2 Score
It's a handy metric because it shows values up to 1.0 (similar to percent accuracy in classification). Is my usage of Keras backend...
Read more >
R2Score — PyTorch-Ignite v0.4.10 Documentation
class ignite.contrib.metrics.regression.R2Score(output_transform=<function Metric. ... Calculates the R-Squared, the coefficient of determination.
Read more >
scikit-learn score metric on the coefficient of determination $R^2
Returns the coefficient of determination R^2 of the prediction. The coefficient R^2 is defined as (1 - u/v), where u is the regression...
Read more >
tfa.metrics.RSquare | TensorFlow Addons
Compute R^2 score. ... of the same metric. Can also calculate the Adjusted R2 Score. ... metric = tfa.metrics.r_square.RSquare()
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found