Skip to content

Spectral clustering with lobpcg solver is unstable #10278

Closed

Description

@jmargeta

Description

Spectral clustering with lobpcg solver appears to be unstable.
An exception is thrown on windows CI build while the code runs well on Linux.
On Linux, the solver is sensitive to tiny image variations.
See #9062 for more details.

This might be because the problem is not particularly well preconditioned.
However at least the behaviour should be consistent across all platforms.

Steps/Code to Reproduce

import numpy as np
from sklearn.cluster import spectral_clustering
from sklearn.feature_extraction import img_to_graph

# make an image with two circles and construct a graph
x, y = np.indices((40, 40))
center1, center2 = (14, 12), (20, 25)
radius1, radius2 = 8, 7

circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2

circles = circle1 | circle2
mask = circles.copy()
img = circles.astype(float)

graph = img_to_graph(img, mask=mask)
graph.data = np.exp(-graph.data / graph.data.std())

# this fails on windows for seemingly random versions of Python
# on linux the results can be sensitive to tiny image variations 
labels = spectral_clustering(graph, n_clusters=2, eigen_solver='lobpcg', random_state=0)

On Linux, when adding random noise to the image, tiny image variations (img = img + 1 + 0.05 * rand.random(*img.shape) vs img += 1 + 0.05 * rand.random(*img.shape) can throw of convergence of the lobpcg solver.

Expected Results

The returned labels of the clusters should be identical to the other two solvers:

labels_arpack = spectral_clustering(graph, n_clusters=2, eigen_solver='arpack', random_state=0)

or

labels_amg = spectral_clustering(graph, n_clusters=2, eigen_solver='amg', random_state=0)

i.e.
clustering_success

Actual Results

Exception is thrown on windows - no noise to the images applied See
E numpy.linalg.linalg.LinAlgError: the leading minor of order 5 of 'b' is not positive definite. The factorization of 'b' could not be completed and no eigenvalues or eigenvectors were computed.

Solver sensitivity to small image variations See

labels_arpack = array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)

labels_lobpcg = array([1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,

       1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,...1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,

       1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1], dtype=int32)

Or the failed result:
clustering_fail

Versions

The exception is thrown on scikit-learn's CI, windows build.
PYTHON=C:\Python35, PYTHON_VERSION=3.5.0, PYTHON_ARCH=32

The sensitivity to tiny image variations was found on:
Linux-4.10.0-40-generic-x86_64-with-Ubuntu-16.04-xenial
Python 3.6.3 (default, Oct 6 2017, 08:44:35)
[GCC 5.4.0 20160609]
NumPy 1.13.3
SciPy 1.0.0
Scikit-Learn 0.20.dev0

Activity

  1. lobpcg commented on Aug 31, 2018

    @lobpcg
    Contributor

    It appears to be the same bug as in #6489

  2. added a commit that references this issue on Sep 1, 2018
  3. massich commented on Sep 1, 2018

    @massich
    Contributor

    It appears to be the same bug as in #6489

    @lobpcg I'm not really sure that they are related. @jmargeta I'm trying to replicate it on my machine but it works fine for me. I opened #11965 to see if any CI complains. Maybe I'll have to run the test with several seeds to see if it breaks

  4. glemaitre commented on Sep 1, 2018

    @glemaitre
    Member

    On the top of the head, @massich be sure that we have the package linked to the solver installed on appveyor.

  5. lobpcg commented on Sep 1, 2018

    @lobpcg
    Contributor

    It is easy to check if it is the same bug. One can set in lobpcg the value verbosityLevel = 1 to see the screen output after every iteration. The most important output to check is residual norms. The lobpcg code stops when all residual norms are below the tolerance. If the max residual norms drops below some value, which is larger than the tolerance, but then starts growing and soon crashes, may an indication of an ill-posed problem. However, this is still not a good enough excuse for the code to crash. One may simply need just to set the tolerance larger so the code stops in the proper time. Excessively small tolerance would likely make lobpcg always crash. See more at #6489 (comment)

  6. lobpcg commented on Sep 3, 2018

    @lobpcg
    Contributor

    #11968 apparently fixes the problem.

  7. jmargeta commented on Sep 3, 2018

    @jmargeta
    ContributorAuthor

    @massich, good point, I cannot replicate it anymore. I think that this can be closed.
    The eigen_tol suggestion makes sense to me.

  8. massich commented on Sep 11, 2018

    @massich
    Contributor

    @jmargeta @glemaitre I think you can close this if you think so

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions