tensorly


Nametensorly JSON
Version 0.8.0 PyPI version JSON
download
home_pagehttps://github.com/tensorly/tensorly
SummaryTensor learning in Python.
upload_time2023-01-14 21:17:45
maintainer
docs_urlNone
authorJean Kossaifi
requires_python
licenseModified BSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            .. 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/workflows/Test%20TensorLy/badge.svg
    :target: https://github.com/tensorly/tensorly/actions?query=workflow%3A%22Test+TensorLy%22

.. 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, MXNet, TensorFlow or CuPy, and run methods at scale on CPU or GPU.

- **Website:** https://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>`_.

------------------

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, 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:

.. code:: python

   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:

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



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tensorly/tensorly",
    "name": "tensorly",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jean Kossaifi",
    "author_email": "jean.kossaifi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6d/f8/0ca88eb07c54b669d9be863667adbc5b7fd01ca757f65d07b294b9e0e163/tensorly-0.8.0.tar.gz",
    "platform": null,
    "description": ".. image:: https://badge.fury.io/py/tensorly.svg\n    :target: https://badge.fury.io/py/tensorly\n\n.. image:: https://anaconda.org/tensorly/tensorly/badges/version.svg   \n    :target: https://anaconda.org/tensorly/tensorly\n\n.. image:: https://github.com/tensorly/tensorly/workflows/Test%20TensorLy/badge.svg\n    :target: https://github.com/tensorly/tensorly/actions?query=workflow%3A%22Test+TensorLy%22\n\n.. image:: https://codecov.io/gh/tensorly/tensorly/branch/master/graph/badge.svg?token=mnZ234sGSA\n    :target: https://codecov.io/gh/tensorly/tensorly\n\n.. image:: https://img.shields.io/badge/Slack-join-brightgreen\n    :target: https://join.slack.com/t/tensorly/shared_invite/zt-wqnts2sk-wbiRX6ml~Xt6~GDYWRPFfg\n\n\n========\nTensorLy\n========\n   \n\nTensorLy 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.\n\n- **Website:** https://tensorly.org\n- **Source-code:**  https://github.com/tensorly/tensorly\n- **Jupyter Notebooks:** https://github.com/JeanKossaifi/tensorly-notebooks\n\n----------------------------\n\nInstalling TensorLy\n===================\n\nThe only pre-requisite is to have **Python 3** installed. The easiest way is via the `Anaconda distribution <https://www.anaconda.com/download/>`_.\n\n+-------------------------------------------+---------------------------------------------------+\n| \u00a0 \u00a0  **With pip** (recommended)           | \u00a0 \u00a0 \u00a0 \u00a0 **With conda**                            |\n+-------------------------------------------+---------------------------------------------------+\n|                                           |                                                   |\n| .. code::                                 | .. code::                                         |\n|                                           |                                                   |\n|   pip install -U tensorly                 |   conda install -c tensorly tensorly              |\n|                                           |                                                   |\n|                                           |                                                   |\n+-------------------------------------------+---------------------------------------------------+\n|                               **Development (from git)**                                      |\n+-------------------------------------------+---------------------------------------------------+\n|                                                                                               |\n|          .. code::                                                                            |\n|                                                                                               |\n|             # clone the repository                                                            |\n|             git clone https://github.com/tensorly/tensorly                                    |\n|             cd tensorly                                                                       |\n|             # Install in editable mode with `-e` or, equivalently, `--editable`               |\n|             pip install -e .                                                                  |\n|                                                                                               |\n+-----------------------------------------------------------------------------------------------+  \n \n**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.\n\nFor detailed instruction, please see the `documentation <http://tensorly.org/dev/installation.html>`_.\n\n------------------\n\nQuickstart\n==========\n\nCreating tensors\n----------------\n\nCreate a small third order tensor of size 3 x 4 x 2, from a NumPy array and perform simple operations on it:\n\n.. code:: python\n\n   import tensorly as tl\n   import numpy as np\n\n\n   tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float64)\n   unfolded = tl.unfold(tensor, mode=0)\n   tl.fold(unfolded, mode=0, shape=tensor.shape)\n\n\nYou can also create random tensors:\n\n.. code:: python\n\n   from tensorly import random\n   \n   # A random tensor\n   tensor = random.random_tensor((3, 4, 2))\n   # A random CP tensor in factorized form\n   cp_tensor = random.random_tensor(shape=(3, 4, 2), rank='same')\n\nYou can also create tensors in TT-format, Tucker, etc, see `random tensors <http://tensorly.org/stable/modules/api.html#module-tensorly.random>`_.\n\nSetting the backend\n-------------------\n\nYou 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:\n\n.. code:: python\n\n   tl.set_backend('pytorch') # Or 'mxnet', 'numpy', 'tensorflow', 'cupy' or 'jax'\n   tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), device='cuda:0')\n   type(tensor) # torch.Tensor\n   \n\nTensor decomposition\n--------------------\n\nApplying tensor decomposition is easy:\n\n.. code:: python\n\n   from tensorly.decomposition import tucker\n   # Apply Tucker decomposition \n   tucker_tensor = tucker(tensor, rank=[2, 2, 2])\n   # Reconstruct the full tensor from the decomposed form\n   tl.tucker_to_tensor(tucker_tensor)\n   \nWe have `many more decompositions <http://tensorly.org/stable/modules/api.html#module-tensorly.decomposition>`_ available, be sure to check them out!\n\nNext steps\n----------\nThis is just a very quick introduction to some of the basic features of TensorLy. \nFor 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\nthe `API <http://tensorly.org/dev/modules/api.html>`_   \n\nIf 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>`_!\n  \n--------------------------\n\nContributing code\n=================\n\nAll 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/>`_.\n\nBefore you submit your changes, you should make sure your code adheres to our style-guide. The easiest way to do this is with `black`:  \n\n.. code:: bash\n\n   pip install black\n   black .\n\n\nRunning the tests\n=================\n\nTesting and documentation are an essential part of this package and all functions come with uni-tests and documentation.\n\nThe tests are ran using the `pytest` package. \nFirst install `pytest`::\n\n    pip install pytest\n    \nThen to run the test, simply run, in the terminal:\n\n.. code::\n\n   pytest -v tensorly\n   \nAlternatively, you can specify for which backend you wish to run the tests:\n\n.. code::\n   \n   TENSORLY_BACKEND='numpy' pytest -v tensorly\n \n\n------------------\n\nCiting\n======\n\nIf you use TensorLy in an academic paper, please cite [1]_::\n\n    @article{tensorly,\n      author  = {Jean Kossaifi and Yannis Panagakis and Anima Anandkumar and Maja Pantic},\n      title   = {TensorLy: Tensor Learning in Python},\n      journal = {Journal of Machine Learning Research},\n      year    = {2019},\n      volume  = {20},\n      number  = {26},\n      pages   = {1-6},\n      url     = {http://jmlr.org/papers/v20/18-277.html}\n    }\n    \n    \n.. [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.\n\n\n",
    "bugtrack_url": null,
    "license": "Modified BSD",
    "summary": "Tensor learning in Python.",
    "version": "0.8.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b9ccabd16580410538beeb63dcb132d08f4689e1396426fd466aa4c40144e3f",
                "md5": "f23f4a517c24d3e6cf61cbd9bc1bd66d",
                "sha256": "ee798200a3b57d118910392d7be6da759422bc7e2c51e2a20033f80f16bf0fcb"
            },
            "downloads": -1,
            "filename": "tensorly-0.8.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f23f4a517c24d3e6cf61cbd9bc1bd66d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 228384,
            "upload_time": "2023-01-14T21:17:43",
            "upload_time_iso_8601": "2023-01-14T21:17:43.806272Z",
            "url": "https://files.pythonhosted.org/packages/7b/9c/cabd16580410538beeb63dcb132d08f4689e1396426fd466aa4c40144e3f/tensorly-0.8.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6df80ca88eb07c54b669d9be863667adbc5b7fd01ca757f65d07b294b9e0e163",
                "md5": "9622b28fc62d1af7c7ee21bffd7f16de",
                "sha256": "8e1e406463127ac239f2808b3ed10a4deaad3cbdf670cf9d113d30ab0d0d8d74"
            },
            "downloads": -1,
            "filename": "tensorly-0.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9622b28fc62d1af7c7ee21bffd7f16de",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 170118,
            "upload_time": "2023-01-14T21:17:45",
            "upload_time_iso_8601": "2023-01-14T21:17:45.623680Z",
            "url": "https://files.pythonhosted.org/packages/6d/f8/0ca88eb07c54b669d9be863667adbc5b7fd01ca757f65d07b294b9e0e163/tensorly-0.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-14 21:17:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "tensorly",
    "github_project": "tensorly",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "tensorly"
}
        
Elapsed time: 0.02789s