tensorly / tensorly

TensorLy: Tensor Learning in Python.
http://tensorly.org
Other
1.54k stars 290 forks source link

ValueError: array must not contain infs or NaNs in tucker decomposition #129

Closed rafikg closed 2 years ago

rafikg commented 5 years ago

Describe the bug

ValueError: array must not contain infs or NaNs

Steps or Code to Reproduce

For instance:

import tensorly as tl
import numpy as np

tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float32)
unfolded = tl.unfold(tensor, mode=0)
tl.fold(unfolded, mode=0, shape=tensor.shape)

from tensorly.decomposition import tucker
# Apply Tucker decomposition
tucker_tensor = tucker(tensor, rank=[2, 3, 2])
# Reconstruct the full tensor from the decomposed form
tl.tucker_to_tensor(tucker_tensor)

Expected behavior

Actual result

ValueError Traceback (most recent call last)

in 1 from tensorly.decomposition import tucker 2 # Apply Tucker decomposition ----> 3 tucker_tensor = tucker(tensor, rank=[2, 3, 2]) 4 # Reconstruct the full tensor from the decomposed form 5 tl.tucker_to_tensor(tucker_tensor) ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/tensorly/decomposition/_tucker.py in tucker(tensor, rank, ranks, n_iter_max, init, svd, tol, random_state, verbose) 147 modes = list(range(tl.ndim(tensor))) 148 return partial_tucker(tensor, modes, rank=rank, ranks=ranks, n_iter_max=n_iter_max, init=init, --> 149 svd=svd, tol=tol, random_state=random_state, verbose=verbose) 150 151 ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/tensorly/decomposition/_tucker.py in partial_tucker(tensor, modes, rank, n_iter_max, init, tol, svd, random_state, verbose, ranks) 85 for index, mode in enumerate(modes): 86 core_approximation = multi_mode_dot(tensor, factors, modes=modes, skip=index, transpose=True) ---> 87 eigenvecs, _, _ = svd_fun(unfold(core_approximation, mode), n_eigenvecs=rank[index]) 88 factors[index] = eigenvecs 89 ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/tensorly/backend/numpy_backend.py in partial_svd(matrix, n_eigenvecs) 230 231 # Default on standard SVD --> 232 U, S, V = scipy.linalg.svd(matrix, full_matrices=full_matrices) 233 U, S, V = U[:, :n_eigenvecs], S[:n_eigenvecs], V[:n_eigenvecs, :] 234 return U, S, V ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/scipy/linalg/decomp_svd.py in svd(a, full_matrices, compute_uv, overwrite_a, check_finite, lapack_driver) 107 108 """ --> 109 a1 = _asarray_validated(a, check_finite=check_finite) 110 if len(a1.shape) != 2: 111 raise ValueError('expected matrix') ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/scipy/_lib/_util.py in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact) 237 raise ValueError('masked arrays are not supported') 238 toarray = np.asarray_chkfinite if check_finite else np.asarray --> 239 a = toarray(a) 240 if not objects_ok: 241 if a.dtype is np.dtype('O'): ~/anaconda3/envs/kerasws/lib/python3.7/site-packages/numpy/lib/function_base.py in asarray_chkfinite(a, dtype, order) 459 if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all(): 460 raise ValueError( --> 461 "array must not contain infs or NaNs") 462 return a 463 ValueError: array must not contain infs or NaNs #### Versions Please run the following snippet and paste the output below. Linux-4.15.0-58-generic-x86_64-with-debian-buster-sid Python 3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0] NumPy 1.15.4 SciPy 1.2.1 Using numpy backend. TensorLy 0.4.3
JeanKossaifi commented 5 years ago

Thanks for reporting, seems the last commit fixed one issue and created another, I'll try to see if this is fixable.

In this case, the issue is that the above code does an overcomplete decomposition.. A rank of 2 for all modes is sufficient and returns the expected result: tucker_tensor = tucker(tensor, rank=[2, 2, 2])..

aarmey commented 2 years ago

I've checked and this no longer occurs on any of the backends.