Package: tensorly
TensorLyTensorLy is a Python library that aims at making tensor learning simple and accessible. It allows to easily perform tensor decomposition, tensor learning and tensor algebra. Its backend system allows to seamlessly perform computation with NumPy, PyTorch, JAX, MXNet, TensorFlow or CuPy, and run methods at scale on CPU or GPU.Website:https://tensorly.orgSource-code:https://github.com/tensorly/tensorlyJupyter Notebooks:https://github.com/JeanKossaifi/tensorly-notebooksInstalling TensorLyThe only pre-requisite is to havePython 3installed. The easiest way is via theAnaconda distribution.With pip(recommended)With condapip install -U tensorlyconda install -c tensorly tensorlyDevelopment (from git)# clone the repository
git clone https://github.com/tensorly/tensorly
cd tensorly
# Install in editable mode with `-e` or, equivalently, `--editable`
pip install -e .Note:TensorLy depends on NumPy by default. If you want to use the MXNet or PyTorch backends, you will need to install these packages separately.For detailed instruction, please see thedocumentation.QuickstartCreating tensorsCreate a small third order tensor of size 3 x 4 x 2, from a NumPy array and perform simple operations on it:importtensorlyastlimportnumpyasnptensor=tl.tensor(np.arange(24).reshape((3,4,2)),dtype=tl.float64)unfolded=tl.unfold(tensor,mode=0)tl.fold(unfolded,mode=0,shape=tensor.shape)You can also create random tensors:fromtensorlyimportrandom# A random tensortensor=random.random_tensor((3,4,2))# A random CP tensor in factorized formcp_tensor=random.random_tensor(shape=(3,4,2),rank='same')You can also create tensors in TT-format, Tucker, etc, seerandom tensors.Setting the backendYou can change the backend to perform computation with a different framework. By default, the backend is NumPy, but you can also perform the computation using  PyTorch, TensorFlow, MXNet, JAX or CuPy (requires to have installed them first). For instance, after setting the backend to PyTorch, all the computation is done by PyTorch, and tensors can be created on GPU:tl.set_backend('pytorch')# Or 'mxnet', 'numpy', 'tensorflow', 'cupy' or 'jax'tensor=tl.tensor(np.arange(24).reshape((3,4,2)),device='cuda:0')type(tensor)# torch.TensorTensor decompositionApplying tensor decomposition is easy:fromtensorly.decompositionimporttucker# Apply Tucker decompositiontucker_tensor=tucker(tensor,rank=[2,2,2])# Reconstruct the full tensor from the decomposed formtl.tucker_to_tensor(tucker_tensor)We havemany more decompositionsavailable, be sure to check them out!Next stepsThis is just a very quick introduction to some of the basic features of TensorLy.
For more information on getting started, checkout theuser-guideand for a detailed reference of the functions and their documentation, refer to
theAPIIf you see a bug, open anissue, or better yet, apull-request!Contributing codeAll contributions are welcome! So if you have a cool tensor method you want to add, if you spot a bug or even a typo or mistake in the documentation, please report it, and even better, open a Pull-Request onGitHub.Before you submit your changes, you should make sure your code adheres to our style-guide. The easiest way to do this is withblack:pipinstallblackblack.Running the testsTesting and documentation are an essential part of this package and all functions come with uni-tests and documentation.The tests are ran using thepytestpackage.
First installpytest:pip install pytestThen to run the test, simply run, in the terminal:pytest -v tensorlyAlternatively, you can specify for which backend you wish to run the tests:TENSORLY_BACKEND='numpy' pytest -v tensorlyCitingIf you use TensorLy in an academic paper, please cite[1]:@article{tensorly,
  author  = {Jean Kossaifi and Yannis Panagakis and Anima Anandkumar and Maja Pantic},
  title   = {TensorLy: Tensor Learning in Python},
  journal = {Journal of Machine Learning Research},
  year    = {2019},
  volume  = {20},
  number  = {26},
  pages   = {1-6},
  url     = {http://jmlr.org/papers/v20/18-277.html}
}[1]Jean Kossaifi, Yannis Panagakis, Anima Anandkumar and Maja Pantic,TensorLy: Tensor Learning in Python,Journal of Machine Learning Research (JMLR), 2019, volume 20, number 26.