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.0142 0.9771
0.5962 0.2219
0.8162 0.7037
X.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
X.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X.U{3} =
0.3193
0.3749
0.8678
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.375056
(1,2,1) 0.823387
(2,1,1) 0.0466361
Y.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
Y.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
Y.U{3} =
0.3193
0.3749
0.8678
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.5979 0.8888
0.9492 0.1016
0.2888 0.0653
Y.core.U{2} =
0.2343 0.0631
0.9331 0.2642
Y.core.U{3} =
0.9995 0.2120
Y.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
Y.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
Y.U{3} =
0.3193
0.3749
0.8678
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.5979 0.8888
0.9492 0.1016
0.2888 0.0653
Y.core.U{2} =
0.2343 0.0631
0.9331 0.2642
Y.core.U{3} =
0.9995 0.2120
Y.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
Y.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
Y.U{3} =
0.3193
0.3749
0.8678
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.4984
0.2905
Z.U{1} =
0.6728 0.1309
0.9580 0.0954
0.7666 0.0149
0.6661 0.2882
Constituent parts of a ttensor
X.core %<-- Core tensor.
ans is a tensor of size 3 x 2 x 1 ans(:,:,1) = 0.0142 0.9771 0.5962 0.2219 0.8162 0.7037
X.U %<-- Cell array of matrices.
ans =
[5x3 double] [4x2 double] [3x1 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.0142 0.9771
0.5962 0.2219
0.8162 0.7037
Y.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
Y.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
Y.U{3} =
0.3193
0.3749
0.8678
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.0142 0.9771
0.5962 0.2219
0.8162 0.7037
X.U{1} =
0.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
X.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X.U{3} =
0.3193
0.3749
0.8678
full(X) %<-- Converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.4236 0.2188 0.5476 0.2676 0.4406 0.2191 0.5840 0.2934 0.2668 0.1204 0.3746 0.1995 0.3678 0.2009 0.4567 0.2128 0.2709 0.1444 0.3425 0.1631 ans(:,:,2) = 0.4974 0.2569 0.6430 0.3142 0.5173 0.2573 0.6857 0.3445 0.3132 0.1414 0.4398 0.2343 0.4318 0.2359 0.5363 0.2498 0.3181 0.1696 0.4022 0.1915 ans(:,:,3) = 1.1514 0.5948 1.4883 0.7272 1.1975 0.5956 1.5872 0.7974 0.7251 0.3273 1.0180 0.5423 0.9996 0.5461 1.2413 0.5783 0.7363 0.3925 0.9310 0.4434
tensor(X) %<-- Also converts to a tensor.
ans is a tensor of size 5 x 4 x 3 ans(:,:,1) = 0.4236 0.2188 0.5476 0.2676 0.4406 0.2191 0.5840 0.2934 0.2668 0.1204 0.3746 0.1995 0.3678 0.2009 0.4567 0.2128 0.2709 0.1444 0.3425 0.1631 ans(:,:,2) = 0.4974 0.2569 0.6430 0.3142 0.5173 0.2573 0.6857 0.3445 0.3132 0.1414 0.4398 0.2343 0.4318 0.2359 0.5363 0.2498 0.3181 0.1696 0.4022 0.1915 ans(:,:,3) = 1.1514 0.5948 1.4883 0.7272 1.1975 0.5956 1.5872 0.7974 0.7251 0.3273 1.0180 0.5423 0.9996 0.5461 1.2413 0.5783 0.7363 0.3925 0.9310 0.4434
Use double to convert a ttensor to a (multidimensional) array
double(X) %<-- Converts to a MATLAB array
ans(:,:,1) =
0.4236 0.2188 0.5476 0.2676
0.4406 0.2191 0.5840 0.2934
0.2668 0.1204 0.3746 0.1995
0.3678 0.2009 0.4567 0.2128
0.2709 0.1444 0.3425 0.1631
ans(:,:,2) =
0.4974 0.2569 0.6430 0.3142
0.5173 0.2573 0.6857 0.3445
0.3132 0.1414 0.4398 0.2343
0.4318 0.2359 0.5363 0.2498
0.3181 0.1696 0.4022 0.1915
ans(:,:,3) =
1.1514 0.5948 1.4883 0.7272
1.1975 0.5956 1.5872 0.7974
0.7251 0.3273 1.0180 0.5423
0.9996 0.5461 1.2413 0.5783
0.7363 0.3925 0.9310 0.4434
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.0142
X.U{2} %<-- Extract a matrix.
ans =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X{2} %<-- Same as above.
ans =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
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.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
X.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X.U{3} =
0.3193
0.3749
0.8678
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.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
X.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X.U{3} =
0.3193
0.3749
0.8678
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.5221 0.1722 0.8948
0.9329 0.9688 0.2861
0.7134 0.3557 0.2512
0.2280 0.0490 0.9327
0.4496 0.7553 0.1310
X.U{2} =
0.9408 0.4551
0.7019 0.0811
0.8477 0.8511
0.2093 0.5620
X.U{3} =
1.0000
1.0000
0.8678
Using end for last index
X{end} %<-- The same as X{3}.
ans =
1.0000
1.0000
0.8678
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.8167 0.0174
0.9855 0.8194
X.core(:,:,2) =
0.6211 0.2440
0.5602 0.8220
X.U{1} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
X.U{2} =
0.6595 0.1834
X.U{3} =
0.6365 0.5396
0.1703 0.6234
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.8167 0.0174
0.9855 0.8194
ans.core(:,:,2) =
0.6211 0.2440
0.5602 0.8220
ans.U{1} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
ans.U{2} =
0.6595 0.1834
ans.U{3} =
0.6365 0.5396
0.1703 0.6234
-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.8167 -0.0174
-0.9855 -0.8194
ans.core(:,:,2) =
-0.6211 -0.2440
-0.5602 -0.8220
ans.U{1} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
ans.U{2} =
0.6595 0.1834
ans.U{3} =
0.6365 0.5396
0.1703 0.6234
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.0837 0.0868
4.9274 4.0970
ans.core(:,:,2) =
3.1057 1.2202
2.8011 4.1100
ans.U{1} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
ans.U{2} =
0.6595 0.1834
ans.U{3} =
0.6365 0.5396
0.1703 0.6234
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.8167 0.0174
0.6211 0.2440
ans.core(:,:,2) =
0.9855 0.8194
0.5602 0.8220
ans.U{1} =
0.6365 0.5396
0.1703 0.6234
ans.U{2} =
0.6595 0.1834
ans.U{3} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
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.8167 0.0174
0.9855 0.8194
ans.core(:,:,2) =
0.6211 0.2440
0.5602 0.8220
ans.U{1} =
0.2632 0.2141
0.7536 0.6021
0.6596 0.6049
ans.U{2} =
0.6595 0.1834
ans.U{3} =
0.6365 0.5396
0.1703 0.6234