张量tensorly笔记(1)——张量基础

这里写图片描述
一个非常形象的图片表示了从标量到张量的演变过程


  • 使用python里面的numpy库和tensorly库来学习张量

  • 张量构建
    这里写图片描述

import numpy as np
import tensorly as tl
X = tl.tensor(np.arange(24).reshape(3, 4, 2))

reshape将np创建的数组变换维度,返回的是一个3*4*2的三维张量
这里写图片描述

  • 张量分片
print(X[:, :, 0])
print(X[:, :, 1])

这里写图片描述

  • 张量矩阵化(Matricization)输出张量的三个矩阵模态
print(tl.unfold(X, mode=0))
print(tl.unfold(X, mode=1))
print(tl.unfold(X, mode=2))

这里写图片描述

  • 同时使用fold函数可以将矩阵转变为对应模态的张量
unfolding = tl.unfold(X, 1)
original_shape = X.shape
tl.fold(unfolding, mode=1, shape=original_sha

  • 张量与矩阵或者向量的n-mode乘(mode_dot函数)
    这里写图片描述
  • 张量乘矩阵
M = tl.tensor(np.arange(4*5).reshape(5, 4))
print(M.shape)
res = tl.tenalg.mode_dot(X, M, mode=1)

转载自
https://github.com/hustfc/tensorlynotebooks/blob/master/01_tensor_basics/tensor_manipulation.ipynb

猜你喜欢

转载自blog.csdn.net/weixin_36372879/article/details/80743720