python中的张量学习。
tensorl的Python项目详细描述
张力
tensorly是一个python库,旨在使tensor学习变得简单易用。它允许轻松地执行张量分解、张量学习和张量代数。它的后端系统允许使用numpy、mxnet、pytorch、tensorflow或cupy无缝执行计算,并在cpu或gpu上按比例运行方法。
- 网站:http://tensorly.org
- 源代码:https://github.com/tensorly/tensorly
- jupyter笔记本:https://github.com/JeanKossaifi/tensorly-notebooks
安装张力
唯一的先决条件是安装python 3。最简单的方法是通过Anaconda distribution。
With pip (recommended) | With conda |
^{pr 1}$ | ^{pr 2}$ |
Development (from git) | |
^{pr 3}$ |
注意:tensorly默认依赖于numpy。如果您想使用mxnet或pytorch后端,则需要分别安装这些软件包。
有关详细说明,请参见documentation。
运行测试
测试和文档是这个包的重要组成部分,所有功能都附带uni测试和文档。
使用pytest包运行测试(尽管您也可以使用nose)。 首先安装pytest
pip install pytest
然后要运行测试,只需在终端中运行:
pytest -v tensorly
或者,您可以指定要运行测试的后端:
TENSORLY_BACKEND='numpy' pytest -v tensorly
快速启动
创建一个3 x 4 x 2大小的小三阶张量并对其执行简单操作:
importtensorlyastlimportnumpyasnptensor=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)
应用张量分解很容易:
fromtensorly.decompositionimporttucker# Apply Tucker decompositioncore,factors=tucker(tensor,rank=[2,2,2])# Reconstruct the full tensor from the decomposed formtl.tucker_to_tensor(core,factors)
您可以更改后端以使用不同的框架执行计算。注意,使用mxnet、pytorch、tensorflow或cupy需要先安装它们。例如,将后端设置为pytorch后,所有计算都由pytorch完成,并且可以在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
有关入门的更多信息,请查看user-guide,有关函数及其文档的详细参考,请参阅 API
如果你看到一个bug,打开一个issue,或者更好的,一个pull-request!
引用
如果在学术论文中使用tensorly,请引用[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. |