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/workflows/Test%20TensorLy/badge.svg 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, MXNet, TensorFlow or CuPy, 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 the MXNet or PyTorch 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, 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.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.8.1.tar.gz (171.3 kB view details)

Uploaded Source

Built Distribution

tensorly-0.8.1-py3-none-any.whl (229.7 kB view details)

Uploaded Python 3

File details

Details for the file tensorly-0.8.1.tar.gz.

File metadata

  • Download URL: tensorly-0.8.1.tar.gz
  • Upload date:
  • Size: 171.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tensorly-0.8.1.tar.gz
Algorithm Hash digest
SHA256 cf78e4ffe612feca3510214002845c6831b267b1f2c1181154d41430310b237d
MD5 09148465f67f7bcbdfabf221fe3cf769
BLAKE2b-256 757f813ac904fc85733a93a9703aea5fe320f0573855cf0eb092531c6b0a8a88

See more details on using hashes here.

File details

Details for the file tensorly-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: tensorly-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 229.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tensorly-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 08988dbc5e433c3f255d0e00855f99a613fe273d50a1627b7e82b03ff2a6da9a
MD5 2a5799f9c27addb4dfe099385c2a912b
BLAKE2b-256 716cb07811af60b429d29ff1aab7a8d7b845f24e27462c7455c3df734007dd67

See more details on using hashes here.

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