tensorly / tensorly

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

Normalizing factors in non-negative CP decomposition #562

Closed akonstodata closed 1 month ago

akonstodata commented 1 month ago

Summary

I would like to perform a non-negative CP tensor decomposition where I set the normalize_factors parameter to True, but am unable to do so as I receive errors in both the non_negative_parafac and non_negative_parafac_hals functions.

Steps or Code to Reproduce

An example following the tutorial for non-negative CP decomposition.

import numpy as np
import tensorly as tl
from tensorly.decomposition import non_negative_parafac, non_negative_parafac_hals

# Create non-negative tensor
tensor = tl.tensor(np.arange(24000).reshape((30, 40, 20)), dtype=tl.float32)

# Run non_negative_parafac with 10 factors, and normalize_factors set to `True`
tensor_mu, errors_mu = non_negative_parafac(tensor, rank=10, return_errors=True, normalize_factors=True)

# Run non_negative_parafac_hals with 1- factors, and normalize_factors set to `True`
tensor_mu, errors_mu = non_negative_parafac_hals(tensor, rank=10, return_errors=True, normalize_factors=True)

Error output

The call to non_negative_parafac returns the following error:

/Users/akonsto/opt/anaconda3/lib/python3.8/site-packages/numpy/core/fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
/Users/akonsto/opt/anaconda3/lib/python3.8/site-packages/tensorly/cp_tensor.py:689: RuntimeWarning: invalid value encountered in multiply
  norm = norm * (T.reshape(weights, (-1, 1))*T.reshape(weights, (1, -1)))
/Users/akonsto/opt/anaconda3/lib/python3.8/site-packages/tensorly/cp_tensor.py:293: RuntimeWarning: invalid value encountered in multiply
  factor = factor*weights
/Users/akonsto/opt/anaconda3/lib/python3.8/site-packages/tensorly/backend/core.py:635: RuntimeWarning: overflow encountered in square
  return self.sqrt(self.sum(self.abs(tensor)**2, axis=axis))

The call to non_negative_parafac_hals returns the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-184bc3e7b92e> in <module>
----> 1 tensor_mu, errors_mu = non_negative_parafac_hals(tensor, rank=10, return_errors=True, normalize_factors=True)

TypeError: non_negative_parafac_hals() got an unexpected keyword argument 'normalize_factors'

Versions

macOS-10.16-x86_64-i386-64bit
Python 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ]
NumPy 1.23.4
SciPy 1.5.2
TensorLy 0.7.0
JeanKossaifi commented 1 month ago

For the first one, looks like it is an overflow error, you may want to check the range of values of your tensor and perhaps normalize its range before decomposing.

In the second case, it should be supported, see the doc. What version of TensorLy are you using (you can check with import tensorly; print(tensorly.__version__) ) ?

akonstodata commented 1 month ago

@JeanKossaifi, thanks for your reply. I had the 0.7.0 version of Tensorly. I installed the newest version (0.8.1) and it seems that both issues have been cleared up. For the first one, I was not able to get the normalize_factors to be True without the overflow error for multiple different datasets in the older version. But it works on all the datasets I tested in the updated version. Thanks!

cohenjer commented 1 month ago

Closing since the problem is fixed in tl>=0.8.0