RepositoryPython
tensorly

tensorly/tensorly

TensorLy: Tensor Learning in Python.

1.7kstars
318forks
50issues
2views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: tensorly

#1 Repository of the week

1
TINIX TRENDING·Topic: decomposition

#1 Repository of the week

1
TINIX TRENDING·Topic: tensor-algebra

#1 Repository of the week

.. image:: https://badge.fury.io/py/tensorly.svg :target: https://badge.fury.io/py/tensorly

.. image:: https://anaconda.org/tensorly/tensorly/badges/version.svg
:target: https://anaconda.org/tensorly/tensorly

.. image:: https://github.com/tensorly/tensorly/actions/workflows/test.yml/badge.svg?branch=main :target: https://github.com/tensorly/tensorly/actions/workflows/test.yml

.. image:: https://codecov.io/gh/tensorly/tensorly/branch/master/graph/badge.svg?token=mnZ234sGSA :target: https://codecov.io/gh/tensorly/tensorly

.. image:: https://img.shields.io/badge/Slack-join-brightgreen :target: https://join.slack.com/t/tensorly/shared_invite/zt-wqnts2sk-wbiRX6ml~Xt6~GDYWRPFfg

======== 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 <https://www.anaconda.com/download/>_.

+-------------------------------------------+---------------------------------------------------+ |     With pip (recommended) |         With conda | +-------------------------------------------+---------------------------------------------------+ | | | | .. code:: | .. code:: | | | | | pip install -U tensorly | conda install -c tensorly tensorly | | | | | | | +-------------------------------------------+---------------------------------------------------+ | Development (from git) | +-------------------------------------------+---------------------------------------------------+ | | | .. code:: | | | | # 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 <http://tensorly.org/dev/installation.html>_.


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:

.. code:: python

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:

.. code:: python

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 <http://tensorly.org/stable/modules/api.html#module-tensorly.random>_.

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:

.. code:: python

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:

.. code:: python

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 <http://tensorly.org/stable/modules/api.html#module-tensorly.decomposition>_ 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 <http://tensorly.org/dev/user_guide/index.html>_ and for a detailed reference of the functions and their documentation, refer to the API <http://tensorly.org/dev/modules/api.html>_

If you see a bug, open an issue <https://github.com/tensorly/tensorly/issues>, or better yet, a pull-request <https://github.com/tensorly/tensorly/pulls>!


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 <https://github.com/tensorly/tensorly/>_.

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:

.. code:: bash

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:

.. code::

pytest -v tensorly

Alternatively, you can specify for which backend you wish to run the tests:

.. code::

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}
}

.. [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.

Similar Projects

Discover other similar projects and AI models.

RepositoryPython
pytorch/pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration

105.8k
Dec 1, 2026
RepositoryJupyter Notebook
rasbt/LLMs-from-scratch

Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

109.5k
Dec 2, 2026
RepositoryC++
tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone

201.9k
Dec 1, 2026
RepositoryPython
huggingface/transformers

🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.

169.5k
Dec 1, 2026
RepositoryPython
Comfy-Org/ComfyUI

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

149.8k
Dec 3, 2026
RepositoryPython
d2l-ai/d2l-zh

《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。

82.9k
Jul 30, 2024
RepositoryPython
openbq-org/OpenBB

Open Data Platform for analysts, quants and AI agents.

76.0k
Nov 29, 2026
RepositoryJupyter Notebook
microsoft/ML-For-Beginners

12 weeks, 26 lessons, 52 quizzes, classic Machine Learning for all

92.6k
Dec 2, 2026
Modelsentence-similarity
sentence-transformers/all-MiniLM-L6-v2

all-MiniLM-L6-v2 This is a sentence-transformers(https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search. Usage (Sentence-Transformers) Using t...

♥0
156.6M
Jun 1, 2026