jacobgil / tensorly

TensorLy: Tensor Learning in Python.

Home Page:http://tensorly.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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, MXNet or PyTorch 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

.. code:

pip install -U tensorly

.. code:

conda install -c tensorly tensorly

**Development

(from git)**

.. code:

# clone the repository
git clone https://github.com/te
cd tensorly
# Install in editable mode with
pip install -e .

nsorly/tensorly

-e or, equivalently, --editable

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, checkout the documentation.


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 (though you can also use nose). 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

Quickstart

import tensorly as tl
import numpy as np

Create a small third order tensor of size 3 x 4 x 2 and perform simple operations on it:

tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)))
tl.unfolded = unfold(tensor, mode=0)
tl.fold(unfolded, mode=0, shape=tensor.shape)

Applying tensor decomposition is easy:

from tensorly.decomposition import tucker
# Apply Tucker decomposition 
core, factors = tucker(tensor, rank=[2, 2, 2])
# Reconstruct the full tensor from the decomposed form
tl.tucker_to_tensor(core, factors) 

Changing the backend to perform computation on GPU for instance. Note that using MXNet or PyTorch requires to have installed them first:

tl.set_backend('pytorch') # Or 'mxnet' or 'numpy'

Now all the computation is done by PyTorch, and tensors can be created on GPU:

import torch
tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=torch.cuda.FloatTensor)
type(tensor) # torch.cuda.FloatTensor

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!


About

TensorLy: Tensor Learning in Python.

http://tensorly.org

License:Other


Languages

Language:Python 99.8%Language:Makefile 0.2%