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 reconstruct to compute part of a full 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
rng('default'); %<- Setting random seed for reproducibility of this script
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.8147 0.9134 0.9058 0.6324 0.1270 0.0975 X.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 X.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 X.U{3} = 0.1712 0.7060 0.0318
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.2238 (2,1,1) 0.7513 (3,1,1) 0.2551 Y.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 Y.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 Y.U{3} = 0.1712 0.7060 0.0318
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.5060 0.9593 0.6991 0.5472 0.8909 0.1386 Y.core.U{2} = 0.1493 0.8407 0.2575 0.2543 Y.core.U{3} = 0.8143 0.2435 Y.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 Y.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 Y.U{3} = 0.1712 0.7060 0.0318
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.5060 0.9593 0.6991 0.5472 0.8909 0.1386 Y.core.U{2} = 0.1493 0.8407 0.2575 0.2543 Y.core.U{3} = 0.8143 0.2435 Y.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 Y.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 Y.U{3} = 0.1712 0.7060 0.0318
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.9293 0.3500 Z.U{1} = 0.1966 0.3517 0.2511 0.8308 0.6160 0.5853 0.4733 0.5497
Constituent parts of a ttensor
X.core %<-- Core tensor.
ans is a tensor of size 3 x 2 x 1 ans(:,:,1) = 0.8147 0.9134 0.9058 0.6324 0.1270 0.0975
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.8147 0.9134 0.9058 0.6324 0.1270 0.0975 Y.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 Y.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 Y.U{3} = 0.1712 0.7060 0.0318
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.8147 0.9134 0.9058 0.6324 0.1270 0.0975 X.U{1} = 0.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 X.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 X.U{3} = 0.1712 0.7060 0.0318
full(X) %<-- Converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.1250 0.2842 0.2465 0.2368 0.1636 0.3596 0.3086 0.3000 0.1714 0.3521 0.2956 0.2947 0.2021 0.4257 0.3605 0.3559 0.0407 0.0873 0.0744 0.0729 ans(:,:,2) = 0.5157 1.1723 1.0165 0.9765 0.6749 1.4832 1.2729 1.2374 0.7067 1.4520 1.2193 1.2153 0.8335 1.7560 1.4869 1.4679 0.1678 0.3601 0.3068 0.3008 ans(:,:,3) = 0.0233 0.0529 0.0458 0.0440 0.0304 0.0669 0.0574 0.0558 0.0319 0.0655 0.0550 0.0548 0.0376 0.0792 0.0670 0.0662 0.0076 0.0162 0.0138 0.0136
tensor(X) %<-- Also converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.1250 0.2842 0.2465 0.2368 0.1636 0.3596 0.3086 0.3000 0.1714 0.3521 0.2956 0.2947 0.2021 0.4257 0.3605 0.3559 0.0407 0.0873 0.0744 0.0729 ans(:,:,2) = 0.5157 1.1723 1.0165 0.9765 0.6749 1.4832 1.2729 1.2374 0.7067 1.4520 1.2193 1.2153 0.8335 1.7560 1.4869 1.4679 0.1678 0.3601 0.3068 0.3008 ans(:,:,3) = 0.0233 0.0529 0.0458 0.0440 0.0304 0.0669 0.0574 0.0558 0.0319 0.0655 0.0550 0.0548 0.0376 0.0792 0.0670 0.0662 0.0076 0.0162 0.0138 0.0136
Use reconstruct to compute part of a full tensor
See also Partial Reconstruction
reconstruct(X,3,1) % Extract first front slice
ans is a tensor of size 5 x 4 x 1 ans(:,:,1) = 0.1250 0.2842 0.2465 0.2368 0.1636 0.3596 0.3086 0.3000 0.1714 0.3521 0.2956 0.2947 0.2021 0.4257 0.3605 0.3559 0.0407 0.0873 0.0744 0.0729
Use double to convert a ttensor to a (multidimensional) array
double(X) %<-- Converts to a MATLAB array
ans(:,:,1) = 0.1250 0.2842 0.2465 0.2368 0.1636 0.3596 0.3086 0.3000 0.1714 0.3521 0.2956 0.2947 0.2021 0.4257 0.3605 0.3559 0.0407 0.0873 0.0744 0.0729 ans(:,:,2) = 0.5157 1.1723 1.0165 0.9765 0.6749 1.4832 1.2729 1.2374 0.7067 1.4520 1.2193 1.2153 0.8335 1.7560 1.4869 1.4679 0.1678 0.3601 0.3068 0.3008 ans(:,:,3) = 0.0233 0.0529 0.0458 0.0440 0.0304 0.0669 0.0574 0.0558 0.0319 0.0655 0.0550 0.0548 0.0376 0.0792 0.0670 0.0662 0.0076 0.0162 0.0138 0.0136
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.8147
X.U{2} %<-- Extract a matrix.
ans = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555
X{2} %<-- Same as above.
ans = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555
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.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 X.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 X.U{3} = 0.1712 0.7060 0.0318
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.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 X.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 X.U{3} = 0.1712 0.7060 0.0318
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.2785 0.9706 0.4218 0.5469 0.9572 0.9157 0.9575 0.4854 0.7922 0.9649 0.8003 0.9595 0.1576 0.1419 0.6557 X.U{2} = 0.0357 0.7577 0.8491 0.7431 0.9340 0.3922 0.6787 0.6555 X.U{3} = 1.0000 1.0000 0.0318
Using end for last index
X{end} %<-- The same as X{3}.
ans = 1.0000 1.0000 0.0318
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.9172 0.7572 0.2858 0.7537 X.core(:,:,2) = 0.3804 0.0759 0.5678 0.0540 X.U{1} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694 X.U{2} = 0.0119 0.3371 X.U{3} = 0.1622 0.3112 0.7943 0.5285 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.9172 0.7572 0.2858 0.7537 ans.core(:,:,2) = 0.3804 0.0759 0.5678 0.0540 ans.U{1} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694 ans.U{2} = 0.0119 0.3371 ans.U{3} = 0.1622 0.3112 0.7943 0.5285
-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.9172 -0.7572 -0.2858 -0.7537 ans.core(:,:,2) = -0.3804 -0.0759 -0.5678 -0.0540 ans.U{1} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694 ans.U{2} = 0.0119 0.3371 ans.U{3} = 0.1622 0.3112 0.7943 0.5285
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) = 4.5860 3.7860 1.4292 3.7686 ans.core(:,:,2) = 1.9022 0.3793 2.8391 0.2698 ans.U{1} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694 ans.U{2} = 0.0119 0.3371 ans.U{3} = 0.1622 0.3112 0.7943 0.5285
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.9172 0.7572 0.3804 0.0759 ans.core(:,:,2) = 0.2858 0.7537 0.5678 0.0540 ans.U{1} = 0.1622 0.3112 0.7943 0.5285 ans.U{2} = 0.0119 0.3371 ans.U{3} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694
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.9172 0.7572 0.2858 0.7537 ans.core(:,:,2) = 0.3804 0.0759 0.5678 0.0540 ans.U{1} = 0.5308 0.1299 0.7792 0.5688 0.9340 0.4694 ans.U{2} = 0.0119 0.3371 ans.U{3} = 0.1622 0.3112 0.7943 0.5285