Use of MATLAB Tensor Toolbox

Use of MATLAB Tensor Toolbox


MATLAB's Tensor Toolbox is a powerful tensor tool that provides some powerful tensor functions.
Download address: the latest version download address

tensor storage format

There are many storage formats for tensors in the toolbox, here are two.

Tucker format

Tucker format is tensor XXX is decomposed into the core tensorGGG and matrices in each dimension (for example,A , B , CA , B , CA , B , C ) product. In other words, tensorXXX can be expressed as:
X = G × 1 A × 2 B × 2 C \mathcal{X}=\mathcal{G} \times_{1} A \times{ }_{2} B \times{ }_{2 } CX=G×1A×2B×2The decomposition expression of C
looks confusing, it doesn’t matter, we write it in the form of summation:
X = ∑ i = a 1 an ∑ j = b 1 bn ∑ k = c 1 cn G ( i , j , k ) A ( : , i ) ⊗ B ( : , j ) ⊗ C ( : , k ) . \mathcal{X} = \sum_{i=a_1}^{a_n}\sum_{j=b_1}^{b_n}\ sum_{k=c_1}^{c_n}\mathcal{G}(i,j,k)A(:,i)\otimes B(:,j)\otimes C(:,k).X=i=a1anj=b1bnk=c1cnG(i,j,k)A(:,i)B(:,j)C(:,k).

In a word, A, B, CA, B, CA,B,All the columns in C are taken out and summed after doing the Kronecker inner product. The coefficients of the summation come from the ( i , j , k ) (i,j,k)of the tensor $ \mathcal{G}$(i,j,k ) position, here( i , j , k ) (i,j,k)(i,j,k ) is the extractedA , B , CA,B,CA,B,C 's( i , j , k ) (i,j,k)(i,j,k ) column. tensor product⊗ \otimes⊗Some places are also written as∘ \circ , represents the outer product.

The representation in MATLAB is X = ttm ( G , { A , B , C } ) \mathrm{X}=\mathrm{ttm}(\mathrm{G},\{\mathrm{A}, \mathrm{B }, \mathrm{C}\})X=ttm(G,{ A,B,C }) . ttensor format stores tensorX \mathrm{X}components of X , and can perform many operations, such asttm \mathrm{ttm}ttm without explicitly forming the tensorX \mathrm{X}X

Show the basic usage as follows (inherited the usage of many MATLAB matrices):

clc
clear
close all
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.
core1 = sptenrand([3 2 1],3); %<-- Create a 3 x 2 x 1 sptensor. 创建一个三个非零元的系数张量
Y = ttensor(core1,U) %<-- Core is a sptensor.
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.
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.
Z = ttensor(tensor(rand(2,1),2), rand(4,2)) %<-- One-dimensional ttensor.
X.core %<-- Core tensor.
X.U %<-- Cell array of matrices.
Y = ttensor(X.core,X.U) %<-- Recreate a tensor from its parts.
X = ttensor %<-- empty ttensor
X = ttensor(core,U) %<-- Create a tensor
full(X) %<-- Converts to a tensor.张开
tensor(X) %<-- Also converts to a tensor.
double(X) %<-- Converts to a MATLAB array 转成 MATLAB 矩阵
ndims(X) %<-- Number of dimensions.
size(X) %<-- Row vector of the sizes.
size(X,2) %<-- Size of the 2nd mode.
X.core(1,1,1) %<-- Access an element of the core.
X.U{2} %<-- Extract a matrix.
X{2} %<-- Same as above.
X.core = tenones(size(X.core)) %<-- Insert a new core. ones 类型的 tensor
X.core(2,2,1) = 7 %<-- Change a single element.
X{3}(1:2,1) = [1;1] %<-- Change the matrix for mode 3.
X{end}  %<-- The same as X{3}.
X = ttensor(tenrand([2 2 2]),{rand(3,2),rand(1,2),rand(2,2)}) %<-- Data.
+X %<-- Calls uplus.
-X %<-- Calls uminus.
5*X %<-- Calls mtimes.
permute(X,[3 2 1]) %<-- Reverses the modes of X 翻滚等变换
disp(X) %<-- Prints out the ttensor.

Kruskal format

The Kruskal format is the tensor XXX decomposes into the sum of the outer products of the columns of the matrix. For example, we can write
X = ∑ rar ∘ br ∘ cr \mathcal{X}=\sum_{r} a_{r} \circ b_{r} \circ c_{r}X=rarbrcr
The subscript indicates the column index, and the circle indicates the outer product. In other words, the tensor X \mathcal{X}X is composed of matricesA, BA, BA , B andCCThe columns of C are constructed. It is often helpful to explicitly specify the weights for each outer product, which we do here:
X = ∑ r λ rar ∘ br ∘ cr \mathcal{X}=\sum_{r} \lambda_{r} a_{r} \ circ b_{r} \circ c_{r}X=rlrarbrcr
The ktensor type of the toolbox stores the tensor X \mathcal{X}components of X , and can perform many operations, such asttm \mathcal{ttm}tt m without explicitly forming the tensorX ∘ X_{\circ}X

Show the basic usage of this storage format:

clc
clear
close all
rand('state',0);
A = rand(4,2); %<-- First column is a_1, second is a_2.
B = rand(3,2); %<-- Likewise for B.
C = rand(2,2); %<-- Likewise for C.
X = ktensor({A,B,C}) %<-- Create the ktensor. 通过矩阵元胞数组转成 ktensor
Y = ktensor({rand(4,1),rand(2,1),rand(3,1)}) %<-- Another ktensor.
lambda = [5.0; 0.25]; %<-- Weights for each factor.
X = ktensor(lambda,{A,B,C}) %<-- Create the ktensor.
Y = ktensor({rand(4,5)}) %<-- A one-dimensional ktensor.
X.lambda %<-- Weights or multipliers.
X.U %<-- Cell array of matrices.
Y = ktensor(X.lambda,X.U) %<-- Recreate X.
Z = ktensor %<-- Empty ktensor.
full(X) %<-- Converts to a tensor.
tensor(X) %<-- Same as above.
double(X) %<-- Converts to an array. 通过 touble 转成高维矩阵
R = length(X.lambda);  %<-- Number of factors in X.
core = tendiag(X.lambda, repmat(R,1,ndims(X))); %<-- Create a diagonal core. 创建一个对角线的 tensor
Y = ttensor(core, X.u) %<-- Assemble the ttensor.
norm(full(X)-full(Y)) %<-- They are the same.
core = sptendiag(X.lambda, repmat(R,1,ndims(X))); %<-- Sparse diagonal core.
Y = ttensor(core, X.u) %<-- Assemble the ttensor
norm(full(X)-full(Y)) %<-- They are the same.
ndims(X) %<-- Number of dimensions.
size(X) %<-- Row vector of the sizes.
size(X,2) %<-- Size of the 2nd mode.
X(1,1,1) %<-- Assemble the (1,1,1) element (requires computation).
X.lambda(2) %<-- Weight of 2nd factor.
X.U{2} %<-- Extract a matrix.
X{2} %<-- Same as above.
X.lambda = ones(size(X.lambda)) %<-- Insert new multipliers.
X.lambda(1) = 7 %<-- Change a single element of lambda.
X{3}(1:2,1) = [1;1] %<-- Change the matrix for mode 3.
X(3:end,1,1)  %<-- Calculated X(3,1,1) and X((4,1,1). 和矩阵的用法是一样的
X(1,1,1:end-1)  %<-- Calculates X(1,1,1).
X{end}  %<-- Or use inside of curly braces. This is X{3}.
X = ktensor({rand(4,2),rand(2,2),rand(3,2)}) %<-- Data.
Y = ktensor({rand(4,2),rand(2,2),rand(3,2)}) %<-- More data.
Z = X + Y %<-- Concatenates the factor matrices.
Z = X - Y %<-- Concatenates as with plus, but changes the weights.
norm( full(Z) - (full(X)-full(Y)) ) %<-- Should be zero.
5*X %<-- Calls mtimes. 只有系数乘了
permute(X,[2 3 1]) %<-- Reorders modes of X 按这个顺序改变一下维度
X = ktensor({rand(3,2),rand(4,2),rand(2,2)})  % <-- Unit weights.
arrange(X) %<-- Normalized and rearranged.
Y = X;
Y.u{1}(:,1) = -Y.u{1}(:,1);  % switch the sign on a pair of columns
Y.u{2}(:,1) = -Y.u{2}(:,1)
fixsigns(Y)
A = rand(4,3) %<-- A random matrix.
[U,S,V] = svd(A,0); %<-- Compute the SVD.
X = ktensor(diag(S),{U,V}) %<-- Store the SVD as a ktensor.
double(X) %<-- Reassemble the original matrix.
disp(X) %<-- Displays the vector lambda and each factor matrix.
X = ktensor({[0.8 0.1 1e-10]',[1e-5 2 3 1e-4]',[0.5 0.5]'}); %<-- Create tensor.
X = arrange(X) %<-- Normalize the factors.
labelsDim1 = {'one','two','three'}; %<-- Labels for mode 1.
labelsDim2 = {'A','B','C','D'}; %<-- Labels for mode 2.
labelsDim3 = {'on','off'}; %<-- Labels for mode 3.
datadisp(X,{labelsDim1,labelsDim2,labelsDim3}) %<-- Display.数据展示

Function usage (continuously supplemented)

There are many useful functions in tensor toolbox, here are a few for illustration. For ktensor, the following functions are available:

    arrange      - 重排 ktensor 的 rank-1 组分
    datadisp     - ktensor 的展示
    disp         - 命令行展示
    display      - 命令行展示
    double       - 转为高维 double 数组
    end          - 同矩阵的用法
    extract      - 通过给定的组分形成新的 ktensor
    fixsigns     - 修复符号二义性
    full         - 同矩阵的 full
    innerprod    - ktensor 内积
    isequal      - 判断是否相等
    isscalar     - 判断是否不是 ktensor
    issymmetric  - 判断在所有的维度上是不是都是对称的
    ktensor      - 生成 ktensor
    mask         - 通过 mask tensor 提取固定的值
    minus        - ktensor 的二元相减
    mtimes       - ktensor 的标量积
    mttkrp       - 矩阵化的张量乘 Khatri-Rao 积
    ncomponents  - ktensor 的组分的数目
    ndims        -ktensor 的维度
    norm         - ktensor 的 F 范数
    normalize    - 正则化因子矩阵的每一列
    nvecs        - 计算张量前面的 n 个向量
    permute      - 置换 ktensor 不同的维度
    plus         - ktensor 的二进制加
    redistribute - 重分布 lambda 值到一个特别的模式
    score        - 判断两个 ktensor 是否相似
    size         - ktensor 的大小
    subsasgn     - ktensor 的下标分配
    subsref      - ktensor 的下标应用
    symmetrize   - 在在所有的维度对称化一个 ktensor
    times        - ktensor 之间的元素乘
    tocell       - 转为元胞数组
    tovec        - ktensor 转为向量
    ttm          - 张量乘矩阵
    ttv          - 张量乘向量
    uminus       - 张量的一元减法 
    update       - 更新张量的某一个 mode 的值
    uplus        - 张量的一元加
    viz          - ktensor 的可视化

Tensor CP decomposition (tensor completion)

The toolbox implements the CP Weighted Optimization (CP-WOPT) method, which can make the tensor missing some entries (become a tensor completion problem), and the existing entries can also add noise. This method can refer to:

E. Acar, D. M. Dunlavy, T. G. Kolda and M. Mørup, Scalable Tensor Factorizations for Incomplete Data, Chemometrics and Intelligent Laboratory Systems, 106(1):41-56, 2011

Paste some sample code as follows:

clc
clear
close all
R = 2;
info = create_problem('Size', [15 10 5], 'Num_Factors', R, ...
    'M', 0.25, 'Noise', 0.10);
X = info.Data;
P = info.Pattern;
M_true= info.Soln;
M_init = create_guess('Data', X, 'Num_Factors', R, ...
    'Factor_Generator', 'nvecs');
[M,~,output] = cp_wopt(X, P, R, 'init', M_init);
exitmsg = output.ExitMsg
scr = score(M,M_true)
clc
clear
close all
R = 2;
info = create_problem('Size', [150 100 50], 'Num_Factors', R, ...
    'M', 0.95, 'Sparse_M', true, 'Noise', 0.00);
X = info.Data;
P = info.Pattern;
M_true= info.Soln;
M_init = create_guess('Data', X, 'Num_Factors', R, ...
    'Factor_Generator', 'nvecs');
[M,~,output] = cp_wopt(X, P, R, 'init', M_init);
exitmsg = output.ExitMsg
scr = score(M,M_true)

The code is very clear. What needs to be explained is that create_problemthe created info object info.Solnis complete and not polluted by noise. We call it the exact solution and store it in the tensor form above. info.Patternis a tensor of logical subscripts indicating which positions are not missing. info.DataIt is the data after missing and noise, and it is stored in the form of general tensor. Num_FactorsIndicates the superscript of the CP decomposition summation symbol, and also the number of factors. create_guessA first guess is given for the iterative method, Mand the Kruskal scheme (CP) of the completed matrix is ​​given. scoregives an error representation of the completed matrix and the original matrix, which does not only consider the missing parts.

It can be seen from the results of the operation that the original missing position completion effect may not be very good. But it seems that higher-dimensional tensor completion works better.

Guess you like

Origin blog.csdn.net/lusongno1/article/details/126152334
Recommended