8000 Raise ValueError instead of RuntimeWarning for LinearModelLoss by akaashp2000 · Pull Request #27332 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

Raise ValueError instead of RuntimeWarning for LinearModelLoss #27332

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

akaashp2000
Copy link
@akaashp2000 akaashp2000 commented Sep 10, 2023

Reference Issues/PRs

Fixes #27016

What does this implement/fix? Explain your changes.

Fixes an issue where PoissonRegressor (and other regressors) encounter a RuntimeWarning, without any suggestion of the cause or ways to fix.

The fix captures the RuntimeWarning and raises a ValueError instead, in the case where the gradient vector contains invalid entries (e.g. inf, null), during a matmul. It also has a message suggesting scaling of features or target variable, or to use another solver.

Any other comments?

So far I have just tested that this exception/message occur on the same test data where I encountered the issue (see the issue #27016).

I have included the same change for the case where the loss is multiclass, but haven't directly tested this on a multiclass problem.

The change caused some pre-existing pytests to start failing, due to warning that would have been raised being replaced by a ValueError as part of the issue. I have included a change to scale the X dataset so that this error doesn't occur to get those tests passing.

Questions:

  1. Is the update to the pytest valid? And if it is, should it be part of this PR or a separate one?
  2. Could throwing a ValueError rather than just a warning break some pipelines/uses of linear loss that are currently 'working'? ('Working' in the sense that there is only a RuntimeWarning, not an error/exception) If so, is it possible (or better even) to instead just update the message on the RuntimeWarning?
  3. There are still 3 checks that are failing - how could they be resolved?

@github-actions
Copy link
github-actions bot commented Sep 10, 2023

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 9585d68. Link to the linter CI: here

@akaashp2000 akaashp2000 changed the title Raise ValueError instead of RuntimeWarning for LinearModelLoss [WIP] Raise ValueError instead of RuntimeWarning for LinearModelLoss Sep 10, 2023
@akaashp2000 akaashp2000 marked this pull request as ready for review September 10, 2023 20:19
@akaashp2000 akaashp2000 changed the title [WIP] Raise ValueError instead of RuntimeWarning for LinearModelLoss Raise ValueError instead of RuntimeWarning for LinearModelLoss Sep 10, 2023
@akaashp2000 akaashp2000 force-pushed the issue27016_raise_value_error branch from a278528 to a6172b6 Compare September 10, 2023 20:31
Copy link
Member
@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix the codecov error, you need to add non-regression test. Basically, it would be a test that would raise the new ValueError and ensure that we raise the proper error message (this is your original issue).

Then, you need to add an entry in the changelog doc/whats_new/1.4.rst to acknowledge the bug fix. This will solve the over CI failure.

with np.errstate(all="raise"):
try:
grad[:n_features] = X.T @ grad_pointwise + l2_reg_strength * weights
except FloatingPointError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except FloatingPointError:
except FloatingPointError as exc:

Copy link
@akaashpatelmns akaashpatelmns Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use exc as you have in this suggestion, testing on the dataset from I get the following:

---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)
File [~/Downloads/scikit-learn/sklearn/linear_model/_linear_loss.py:296](https://file+.vscode-resource.vscode-cdn.net/Users/d0055315_akaash_patel/Downloads/scikit-learn/scratchpad~/~/Downloads/scikit-learn/sklearn/linear_model/_linear_loss.py:296), in LinearModelLoss.loss_gradient(self, coef, X, y, sample_weight, l2_reg_strength, n_threads, raw_prediction)
    [295] try:
--> [296]     grad[:n_features] = X.T @ grad_pointwise + l2_reg_strength * weights
    [297]     print("min entry", np.min(grad_pointwise))

FloatingPointError: invalid value encountered in matmul

The above exception was the direct cause of the following exception:

so it still includes the original exception that I am "wrapping" with a ValueError and message. Do you reckon it's better this way?

raise ValueError(
"Overflow detected. Try scaling the target variable or"
" features, or using a different solver"
) from None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) from None
) from exc

grad[:, :n_features] = (
grad_pointwise.T @ X + l2_reg_strength * weights
)
except FloatingPointError:
Copy link
Member

Choose a reason 10000 for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except FloatingPointError:
except FloatingPointError as exc:

raise ValueError(
"Overflow detected. Try scaling the target variable or"
" features, or using a different solver"
) from None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) from None
) from exc

if self.fit_intercept:
grad[-1] = grad_pointwise.sum()
else:
grad = np.empty((n_classes, n_dof), dtype=weights.dtype, order="F")
# grad_pointwise.shape = (n_samples, n_classes)
grad[:, :n_features] = grad_pointwise.T @ X + l2_reg_strength * weights
with np.errstate(all="raise"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is an overflow, I think that using over="raise" would be better.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using over="raise", however this didn't raise an Exception and the RuntimeWarning appeared instead - when I tested with the dataset I encountered this issue with in the first place #27016. During one of the iterations, there was an infinity in one the gradients which is where I wanted to raise the Exception.

@akaashp2000 akaashp2000 force-pushed the issue27016_raise_value_error branch from 3b8c7e7 to 0a58a75 Compare October 7, 2023 13:49
@akaashp2000 akaashp2000 force-pushed the issue27016_raise_value_error branch from 0a58a75 to fce1afc Compare October 26, 2023 22:02
@glemaitre glemaitre self-requested a review October 30, 2023 09:20
Copy link
Member
@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akaashp2000 Could you address the review and add a changelog entry and a non-regression test?

@akaashp2000 akaashp2000 force-pushed the issue27016_raise_value_error branch from fce1afc to d1c7ebf Compare November 2, 2023 18:20
@akaashp2000 akaashp2000 force-pushed the issue27016_raise_value_error branch from d1c7ebf to 9585d68 Compare November 3, 2023 21:33
@glemaitre
Copy link
Member

@akaashp2000 Could you request a review when the PR is ready for review.
I just don't want to loose this PR in my notification.

@akaashp2000
Copy link
Author

@glemaitre Hi, sorry for the slowness on this issue.

Yes I'll address the non-regression test and changelog this weekend.

A couple of other questions have come up too regarding the remaining suggestions you have made on the PR - which I'll reply to directly.

@akaashp2000
Copy link
Author

@glemaitre while trying to write the non-regression test, I'm testing with the breast cancer data.

I multiplied all values in the first column of X_breast_cancer by 100, which meant that ValueError was now raised, even though all entries in the gradient vector seemed okay (no infinities or NaNs, there were zeroes and very small values like 0 or 1.03566975e-157).

stes added a commit to stes/scikit-learn that referenced this pull request Aug 16, 2024
stes added a commit to stes/scikit-learn that referenced this pull request Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

PoissonRegressor lbfgs solver giving coefficients of 0 and Runtime Warning
4 participants
0