Repository navigation
Closed
Labels
Milestone
Description
I am trying to fit a spectral clustering model on a 50 X 50 symmetric adjacency matrix:
from sklearn.cluster import SpectralClustering
labels = SpectralClustering(n_clusters=5,
affinity="precomputed",
assign_labels="kmeans",
random_state=1991,
eigen_solver="lobpcg").fit_predict(X)
And I got following error:
numpy.linalg.linalg.LinAlgError: the leading minor of order 15 of 'b' is not positive definite.
The factorization of 'b' could not be completed and no eigenvalues or eigenvectors were computed.
I follow #2924 to change liblapack to atlas implementation, it doesn't work.
The matrix X is attached as aff.pkl in following zip file for whom attempt to reproduce this error:
or one can directly copy this verbose version:
import numpy as np
X=np.array([[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, 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, 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, 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, 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, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 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, 1, 1,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1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 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, 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, 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, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1]])
from sklearn.cluster import SpectralClustering
labels = SpectralClustering(n_clusters=5,
affinity="precomputed",
assign_labels="kmeans",
random_state=1991,
eigen_solver="lobpcg").fit_predict(X)
System info:

Activity
giorgiop commentedon Mar 5, 2016
What is the reason why you set
lobpcgas solver?arpackgives you a solution:By a quick look, your matrix is very low rank:
The graph is almost fully connected, with only a few edges missing. I did not investigate if this is the issue with
lobpcg. You may take a look a the scipy doc.cinvro commentedon Mar 5, 2016
I run into same problem when using arpack, so I swiched to lobpcg.
It seems at some point arpack calls lobpcg, I guess switching back to arpack cannot completely address this problem, maybe?
Here is another example I have in my experiments which uses arpack, it produces the same error:
error messages:
data_matrix
This example trys to apply spectral clustering on a 80 X 100 matrix directly without precomputed affinity.
And strangely, I cannot reproduce this error using my laptop which runs OS X instead of Ubunutu on my server. Maybe it is related to the version of liblobpcg?
@giorgiop Shouldn't spectral clustering module still work on low rank matrix?
lobpcg commentedon Aug 30, 2018
That may be a bug in LOBPCG code. I'll try to investigate.
lobpcg commentedon Aug 31, 2018
The bug is aparently in the spectral_embedding function - when calling lobpcg in all places, it ignores the value of eigen_tol and instead uses fixed predefined value tol=1e-15 . In the example above, this tolerance cannot be obtained by lobpcg, so rather then stopping after 2 iterations, as it should have, lobpcg keeps running and starts soon producing nonsense at the level of the round-off errors, and quickly, at iteration 4 or 5, crashes. The immediate simple solution is
(1) to make sure that the eigen_tol value in SpectralClustering input actually propagates through all intermediary functions to lobpcg and being in fact used in the lobpcg function
(2) change the default values from eigen_tol=0, e.g., in spectral.py, and advise the user to input "reasonable" tolerance, or none. The default tolerance set in lobpcg is n*sqrt(eps) = 1e-6 for the given example
massich commentedon Sep 1, 2018
I can replicate the error, but I don't think that there's any problem with it but rather an ill-post example.
lobpcg commentedon Sep 1, 2018
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. In the example above the max residual norms drops below 1e-10 but then starts growing and soon crashes. The fact that the residual norms cannot get smaller than 1e-10 is, indeed, an indication of somewhat ill-posed example. However, this is still not a good enough excuse for the code to crash. And it would not crash and give good results just by changing the tolerance to 1e-10 or larger. Here is a sample output with the built-in tol = 1e-15 and setting verbosityLevel = 1
Solving generalized eigenvalue problem with preconditioning
matrix size 50
block size 6
No constraints
iteration 0
current block size: 5
eigenvalue: [4.10052890e-16 1.01537911e+00 1.01636536e+00 1.01904567e+00
1.02052286e+00 1.02154829e+00]
residual norms: [2.13133519e-16 1.41923167e-02 2.27834607e-02 1.25036013e-02
2.16503765e-03 1.46368966e-02]
iteration 1
current block size: 5
eigenvalue: [4.10052890e-16 9.76745287e-01 9.76803141e-01 9.77542806e-01
1.02040816e+00 1.02040816e+00]
residual norms: [2.25673583e-16 2.26269818e-04 1.65288398e-03 6.04664762e-03
2.03723156e-14 1.11776728e-14]
iteration 2
current block size: 5
eigenvalue: [4.10052890e-16 9.76744186e-01 9.76744186e-01 9.76744186e-01
1.02040816e+00 1.02040816e+00]
residual norms: [5.19212918e-15 1.83085796e-13 2.13367515e-14 6.43454989e-14
3.28480178e-11 3.32759204e-13]
iteration 3
current block size: 5
eigenvalue: [4.10052889e-16 9.76744186e-01 9.76744186e-01 9.76744186e-01
1.02040810e+00 1.02040816e+00]
residual norms: [5.98837770e-13 2.28776891e-11 1.98930887e-14 7.69244428e-12
1.72832858e-07 2.38152764e-11]
iteration 4
current block size: 5
eigenvalue: [4.10049115e-16 9.76744186e-01 9.76744186e-01 9.76744186e-01
1.01760814e+00 1.02040700e+00]
residual norms: [6.20183840e-11 1.93258324e-09 3.30695986e-10 5.05859877e-10
3.21429578e-03 6.46491870e-06]
Traceback (most recent call last):
File "<pyshell#25>", line 5, in
eigen_solver="lobpcg").fit_predict(X)
File "C:\Python27\lib\site-packages\sklearn\base.py", line 410, in fit_predict
self.fit(X)
File "C:\Python27\lib\site-packages\sklearn\cluster\spectral.py", line 472, in fit
assign_labels=self.assign_labels)
File "C:\Python27\lib\site-packages\sklearn\cluster\spectral.py", line 262, in spectral_clustering
eigen_tol=eigen_tol, drop_first=False)
File "C:\Python27\lib\site-packages\sklearn\manifold\spectral_embedding_.py", line 317, in spectral_embedding
largest=False, maxiter=2000)
File "C:\Python27\lib\site-packages\scipy\sparse\linalg\eigen\lobpcg\lobpcg.py", line 452, in lobpcg
activeBlockVectorBP, retInvR=True)
File "C:\Python27\lib\site-packages\scipy\sparse\linalg\eigen\lobpcg\lobpcg.py", line 96, in _b_orthonormalize
gramVBV = cholesky(gramVBV)
File "C:\Python27\lib\site-packages\scipy\linalg\decomp_cholesky.py", line 91, in cholesky
check_finite=check_finite)
File "C:\Python27\lib\site-packages\scipy\linalg\decomp_cholesky.py", line 40, in _cholesky
"definite" % info)
LinAlgError: 5-th leading minor of the array is not positive definite
[DEBUG ON]
12 remaining items
dfilan commentedon Feb 26, 2019
Have run the tests with the normal version of scipy and the fixes to scikit-learn in #11968. Linked is the 3330-line output with lobpcg, and below is the output with amg:
lobpcg commentedon Feb 26, 2019
Thanks for your testing! Your LOBPCG trouble is evident from your output - excessive tolerance. #11968 fix is incomplete. As I write in #11968 (comment) the tolerance 1e-15 used there is way too small for the default eigen_tol. E.g., the default eigen_tol in the https://github.com/scipy/scipy/blob/v1.1.0/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py line 307-308 is np.sqrt(1e-15) * n where n is the size of the matrix. Just force eigen_tol = 1e-5 and LOBPCG should run fine in your case, I predict. You can try even more aggressive values, checking how it affects your clustering.
The AMG trouble is unclear from the output. Could you please try the latest LOBPCG from #12319 or scipy/scipy#9650 - they are identical. The latter is added to the 1.3.0 milestone Due by May 15, 2019. I hope that your AMG call runs into a bug already fixed in LOBPCG...
dfilan commentedon Feb 26, 2019
lobpcg commentedon Mar 1, 2019
Yes, I have actually meant setting residualTolerance. Thanks for figuring this out!
Your AMG bug is unrelated to residualTolerance. Could you please try the latest LOBPCG from #12319 or scipy/scipy#9650 - they are identical. The latter is added to the 1.3.0 milestone Due by May 15, 2019. I hope that your AMG call runs into a bug already fixed in LOBPCG... If not, it would require a separate investigation.
May be your sym_matrix just cannot be directly used as affinity = 'precomputed' with AMG. It looks like you put negative diagonal values in your sym_matrix, and AMG may be unhappy about it, although it is probably undocumented if true.
import sklearn.cluster
import scipy.sparse as sparse
import random
random.seed(1337)
rand_int = random.randrange(1000)
num_nodes = 1000
X = sparse.rand(num_nodes, num_nodes, density = 0.1, random_state = rand_int)
upper = sparse.triu(X) - sparse.diags(X.diagonal())
sym_matrix = upper + upper.T
clustering = sklearn.cluster.SpectralClustering(n_clusters = 3,
eigen_solver = 'amg',
random_state = rand_int,
affinity = 'precomputed')
labels = clustering.fit(sym_matrix)
dfilan commentedon Mar 1, 2019
My previous comment does use the latest LOBPCG in the 6th bullet point. Regarding the sym_matrix, I had assumed that the code would ensure that its diagonal was zeroed out, which is what it looks like is happening when I manually run that segment on a 10x10 sparse matrix.
dfilan commentedon Mar 1, 2019
Also, adding
print(sym_matrix.diagonal())before the call to the spectral clustering algorithm shows an array of all zeroes.lobpcg commentedon Mar 2, 2019
@dfilan My local install seems broken, so I cannot run with debugging myself and can only advise you.
Please make sure that you do run the latest LOBPCG from [MRG] add lobpcg svd_solver to PCA and TruncatedSVD #12319 or ENH: many lobpcg() algorithm improvements scipy/scipy#9650 - they are identical.
Without AMG, lobpcg runs fine here with reasonable residualTolerance. So, this seems to be an issue with AMG preconditioning, in lines 284-303 of sklearn\manifold\spectral_embedding_.py - I am looking at a recent developer master sklearn version. Please check that your run goes there, e.g., by adding a print before the lobpcg call at line 297.
Line 293 sets the AMG preconditioning from the laplacian
Change it to
and add a line before that, creating the new matrix laplacian4AMG from the present laplacian by adding a small, e.g., 1e-6, scalar value to every diagonal entry of laplacian. Let me know the outcome if you try this possible fix.
Let me copy @rc - may be he can help.
dfilan commentedon Mar 4, 2019
fixes the problem in this run, even without fiddling with the residualTolerance in lobpcg.py. It looks like the small scalar value can be as small as 1e-14 without causing problems in this case. In other code where I generate a sparse clustered and try to recover the clustering, the desired clustering is recovered.
lobpcg commentedon Mar 5, 2019
Perfect! @dfilan you are welcome.
lobpcg commentedon Dec 4, 2019
To my recollection, this is already fixed and can be closed