Generating the leading mode-n vectors
The leading mode-n vectors are those vectors that span the subspace of the mode-n fibers. In other words, the left singular vectors of the n-mode matricization of X.
Contents
Using nvecs to calculate the leading mode-n vectors
The nvecs command efficient computes the leading n-mode vectors.
rand('state',0); X = sptenrand([4,3,2],6) %<-- A sparse tensor
X is a sparse tensor of size 4 x 3 x 2 with 6 nonzeros (1,2,1) 0.8385 (2,3,1) 0.5681 (3,2,1) 0.3704 (3,3,1) 0.7027 (4,2,2) 0.5466 (4,3,2) 0.4449
nvecs(X,1,2) %<-- The 2 leading mode-1 vectors
ans =
0.5810 0.7687
0.3761 -0.5451
0.7219 -0.3347
-0.0000 -0.0000
nvecs(X,1,3) % <-- The 3 leading mode-1 vectors
ans =
0.5810 0.7687 0.0000
0.3761 -0.5451 -0.0000
0.7219 -0.3347 -0.0000
0.0000 -0.0000 1.0000
nvecs(full(X),1,3) %<-- The same thing for a dense tensor
ans =
0.5810 0.7687 0.0000
0.3761 -0.5451 -0.0000
0.7219 -0.3347 -0.0000
-0.0000 -0.0000 1.0000
X = ktensor({rand(3,2),rand(3,2),rand(2,2)}) %<-- A random ktensor
X is a ktensor of size 3 x 3 x 2
X.lambda = [ 1 1 ]
X.U{1} =
0.1365 0.1991
0.0118 0.2987
0.8939 0.6614
X.U{2} =
0.2844 0.9883
0.4692 0.5828
0.0648 0.4235
X.U{3} =
0.5155 0.4329
0.3340 0.2259
nvecs(X,2,1) %<-- The 1 leading mode-2 vector
ans =
0.7147
0.6480
0.2633
nvecs(full(X),2,1) %<-- Same thing for a dense tensor
ans =
0.7147
0.6480
0.2633
X = ttensor(tenrand([2,2,2,2]),{rand(3,2),rand(3,2),rand(2,2),rand(2,2)}); %<-- A random ttensor
nvecs(X,4,2) %<-- The 1 leading mode-2 vector
ans =
0.7401 0.6725
-0.6725 0.7401
nvecs(full(X),4,2) %<-- Same thing for a dense tensor
ans =
0.7401 0.6725
-0.6725 0.7401
Using nvecs for the HOSVD
X = tenrand([4 3 2]) %<-- Generate data
X is a tensor of size 4 x 3 x 2 X(:,:,1) = 0.0272 0.6831 0.6085 0.3127 0.0928 0.0158 0.0129 0.0353 0.0164 0.3840 0.6124 0.1901 X(:,:,2) = 0.5869 0.7176 0.4418 0.0576 0.6927 0.3533 0.3676 0.0841 0.1536 0.6315 0.4544 0.6756
U1 = nvecs(X,1,4); %<-- Mode 1 U2 = nvecs(X,2,3); %<-- Mode 2 U3 = nvecs(X,3,2); %<-- Mode 3 S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core Y = ttensor(S,{U1,U2,U3}) %<-- HOSVD of X
Y is a ttensor of size 4 x 3 x 2
Y.core is a tensor of size 4 x 3 x 2
Y.core(:,:,1) =
0.0974 -0.0452 -0.1786
-0.3189 0.0200 -0.0393
-0.0932 0.5059 -0.2194
-0.0911 -0.2314 0.0560
Y.core(:,:,2) =
-0.0282 0.0852 0.0013
0.0848 0.3216 -0.0229
-0.1012 -0.1193 0.0669
-0.0009 0.0684 1.9279
Y.U{1} =
0.0424 -0.6031 -0.4132 0.6809
0.1799 -0.2230 0.8973 0.3358
0.9126 0.3452 -0.1551 0.1548
-0.3647 0.6836 0.0064 0.6321
Y.U{2} =
-0.1521 0.8780 0.4538
-0.5319 -0.4597 0.7111
0.8330 -0.1332 0.5370
Y.U{3} =
0.8409 0.5412
-0.5412 0.8409
norm(full(Y) - X) %<-- Reproduces the same result.
ans = 9.0558e-16
U1 = nvecs(X,1,2); %<-- Mode 1 U2 = nvecs(X,2,2); %<-- Mode 2 U3 = nvecs(X,3,2); %<-- Mode 3 S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core Y = ttensor(S,{U1,U2,U3}) %<-- Rank-(2,2,2) HOSVD approximation of X
Y is a ttensor of size 4 x 3 x 2
Y.core is a tensor of size 2 x 2 x 2
Y.core(:,:,1) =
0.0560 -0.2314
-0.2194 0.5059
Y.core(:,:,2) =
1.9279 0.0684
0.0669 -0.1193
Y.U{1} =
0.6809 -0.4132
0.3358 0.8973
0.1548 -0.1551
0.6321 0.0064
Y.U{2} =
0.4538 0.8780
0.7111 -0.4597
0.5370 -0.1332
Y.U{3} =
0.8409 0.5412
-0.5412 0.8409
100*(1-norm(full(Y)-X)/norm(X)) %<-- Percentage explained by approximation
ans = 74.1571