Skip to main content

Tensor learning in Python.

Project description

https://badge.fury.io/py/tensorly.svg https://anaconda.org/tensorly/tensorly/badges/version.svg https://github.com/tensorly/tensorly/actions/workflows/test.yml/badge.svg?branch=main https://codecov.io/gh/tensorly/tensorly/branch/master/graph/badge.svg?token=mnZ234sGSA https://img.shields.io/badge/Slack-join-brightgreen

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.


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

pip install -U tensorly
conda install -c tensorly tensorly

Development (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 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorly-0.9.0.tar.gz (7.3 MB view details)

Uploaded Source

Built Distribution

tensorly-0.9.0-py3-none-any.whl (7.4 MB view details)

Uploaded Python 3

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

Hashes for tensorly-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9b970dde47e2267a17b66a1444113e879c0bb77fb22f497a4207eb2b548ff792
MD5 5f1741df3a0f8594343d905be0bcc71e
BLAKE2b-256 573976a859505dc5331cf43baba1fc011e85184c8ad0dbe01e6b01fe0f935ea2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensorly-0.9.0.tar.gz:

Publisher: deploy_pypi.yml on tensorly/tensorly

Attestations:

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

Hashes for tensorly-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a393022ff1616a36c021496f46a955d94234d6bc8c42793f477a6be8849f4873
MD5 cbb3c838b3ae012f31bce2be8c980cc8
BLAKE2b-256 c3ad83380fb3c85ddf68c509059cf328374c1878f87382070475abc72867c7ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensorly-0.9.0-py3-none-any.whl:

Publisher: deploy_pypi.yml on tensorly/tensorly

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page