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

R2s from .predict() and .test() do not match #2874

Open
giusmatera opened this issue Dec 19, 2024 · 1 comment
Open

R2s from .predict() and .test() do not match #2874

giusmatera opened this issue Dec 19, 2024 · 1 comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.4.x

Comments

@giusmatera
Copy link

giusmatera commented Dec 19, 2024

Hi, I found what it seems to be a bug:

I have

from torchmetrics import MeanSquaredError, R2Score

	def test_step(self, batch, batch_idx):
	
	    batch = batch.to(torch.float32)
	
	    pooled_output = batch[
	        :, :-1
	    ].float()  # All columns except the last
	    targets = batch[:, -1].float()
	
	    predictions = self(pooled_output).squeeze(-1)
	
	    score_r2 = self.test_r2(predictions, targets)  # Compute R2
	    self.log("test_r2", score_r2, on_epoch=True)
	
	    return score_r2

but the test r2 score this step gives differs from what I retrieve directly from predictions and actual values.

To clarify, the r2 score in results = trainer.test(regressor_model, test_dataloader) is different from what I get from

predictions = trainer.predict(regressor_model, test_dataloader)
torchmetrics.R2Score()(predictions, error)

Also, torchmetrics and sklearn r2 functions match in the latter task. How come? Thanks a lot.

Info:

  • torchmetrics' version is 1.4.2
@giusmatera giusmatera added bug / fix Something isn't working help wanted Extra attention is needed labels Dec 19, 2024
Copy link

Hi! thanks for your contribution!, great first issue!

@Borda Borda added the v1.4.x label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.4.x
Projects
None yet
Development

No branches or pull requests

2 participants