Repository navigation
[MRG] Fix bug in graph lasso when n_features=2 - #13276
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 |
jnothman
left a comment
There was a problem hiding this comment.
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 ! |
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.ascontiguousarraydoes not always copy the original array:This problem led to the matrix
covariance_being modified whensub_covarianceis 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.ascontiguousarrayto make a copy whenn_features>2:When
n_features=2, the output is a single value (which is already contiguous), hence the bug.