tensorly实现张量的展开及逆过程

import tensorly as tl
import numpy as np
a = np.array([[[1,2,3,4],[4,5,6,7],[7,8,9,10]],
            [[11,12,13,14],[20,21,22,23],[15,16,17,18]]])
print(a.shape)
b = tl.unfold(a,mode=0) #实现1模展开
print('b=',b)
c = tl.fold(b,mode=0,shape=(2,3,4)) #相当于张量展开的逆过程
print('c=',c)

#out

(2, 3, 4)
b= [[ 1  2  3  4  4  5  6  7  7  8  9 10]
 [11 12 13 14 20 21 22 23 15 16 17 18]]
c= [[[ 1  2  3  4]
  [ 4  5  6  7]
  [ 7  8  9 10]]

 [[11 12 13 14]
  [20 21 22 23]
  [15 16 17 18]]]

 

你可能感兴趣的:(py-tensorly)