-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG] Fix bug in graph lasso when n_features=2 #13276
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
Conversation
I will try to add some tests |
See my update in the PR text: it looks like the bug has small scope as it only occurs when |
Interesting but makes sense that it only happens when n_features=2; because it ends up with a size-1 tensor, right? This is the only case when such a slicing could yield a contiguous view; otherwise you can't avoid crossing some strides. Neat, I guess... 😵 the test is a good idea |
Yes, this is exactly what happens |
I added a test for the case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have your approval @vene? At a glance it looks good to me, but I don't feel I've looked carefully at the context.
Please add an entry to the change log at doc/whats_new/v0.20.rst
under version 0.20.3. Like the other entries there, please reference this pull request with :issue:
and credit yourself (and other contributors if applicable) with :user:
@jnothman the change itself has my approval, yes. Since I'm out of date with our deprecation policies, i was waiting for someone else to weigh in on whether there should be an added test for the deprecated |
All done ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks @bellet!
My policy is that worrying about this sort of question for deprecated things is more effort than it's worth. |
thx @bellet |
* fix bug in graph lasso * better fix * add test for case n_features=2 * remove test for deprecated version * update whats new * note that the bug was a regression
* fix bug in graph lasso * better fix * add test for case n_features=2 * remove test for deprecated version * update whats new * note that the bug was a regression
* fix bug in graph lasso * better fix * add test for case n_features=2 * remove test for deprecated version * update whats new * note that the bug was a regression
Reference Issues/PRs
Fixes a bug introduced in #9858
Related to some discussions in #12228
What does this implement/fix? Explain your changes.
This PR fixes a subtle bug in the graph lasso implementation, which was introduced in PR #9858 proposing an online way to update the matrix
sub_covariance
(leading to significant speed ups for large dense matrices). This bug affects both the CD and LARS solvers as it is in the outer loop.More precisely, the problem originates from the fact that
np.ascontiguousarray
does not always copy the original array:This problem led to the matrix
covariance_
being modified whensub_covariance
is updated, which is not correct.Example of incorrect behavior on a simple PSD matrix taken from #12228 (comment):
With this PR:
This is consistent with the output of graph lasso as implemented in package skggm:
UPDATE: the problem arises only when
n_features=2
, hence has small scope. This explains why the tests passed for PR #9858. The root cause of the problem can still valid for larger matrices, as seen in this example:However slicing the two dimensions at the same time (as done for graph lasso) forces
np.ascontiguousarray
to make a copy whenn_features>2
:When
n_features=2
, the output is a single value (which is already contiguous), hence the bug.