1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 | .. raw:: html
<p align="center"><img width="35%" src="http://tensorly.org/stable/_static/TensorLy_logo.png" /></p>
<p align="center">
<a href="https://badge.fury.io/py/tensorly" target=blank>
<img src="https://badge.fury.io/py/tensorly.svg"
</a>
<a href="https://anaconda.org/tensorly/tensorly" target=blank>
<img src="https://anaconda.org/tensorly/tensorly/badges/version.svg"
</a>
<a href="https://travis-ci.org/tensorly/tensorly" target=blank>
<img src="https://travis-ci.org/tensorly/tensorly.svg?branch=master"
</a>
<a href="https://coveralls.io/github/tensorly/tensorly?branch=master" target=blank>
<img src="https://coveralls.io/repos/github/tensorly/tensorly/badge.svg?branch=master"
</a>
<a href="https://gitter.im/tensorly/tensorly?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge" target=blank>
<img src="https://badges.gitter.im/tensorly/tensorly.svg"
</a>
</p>
\
========
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, PyTorch, TensorFlow or CuPy, and run methods at scale on CPU or GPU.
- **Website:** http://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 <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 the MXNet or PyTorch backends, you will need to install these packages separately.
For detailed instruction, please see the `documentation <http://tensorly.org/dev/installation.html>`_.
--------------------------
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:
.. code::
pytest -v tensorly
Alternatively, you can specify for which backend you wish to run the tests:
.. code::
TENSORLY_BACKEND='numpy' pytest -v tensorly
------------------
Quickstart
==========
Create a small third order tensor of size 3 x 4 x 2 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)))
unfolded = tl.unfold(tensor, mode=0)
tl.fold(unfolded, mode=0, shape=tensor.shape)
Applying tensor decomposition is easy:
.. code:: python
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, PyTorch, TensorFlow 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:
.. code:: python
tl.set_backend('pytorch') # Or 'mxnet', 'numpy', 'tensorflow' or 'cupy'
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 <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>`_!
-------------
|