Tucker tensors
Tucker format is a decomposition of a tensor X as the product of a core tensor G and matrices (e.g., A,B,C) in each dimension. In other words, a tensor X is expressed as:
In MATLAB notation, X=ttm(G,{A,B,C}). The ttensor class stores the components of the tensor X and can perform many operations, e.g., ttm, without explicitly forming the tensor X.
Contents
- Creating a ttensor with a tensor core
- Alternate core formats: sptensor, ktensor, or ttensor
- Creating a one-dimensional ttensor
- Constituent parts of a ttensor
- Creating a ttensor from its constituent parts
- Creating an empty ttensor.
- Use full or tensor to convert a ttensor to a tensor
- Use double to convert a ttensor to a (multidimensional) array
- Use ndims and size to get the size of a ttensor
- Subscripted reference to a ttensor
- Subscripted assignment for a ttensor
- Using end for last index
- Basic operations (uplus, uminus, mtimes) for a ttensor.
- Use permute to reorder the modes of a ttensor
- Displaying a ttensor
Creating a ttensor with a tensor core
core = tensor(rand(3,2,1),[3 2 1]); %<-- The core tensor. U = {rand(5,3), rand(4,2), rand(3,1)}; %<-- The matrices. X = ttensor(core,U) %<-- Create the ttensor.
X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 0.1298 0.8117 0.1048 0.6696 0.0632 0.6540 X.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 X.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 X.U{3} = 0.4853 0.5602 0.2898
Alternate core formats: sptensor, ktensor, or ttensor
core1 = sptenrand([3 2 1],3); %<-- Create a 3 x 2 x 1 sptensor. Y = ttensor(core1,U) %<-- Core is a sptensor.
Y is a ttensor of size 5 x 4 x 3 Y.core is a sparse tensor of size 3 x 2 x 1 with 3 nonzeros (1,1,1) 0.9871 (2,2,1) 0.5015 (3,2,1) 0.8833 Y.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 Y.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 Y.U{3} = 0.4853 0.5602 0.2898
V = {rand(3,2),rand(2,2),rand(1,2)}; %<-- Create some random matrices. core2 = ktensor(V); %<-- Create a 3 x 2 x 1 ktensor. Y = ttensor(core2,U) %<-- Core is a ktensor.
Y is a ttensor of size 5 x 4 x 3 Y.core is a ktensor of size 3 x 2 x 1 Y.core.lambda = [ 1 1 ] Y.core.U{1} = 0.8746 0.7261 0.6178 0.2917 0.9611 0.4665 Y.core.U{2} = 0.9439 0.0119 0.0943 0.3723 Y.core.U{3} = 0.3542 0.0820 Y.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 Y.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 Y.U{3} = 0.4853 0.5602 0.2898
core3 = ttensor(tensor(1:8,[2 2 2]),V); %<-- Create a 3 x 2 x 1 ttensor. Y = ttensor(core3,U) %<-- Core is a ttensor.
Y is a ttensor of size 5 x 4 x 3 Y.core is a ttensor of size 3 x 2 x 1 Y.core.core is a tensor of size 2 x 2 x 2 Y.core.core(:,:,1) = 1 3 2 4 Y.core.core(:,:,2) = 5 7 6 8 Y.core.U{1} = 0.8746 0.7261 0.6178 0.2917 0.9611 0.4665 Y.core.U{2} = 0.9439 0.0119 0.0943 0.3723 Y.core.U{3} = 0.3542 0.0820 Y.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 Y.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 Y.U{3} = 0.4853 0.5602 0.2898
Creating a one-dimensional ttensor
Z = ttensor(tensor(rand(2,1),2), rand(4,2)) %<-- One-dimensional ttensor.
Z is a ttensor of size 4 Z.core is a tensor of size 2 Z.core(:) = 0.3109 0.2558 Z.U{1} = 0.1048 0.3074 0.2903 0.7715 0.4985 0.2026 0.8205 0.9396
Constituent parts of a ttensor
X.core %<-- Core tensor.
ans is a tensor of size 3 x 2 x 1 ans(:,:,1) = 0.1298 0.8117 0.1048 0.6696 0.0632 0.6540
X.U %<-- Cell array of matrices.
ans = 1×3 cell array [5×3 double] [4×2 double] [3×1 double]
Creating a ttensor from its constituent parts
Y = ttensor(X.core,X.U) %<-- Recreate a tensor from its parts.
Y is a ttensor of size 5 x 4 x 3 Y.core is a tensor of size 3 x 2 x 1 Y.core(:,:,1) = 0.1298 0.8117 0.1048 0.6696 0.0632 0.6540 Y.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 Y.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 Y.U{3} = 0.4853 0.5602 0.2898
Creating an empty ttensor.
X = ttensor %<-- empty ttensor
X is a ttensor of size [empty tensor] X.core is a tensor of size [empty tensor] X.core = []
Use full or tensor to convert a ttensor to a tensor
X = ttensor(core,U) %<-- Create a tensor
X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 0.1298 0.8117 0.1048 0.6696 0.0632 0.6540 X.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 X.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 X.U{3} = 0.4853 0.5602 0.2898
full(X) %<-- Converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.8341 0.7370 0.3768 0.6139 0.4723 0.4201 0.2122 0.3489 0.2987 0.2622 0.1356 0.2190 0.5438 0.4827 0.2447 0.4012 0.7929 0.6988 0.3589 0.5827 ans(:,:,2) = 0.9627 0.8507 0.4349 0.7086 0.5451 0.4849 0.2450 0.4027 0.3447 0.3026 0.1566 0.2528 0.6277 0.5572 0.2825 0.4631 0.9152 0.8066 0.4143 0.6726 ans(:,:,3) = 0.4980 0.4401 0.2250 0.3665 0.2820 0.2508 0.1267 0.2083 0.1783 0.1566 0.0810 0.1308 0.3247 0.2882 0.1461 0.2396 0.4734 0.4172 0.2143 0.3479
tensor(X) %<-- Also converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.8341 0.7370 0.3768 0.6139 0.4723 0.4201 0.2122 0.3489 0.2987 0.2622 0.1356 0.2190 0.5438 0.4827 0.2447 0.4012 0.7929 0.6988 0.3589 0.5827 ans(:,:,2) = 0.9627 0.8507 0.4349 0.7086 0.5451 0.4849 0.2450 0.4027 0.3447 0.3026 0.1566 0.2528 0.6277 0.5572 0.2825 0.4631 0.9152 0.8066 0.4143 0.6726 ans(:,:,3) = 0.4980 0.4401 0.2250 0.3665 0.2820 0.2508 0.1267 0.2083 0.1783 0.1566 0.0810 0.1308 0.3247 0.2882 0.1461 0.2396 0.4734 0.4172 0.2143 0.3479
Use double to convert a ttensor to a (multidimensional) array
double(X) %<-- Converts to a MATLAB array
ans(:,:,1) = 0.8341 0.7370 0.3768 0.6139 0.4723 0.4201 0.2122 0.3489 0.2987 0.2622 0.1356 0.2190 0.5438 0.4827 0.2447 0.4012 0.7929 0.6988 0.3589 0.5827 ans(:,:,2) = 0.9627 0.8507 0.4349 0.7086 0.5451 0.4849 0.2450 0.4027 0.3447 0.3026 0.1566 0.2528 0.6277 0.5572 0.2825 0.4631 0.9152 0.8066 0.4143 0.6726 ans(:,:,3) = 0.4980 0.4401 0.2250 0.3665 0.2820 0.2508 0.1267 0.2083 0.1783 0.1566 0.0810 0.1308 0.3247 0.2882 0.1461 0.2396 0.4734 0.4172 0.2143 0.3479
Use ndims and size to get the size of a ttensor
ndims(X) %<-- Number of dimensions.
ans = 3
size(X) %<-- Row vector of the sizes.
ans = 5 4 3
size(X,2) %<-- Size of the 2nd mode.
ans = 4
Subscripted reference to a ttensor
X.core(1,1,1) %<-- Access an element of the core.
ans = 0.1298
X.U{2} %<-- Extract a matrix.
ans = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291
X{2} %<-- Same as above.
ans = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291
Subscripted assignment for a ttensor
X.core = tenones(size(X.core)) %<-- Insert a new core.
X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 1 1 1 X.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 X.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 X.U{3} = 0.4853 0.5602 0.2898
X.core(2,2,1) = 7 %<-- Change a single element.
X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 7 1 1 X.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 X.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 X.U{3} = 0.4853 0.5602 0.2898
X{3}(1:2,1) = [1;1] %<-- Change the matrix for mode 3.
X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 7 1 1 X.U{1} = 0.5678 0.8964 0.8084 0.3669 0.1125 0.8239 0.2715 0.4471 0.0643 0.3697 0.3141 0.8189 0.6985 0.8726 0.5384 X.U{2} = 0.9079 0.9561 0.4013 0.9000 0.5772 0.4089 0.4832 0.7291 X.U{3} = 1.0000 1.0000 0.2898
Using end for last index
X{end} %<-- The same as X{3}.
ans = 1.0000 1.0000 0.2898
Basic operations (uplus, uminus, mtimes) for a ttensor.
X = ttensor(tenrand([2 2 2]),{rand(3,2),rand(1,2),rand(2,2)}) %<-- Data. +X %<-- Calls uplus.
X is a ttensor of size 3 x 1 x 2 X.core is a tensor of size 2 x 2 x 2 X.core(:,:,1) = 0.2107 0.6356 0.9670 0.4252 X.core(:,:,2) = 0.2262 0.7426 0.9325 0.5133 X.U{1} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121 X.U{2} = 0.0968 0.1922 X.U{3} = 0.0639 0.4619 0.4969 0.8735 ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.2107 0.6356 0.9670 0.4252 ans.core(:,:,2) = 0.2262 0.7426 0.9325 0.5133 ans.U{1} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121 ans.U{2} = 0.0968 0.1922 ans.U{3} = 0.0639 0.4619 0.4969 0.8735
-X %<-- Calls uminus.
ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = -0.2107 -0.6356 -0.9670 -0.4252 ans.core(:,:,2) = -0.2262 -0.7426 -0.9325 -0.5133 ans.U{1} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121 ans.U{2} = 0.0968 0.1922 ans.U{3} = 0.0639 0.4619 0.4969 0.8735
5*X %<-- Calls mtimes.
ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 1.0536 3.1781 4.8349 2.1261 ans.core(:,:,2) = 1.1311 3.7129 4.6625 2.5665 ans.U{1} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121 ans.U{2} = 0.0968 0.1922 ans.U{3} = 0.0639 0.4619 0.4969 0.8735
Use permute to reorder the modes of a ttensor
permute(X,[3 2 1]) %<-- Reverses the modes of X
ans is a ttensor of size 2 x 1 x 3 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.2107 0.6356 0.2262 0.7426 ans.core(:,:,2) = 0.9670 0.4252 0.9325 0.5133 ans.U{1} = 0.0639 0.4619 0.4969 0.8735 ans.U{2} = 0.0968 0.1922 ans.U{3} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121
Displaying a ttensor
The tensor displays by displaying the core and each of the component matrices.
disp(X) %<-- Prints out the ttensor.
ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.2107 0.6356 0.9670 0.4252 ans.core(:,:,2) = 0.2262 0.7426 0.9325 0.5133 ans.U{1} = 0.5417 0.6280 0.2143 0.0907 0.8007 0.8121 ans.U{2} = 0.0968 0.1922 ans.U{3} = 0.0639 0.4619 0.4969 0.8735