Repository navigation
ENH Fix the eigsh call in spectral embedding - #33262
Conversation
|
Oh wow, some tests fail but only in some environments (on my local machine all tests pass). At the moment I have no idea why. |
|
You can set |
|
@ogrisel Thanks, I did not know about it. But this does not seem to be the problem here. On my machine passes for all seeds, but here we see some failing tests in This must be due to some ARPACK-related differences, right? I only changed one single thing that only affects one |
|
OK it seems the issue was due to conversion between different |
|
OK I followed an advice of @lobpcg found in some very old comment thread #14647 (comment) and updated the PR accordingly. Tests look better now, but some tests still fail in some environments. One of the failing tests is |
|
Status update. I have fixed everything and all tests now pass :-) I added one new test and had to fiddle with some of the old tests to make them less fragile. This is ready for review @ogrisel. In the process I changed my actual fix to the So this PR boils down to some refactoring and improved tests, but I do not claim increased runtime... What do you think? |
|
As you say, it looks like in the end we can't get the performance benefit we were hoping, and what I'm not sure of, is if the change is required. Would it make sense to only update the inline comment for future contributors not to chase the same route as you did and leave it at that? |
|
I support the proposed changes. The PR fixes the current undocumented and meaningless setup. It also removes simply unnecessary operations -L thus has performance benefits even though not as dramatic as originally hoped for. |
|
@adrinjalali @lobpcg I ran the runtime comparison a bit more systematically (multiple runs for two sample sizes) and the good news is, I do see a runtime improvement after all! The code is: X, _ = make_swiss_roll(n_samples=N) # I tried 5000 and 10000
%time Z = SpectralEmbedding(eigen_solver="arpack", affinity="rbf", eigen_tol=0).fit_transform(X) |
|
@lobpcg In the code, if ARPACK with shift-invert fails with a RuntimeError, then LOBPCG is used instead. Do you know how to trigger this situation? I would like to add a unit test that checks that this situation is handled correctly, but I have no idea how to trigger this. try:
...
... = eigsh(laplacian, k=n_components, sigma=-1e-5, which="LM", tol=tol, v0=v0)
...
except RuntimeError:
# When submatrices are exactly singular, an LU decomposition
# in arpack fails. We fallback to lobpcg
warnings.warn("ARPACK has failed, falling back to LOBPCG.")
eigen_solver = "lobpcg" |
Choose sigma to make the shifted Laplacian singular, i.e. use an eigenvalue of the Laplacian. Precompute it a priori for the given example and hard code it to save the test time. |
|
@adrinjalali Gentle ping to check out my updated more systematic runtime comparison above :-) |
I thought it was in a unit test not the function itself. Well, ARPACK should not fail for that specific reason with such a call in theory but it's a complicated algorithm so I would keep the check to be safe. Only maybe change the comment to reflect the current status of the choice of the shift in this PR. |
|
@antoinebaker Maybe you would also be interested in taking a look at this PR. Would be great to have your review! Cheers. |
antoinebaker
left a comment
There was a problem hiding this comment.
Thanks for the PR @dkobak ! The code path for the dealing with the eigen_solver is much clearer now.
LGTM overall, a few minor comments below.
Co-authored-by: antoinebaker <[email protected]>
|
Thanks @antoinebaker, all suggestions merged/implemented. |
test_spectral_embedding_lobpcg_solver test_precomputed_nearest_neighbors_filtering

Fixes #33242
This fixes the way
eigsh()is called in spectral embedding. As a result, it gets faster:On my machine, this runs in 24 seconds on current main, and in 11 second after my fix. I am using
affinity="rbf"here to get a denseN x Naffinity matrix (with sparse matrix it's much faster).