Skip to content

A mistake about spectral clustering with amg solver #10715

Closed

Description

@sky88088

I found a problem in the code of spectral clustering.

When using the amg solver and "n_nodes < 5 * n_components", it will firstly generate an embedding with arpack solver. However, this generated embedding would not be return. And the transformed laplacian matrix would be used to generate a new embedding with amg solver. Since the laplacian has been changed in the arpack part, the new embedding is wrong.

It seems to be a bug.

Activity

  1. jnothman commented on Feb 27, 2018

    @jnothman
    Member

    I'm not especially familiar with the algorithm. Would you be able to offer a fix, ideally with a test that illustrates what is currently broken?

  2. sky88088 commented on Feb 28, 2018

    @sky88088
    Author

    @jnothman I can only offer a simple fix for this problem. Since this is a small mistake if I'm right, maybe you can just accept my patch or take it as a reference.

  3. lesteve commented on Feb 28, 2018

    @lesteve
    Member

    @sky88088 what would be really good if you could one (or more) of these things:

    • give a stand-alone snippet reproducing the problem.
    • point to the lines of code you mention. You can point to lines of code inside comments like this.
    • open a PR. Have a look at our contribution guidelines if you go down this route.

    cc @jmargeta and @glemaitre who have worked on this a little while ago IIRC and who may have some insights.

  4. glemaitre commented on Feb 28, 2018

    @glemaitre
    Member

    I also missing insights and just try to connect the dots. From what I can see:

    • pyamg should be used with sparse matrix
    • amg is a preconditionner before to use lobcpg
    • lobcpg does not work with n_nodes < 5 and should default on eigh
    • when arpack failed, it should fallback to the lobcpg
    • lobcpg and arpack accepts both dense and sparse.

    I have the impression that we should probably clean some of the logic there.

    if not sparse.isparse(laplacian) and eigen_solver == 'amg':
        # warns to we switch to arpack
        eigen_solver = 'arpack'
    
    if eigen_solver == 'arpack':
        # solve the problem
        try:
            ...
        except RuntimeError:
            eigen_solver = 'lobcpg'
    
    # from that point eigen_solver will be either amg or lobcpg
    # check if we have enough n_nodes
    if n_nodes < 5:
        # use eigh
    else:
        # check that we should precondition
        if eigen_solver == 'amg':
            # preconditionne and get M
        # call lobcpg with M if available otherwise go for default
  5. glemaitre commented on Feb 28, 2018

    @glemaitre
    Member

    @lesteve @jnothman could you check if my guesses seem correct or not.

  6. glemaitre commented on Feb 28, 2018

    @glemaitre
    Member

    Also currently we don't test pyamg with dense matrix so we never get into the error reported for sure.

  7. sky88088 commented on Mar 1, 2018

    @sky88088
    Author

    I hold the same idea with @glemaitre and my patch is adding the missing code in the arpack part:

    if eigen_solver == 'amg':
        # warns to we switch to arpack
        eigen_solver = 'arpack'

    Of course, the eigen solver type can also be resigned before the arpack part.

  8. jmargeta commented on Mar 1, 2018

    @jmargeta
    Contributor

    The idea behind @sky88088 patch looks correct and the logic of spectral embedding definitely looks like a good candidate for simplification. The selection of the eigen solver should probably be untangled from the execution as much as possible.

    After playing with that idea for a bit, it also revealed some places of potential code clean-up. https://gist.github.com/jmargeta/c393248ea7149b09e6ba5cfc6de8a16d (untested).

    I feel that the whole logic of spectral_embedding needs a more complete test coverage before any changes take place. Otherwise we risk of coming back to this issue again and again, whenever any of the solvers crashes.

  9. glemaitre commented on Mar 2, 2018

    @glemaitre
    Member

    I feel that the whole logic of spectral_embedding needs a more complete test coverage before any changes take place. Otherwise we risk of coming back to this issue again and again, whenever any of the solvers crashes.

    Yes the PR needs to come with some regression test for sure.

  10. amueller commented on Aug 7, 2019

    @amueller
    Member
  11. self-assigned this
    on Aug 12, 2019
  12. amueller commented on Aug 13, 2019

    @amueller
    Member

    quite convinced that #14647 is the correct fix, also contains a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions