FIX Make eignvectors consistent#1
Merged
Micky774 merged 7 commits intoMicky774:change_svdfrom Mar 13, 2022
Merged
Conversation
thomasjpfan
commented
Mar 9, 2022
|
|
||
| @pytest.mark.parametrize("add_noise", [True, False]) | ||
| @pytest.mark.parametrize("seed", range(2)) | ||
| def test_fastica_simple_different_solvers(add_noise, seed): |
Author
There was a problem hiding this comment.
This is a new test to check for consistency based on what you had.
thomasjpfan
commented
Mar 9, 2022
| outs[solver] = sources | ||
| assert ica.components_.shape == (2, 2) | ||
| assert sources.shape == (1000, 2) | ||
| _, _, sources_fun = fastica(m.T, fun=nl, algorithm=algo, random_state=seed) |
Author
There was a problem hiding this comment.
This reverts the tests to what they were on upstream/main.
thomasjpfan
commented
Mar 9, 2022
| d, u = d[sort_indices], u[sort_indices] | ||
| # Resize and reorder to match svd | ||
| u = u[::-1, : min(X.shape) : -1] | ||
| d, u = d[sort_indices], u[:, sort_indices] |
Author
There was a problem hiding this comment.
Based on eigh docs, the eigenvectors has shape (M, N) where N is the number of eigenvalues. Thus, I think the sorting should be done over axis=1.
Author
|
Nevermind I think this works. |
Author
|
Here is a code snippet to demonstrate: from scipy import linalg
import numpy as np
from numpy.random import default_rng
from numpy.testing import assert_allclose
def my_svd(X):
u, d = linalg.svd(X, full_matrices=False)[:2]
u *= np.sign(u[0])
return u, d
def my_eigh(X):
d, u = linalg.eigh(X.T.dot(X))
sort_indicies = np.argsort(d)[::-1]
d = d[sort_indicies]
u = u[:, sort_indicies]
u *= np.sign(u[0])
return u, np.sqrt(d)
rng = default_rng()
for i in range(10):
m, n = 9, 6
X = rng.standard_normal((m, n))
u_svd, d_svd = my_svd(X.T)
u_eigen, d_eigen = my_eigh(X)
assert_allclose(d_svd, d_eigen)
assert_allclose(u_svd, u_eigen) |
Author
|
Although, we can use the align the eigenvectors based on the "maximum": from scipy import linalg
import numpy as np
from numpy.random import default_rng
from numpy.testing import assert_allclose
def my_svd(X):
u, d = linalg.svd(X, full_matrices=False)[:2]
max_abs_cols = np.argmax(np.abs(u), axis=0)
signs = np.sign(u[max_abs_cols, range(u.shape[1])])
u *= signs
return u, d
def my_eigh(X):
d, u = linalg.eigh(X.T.dot(X))
sort_indicies = np.argsort(d)[::-1]
d = d[sort_indicies]
u = u[:, sort_indicies]
max_abs_cols = np.argmax(np.abs(u), axis=0)
signs = np.sign(u[max_abs_cols, range(u.shape[1])])
u *= signs
return u, np.sqrt(d)
rng = default_rng()
for i in range(10):
m, n = 9, 6
X = rng.standard_normal((m, n))
u_svd, d_svd = my_svd(X.T)
u_eigen, d_eigen = my_eigh(X)
assert_allclose(d_svd, d_eigen)
assert_allclose(u_svd, u_eigen) |
thomasjpfan
commented
Mar 9, 2022
| u, d = linalg.svd(XT, full_matrices=False, check_finite=False)[:2] | ||
|
|
||
| # Give consistent eigenvectors for both svd solvers | ||
| u *= np.sign(u[0]) |
Author
There was a problem hiding this comment.
The alternative solution is:
max_abs_cols = np.argmax(np.abs(u), axis=0)
signs = np.sign(u[max_abs_cols, range(u.shape[1])])
u *= signsIf we want to be strictly the same as svd_flip.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Here is a quick fix to make the eigenvectors consistent.