From 463ba4ea01562685b6b180310b29d791193adf62 Mon Sep 17 00:00:00 2001 From: Andrew Knyazev Date: Wed, 29 Sep 2021 09:14:07 -0400 Subject: [PATCH 1/5] Update _spectral_embedding.py Fix the typo in tol from -15 to -5 --- sklearn/manifold/_spectral_embedding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/manifold/_spectral_embedding.py b/sklearn/manifold/_spectral_embedding.py index 623f00e98f612..addab2c4d24da 100644 --- a/sklearn/manifold/_spectral_embedding.py +++ b/sklearn/manifold/_spectral_embedding.py @@ -367,7 +367,7 @@ def spectral_embedding( X = random_state.rand(laplacian.shape[0], n_components + 1) X[:, 0] = dd.ravel() _, diffusion_map = lobpcg( - laplacian, X, tol=1e-15, largest=False, maxiter=2000 + laplacian, X, tol=1e-5, largest=False, maxiter=2000 ) embedding = diffusion_map.T[:n_components] if norm_laplacian: From f67964589d042eae072bae73c138dd8a7a9a195e Mon Sep 17 00:00:00 2001 From: Andrew Knyazev Date: Wed, 6 Oct 2021 11:23:44 -0400 Subject: [PATCH 2/5] Update v1.0.rst Added info on #21194 --- doc/whats_new/v1.0.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index d8776653cd9e8..4a55cc18d82e7 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -608,6 +608,10 @@ Changelog :mod:`sklearn.manifold` ....................... +- |Fix| Decrease the numerical default tolerance in the lobpcg call + in :func:`manifold.spectral_embedding` to prevent numerical instability. + :pr:`21194` by :user:`Andrew Knyazev `. + - |Enhancement| Implement `'auto'` heuristic for the `learning_rate` in :class:`manifold.TSNE`. It will become default in 1.2. The default initialization will change to `pca` in 1.2. PCA initialization will From 1dff0ea40fa1d013ce03fa06b184a001687609d6 Mon Sep 17 00:00:00 2001 From: Andrew Knyazev Date: Wed, 6 Oct 2021 13:16:17 -0400 Subject: [PATCH 3/5] Update test_spectral.py A unit test added --- sklearn/cluster/tests/test_spectral.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sklearn/cluster/tests/test_spectral.py b/sklearn/cluster/tests/test_spectral.py index 679adf27520e4..46bfca45d6266 100644 --- a/sklearn/cluster/tests/test_spectral.py +++ b/sklearn/cluster/tests/test_spectral.py @@ -318,3 +318,13 @@ def test_spectral_clustering_np_matrix_raises(): msg = r"spectral_clustering does not support passing in affinity as an np\.matrix" with pytest.raises(TypeError, match=msg): spectral_clustering(X) + + +def test_spectral_embedding_lobpcg_tol_decrease(): + """Check that spectral_embedding call of lobpcg does not fail due to toll + too small. Fixes #21147; see #21194""" + X = np.random.randn(100, 10) + A = X @ X.T + Q = np.random.randn(X.shape[0], 4) + eigenvalues, _ = lobpcg(A, Q, maxiter=20) + assert(np.max(eigenvalues) > 0) From 775fcd44dbb3f709424ab545f073023c4dd03979 Mon Sep 17 00:00:00 2001 From: Andrew Knyazev Date: Wed, 6 Oct 2021 13:19:16 -0400 Subject: [PATCH 4/5] Update v1.0.rst moved for the proper order --- doc/whats_new/v1.0.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index 4a55cc18d82e7..d0d4e4db00c78 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -608,10 +608,6 @@ Changelog :mod:`sklearn.manifold` ....................... -- |Fix| Decrease the numerical default tolerance in the lobpcg call - in :func:`manifold.spectral_embedding` to prevent numerical instability. - :pr:`21194` by :user:`Andrew Knyazev `. - - |Enhancement| Implement `'auto'` heuristic for the `learning_rate` in :class:`manifold.TSNE`. It will become default in 1.2. The default initialization will change to `pca` in 1.2. PCA initialization will @@ -628,6 +624,10 @@ Changelog every infinite distances to zero. :pr:`20531` by `Roman Yurchak`_ and `Tom Dupre la Tour`_. +- |Fix| Decrease the numerical default tolerance in the lobpcg call + in :func:`manifold.spectral_embedding` to prevent numerical instability. + :pr:`21194` by :user:`Andrew Knyazev `. + :mod:`sklearn.metrics` ...................... From 2e8932723f0ecc2e722cfe44928caa8ceea3a821 Mon Sep 17 00:00:00 2001 From: Andrew Knyazev Date: Wed, 6 Oct 2021 13:21:19 -0400 Subject: [PATCH 5/5] Update test_spectral.py wrong file edited! Back to the original --- sklearn/cluster/tests/test_spectral.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sklearn/cluster/tests/test_spectral.py b/sklearn/cluster/tests/test_spectral.py index 46bfca45d6266..679adf27520e4 100644 --- a/sklearn/cluster/tests/test_spectral.py +++ b/sklearn/cluster/tests/test_spectral.py @@ -318,13 +318,3 @@ def test_spectral_clustering_np_matrix_raises(): msg = r"spectral_clustering does not support passing in affinity as an np\.matrix" with pytest.raises(TypeError, match=msg): spectral_clustering(X) - - -def test_spectral_embedding_lobpcg_tol_decrease(): - """Check that spectral_embedding call of lobpcg does not fail due to toll - too small. Fixes #21147; see #21194""" - X = np.random.randn(100, 10) - A = X @ X.T - Q = np.random.randn(X.shape[0], 4) - eigenvalues, _ = lobpcg(A, Q, maxiter=20) - assert(np.max(eigenvalues) > 0)