python tensor_TensorLy是一个用于tensor学习的快速简单的Python库

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.

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.

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

Create a small third order tensor of size 3 x 4 x 2 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)

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)

You can change the backend to perform computation with a different framework. 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:

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

tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), device='cuda:0')

type(tensor) # torch.Tensor

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!

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}

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值