Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when running sparse Robust PCA #499

Open
dawe opened this issue Apr 4, 2023 · 5 comments
Open

Error when running sparse Robust PCA #499

dawe opened this issue Apr 4, 2023 · 5 comments
Labels

Comments

@dawe
Copy link

dawe commented Apr 4, 2023

Describe the bug

Computation of sparse robust pca crashes as it calls svd with unexpected keyword (full_matrices)

Steps or Code to Reproduce

Mostly copied from tensorly docs

import tensorly.contrib.sparse as stl; import sparse
shape = (1000, 1001, 1002)
rank = 5
starting_weights = stl.ones((rank))
starting_factors = [sparse.random((i, rank)) for i in shape]
from tensorly.contrib.sparse.cp_tensor import cp_to_tensor
tensor = cp_to_tensor((starting_weights, starting_factors))
from tensorly.decomposition import robust_pca
from tensorly.contrib.sparse.decomposition import robust_pca as  sparse_robust_pca
dense_tensor = tensor.todense()
d_RPCA = robust_pca(dense_tensor)
s_RPCA = sparse_robust_pca(tensor)

Expected behavior

Should compute s_RPCA (equal to d_RPCA)

Actual result

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cittaro.davide/miniforge3/envs/sep22/lib/python3.9/site-packages/tensorly/contrib/sparse/core.py", line 12, in inner
    return func(*args, **kwargs)
  File "/home/cittaro.davide/miniforge3/envs/sep22/lib/python3.9/site-packages/tensorly/decomposition/robust_decomposition.py", line 112, in robust_pca
    svd_thresholding(unfold(D, i) + unfold(L[i], i) / mu, reg_J / mu),
  File "/home/cittaro.davide/miniforge3/envs/sep22/lib/python3.9/site-packages/tensorly/tenalg/proximal.py", line 845, in svd_thresholding
    U, s, V = tl.truncated_svd(matrix, n_eigenvecs=min(matrix.shape))
  File "/home/cittaro.davide/miniforge3/envs/sep22/lib/python3.9/site-packages/tensorly/tenalg/svd.py", line 226, in truncated_svd
    U, S, V = tl.svd(matrix, full_matrices=full_matrices)
  File "/home/cittaro.davide/miniforge3/envs/sep22/lib/python3.9/site-packages/tensorly/backend/__init__.py", line 206, in wrapped_backend_method
    return getattr(
TypeError: svd() got an unexpected keyword argument 'full_matrices'

Versions

>>> import platform; print(platform.platform())
Linux-3.10.0-1127.el7.x86_64-x86_64-with-glibc2.17
>>> import sys; print("Python", sys.version)
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21) 
[GCC 10.3.0]
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.23.5
>>> import scipy; print("SciPy", scipy.__version__)
SciPy 1.9.1
>>> import tensorly; print("TensorLy", tensorly.__version__)
TensorLy 0.8.1
@aarmey
Copy link
Contributor

aarmey commented May 17, 2023

Thanks for reporting this @dawe.

@JeanKossaifi, because this skip line is not indented within the if statement above, the sparse tests are always skipped. I assume this should be moved within the if statement?

@aarmey
Copy link
Contributor

aarmey commented May 17, 2023

Some information I found:

  • This is not actually an error with the full_matrices option. The actual error when you remove dynamic dispatch is that no svd method exists for COO matrices.
  • The error appears this way because we need to add the full_matrices option to the stub in backend/core.py to match the real function signatures.
  • One broader issue here is that svd_thresholding() needs to avoid calling tl.truncated_svd() in the first place, because it is not setup for sparse matrices.
  • If this will be an option within svd_thresholding(), then we would need to expose it in robust_pca(). Adding an option for the SVD method is probably best so that the user can adjust this for performance, too.
  • There still exists the issue with testing, I believe.

@JeanKossaifi
Copy link
Member

Thanks for opening @dawe and thanks for digging into it @aarmey!

Regarding the testing, it should only be skipped if sparse is not installed - that should never be an issue for us (we can double check we indeed install pydata-sparse) but avoids test failures if users run the test suite locally without it installed.

pytest.importorskip only skips if import fails:

Import and return the requested module modname, or skip the current test if the module cannot be imported.

We haven't updated the sparse backend for a while and indeed quite likely the interfaces no longer match and should be updated.

@JeanKossaifi
Copy link
Member

@dawe would you be interested in opening a small PR to fix the issue?

@ghbrown
Copy link

ghbrown commented Mar 29, 2024

The same error seems to plague tensorly.contrib.sparse.decomposition.parafac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants