Tensor learning in Python.
Project description
TensorLy
TensorLy 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, TensorFlow, CuPy or Paddle, and run methods at scale on CPU or GPU.
Website: https://tensorly.org
Source-code: https://github.com/tensorly/tensorly
Jupyter Notebooks: https://github.com/JeanKossaifi/tensorly-notebooks
Installing TensorLy
The only pre-requisite is to have Python 3 installed. The easiest way is via the Anaconda distribution.
With pip (recommended) |
With conda |
|
|
Development (from git) |
|
|
Note: TensorLy depends on NumPy by default. If you want to use other backends, you will need to install these packages separately.
For detailed instruction, please see the documentation.
Quickstart
Creating tensors
Create a small third order tensor of size 3 x 4 x 2, from a NumPy array and perform simple operations on it:
import tensorly as tl
import numpy as np
tensor = 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:
from tensorly import random
# A random tensor
tensor = random.random_tensor((3, 4, 2))
# A random CP tensor in factorized form
cp_tensor = random.random_tensor(shape=(3, 4, 2), rank='same')
You can also create tensors in TT-format, Tucker, etc, see random tensors.
Setting the backend
You 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, JAX, CuPy or Paddle (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 'numpy', 'tensorflow', 'cupy' or 'jax'
tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), device='cuda:0')
type(tensor) # torch.Tensor
Tensor decomposition
Applying tensor decomposition is easy:
from tensorly.decomposition import tucker
# Apply Tucker decomposition
tucker_tensor = tucker(tensor, rank=[2, 2, 2])
# Reconstruct the full tensor from the decomposed form
tl.tucker_to_tensor(tucker_tensor)
We have many more decompositions available, be sure to check them out!
Next steps
This is just a very quick introduction to some of the basic features of TensorLy. For more information on getting started, checkout the user-guide and for a detailed reference of the functions and their documentation, refer to the API
If you see a bug, open an issue, or better yet, a pull-request!
Contributing code
All 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 on GitHub.
Before you submit your changes, you should make sure your code adheres to our style-guide. The easiest way to do this is with black:
pip install black
black .
Running the tests
Testing and documentation are an essential part of this package and all functions come with uni-tests and documentation.
The tests are ran using the pytest package. First install pytest:
pip install pytest
Then to run the test, simply run, in the terminal:
pytest -v tensorly
Alternatively, you can specify for which backend you wish to run the tests:
TENSORLY_BACKEND='numpy' pytest -v tensorly
Citing
If 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} }
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tensorly-0.9.0.tar.gz
.
File metadata
- Download URL: tensorly-0.9.0.tar.gz
- Upload date:
- Size: 7.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b970dde47e2267a17b66a1444113e879c0bb77fb22f497a4207eb2b548ff792 |
|
MD5 | 5f1741df3a0f8594343d905be0bcc71e |
|
BLAKE2b-256 | 573976a859505dc5331cf43baba1fc011e85184c8ad0dbe01e6b01fe0f935ea2 |
Provenance
The following attestation bundles were made for tensorly-0.9.0.tar.gz
:
Publisher:
deploy_pypi.yml
on tensorly/tensorly
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
tensorly-0.9.0.tar.gz
- Subject digest:
9b970dde47e2267a17b66a1444113e879c0bb77fb22f497a4207eb2b548ff792
- Sigstore transparency entry: 148400455
- Sigstore integration time:
- Predicate type:
File details
Details for the file tensorly-0.9.0-py3-none-any.whl
.
File metadata
- Download URL: tensorly-0.9.0-py3-none-any.whl
- Upload date:
- Size: 7.4 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a393022ff1616a36c021496f46a955d94234d6bc8c42793f477a6be8849f4873 |
|
MD5 | cbb3c838b3ae012f31bce2be8c980cc8 |
|
BLAKE2b-256 | c3ad83380fb3c85ddf68c509059cf328374c1878f87382070475abc72867c7ba |
Provenance
The following attestation bundles were made for tensorly-0.9.0-py3-none-any.whl
:
Publisher:
deploy_pypi.yml
on tensorly/tensorly
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
tensorly-0.9.0-py3-none-any.whl
- Subject digest:
a393022ff1616a36c021496f46a955d94234d6bc8c42793f477a6be8849f4873
- Sigstore transparency entry: 148400457
- Sigstore integration time:
- Predicate type: