# 张量分解 **Repository Path**: slandarer/tensor-decomposition ## Basic Information - **Project Name**: 张量分解 - **Description**: TUCKER积相关张量分解的实例数据与代码 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 11 - **Forks**: 6 - **Created**: 2022-05-16 - **Last Updated**: 2025-09-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: 张量分解, CP分解, HO-SVD, O-SVD ## README # 张量分解 @author: slandarer ### 目录 + 1 [绪论](https://mp.weixin.qq.com/s/UHX616VOabbY7dbR1T0vrA) + 2 张量分解相关运算 + 2.1 [张量的基本概念与表示形式](https://mp.weixin.qq.com/s/UHX616VOabbY7dbR1T0vrA) + 2.2 [Kronecker积、Khatri-Rao积、Hadamard积与广义逆](https://mp.weixin.qq.com/s/Ls32hwHm1LU70tYnPN7OIA) + 2.3 [张量的展开与Tucker积](https://mp.weixin.qq.com/s/N4L6Bl627NlGGs6HB0qeww) + 2.4 [张量的依有序划分矩阵化](https://mp.weixin.qq.com/s/Ra3zAOTZ7lRa6CVCoqM8ww) + 2.4 [外积与秩一张量、内积与张量范数](https://mp.weixin.qq.com/s/2O-a5YT7soXlsdQsE2bcAA) + **附加:** [数据说明](https://mp.weixin.qq.com/s/2IDRcMDlEEYABW-qII1ksg) + 3 张量分解的基本方法 + 3.1 [CP(CANDECOMP/PARAFAC)分解及其ALS方法](https://mp.weixin.qq.com/s/q5kKODzeCX_kk4YlrUOIpQ) + 3.2 [Tucker分解形式HO-SVD方法及其截断](https://mp.weixin.qq.com/s/0BT49LV21hfz4Zn8c5rGwA) + 3.2.1 **附加:** [CP分解为特殊Tucker分解的原因](https://mp.weixin.qq.com/s/4rJLrQDn8-C6P8zb0a0bzQ) + 3.3 [O-SVD及其截断](https://mp.weixin.qq.com/s/qaRcGv_JzBwlQy42s2bU0g) + [参考文献](https://gitee.com/slandarer/tensor-decomposition/tree/master/%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE) ### 张量切片及纤维 ![输入图片说明](gallery/%E7%BB%93%E6%9E%841-%E5%BC%A0%E9%87%8F%E5%88%87%E7%89%87.png) ![输入图片说明](gallery/%E7%BB%93%E6%9E%842-%E5%BC%A0%E9%87%8F%E7%BA%A4%E7%BB%B4.png) ### 单位张量与超对角单位张量 ![输入图片说明](gallery/%E7%89%B9%E6%AE%8A%E5%BC%A0%E9%87%8F.jpg) **单位张量生成函数:**[teye.m](TUCKER_opt/teye.m) **超对角张量生成函数:**[sdiag.m](TUCKER_opt/sdiag.m) **使用实例:** ```matlab % 超对角线为[1,2,3]的超对角张量 A=sdiag([1,2,3],3) % 3x3x2大小的单位张量 B=teye([3,3,2]) ``` A(:,:,1) = 1 0 0 0 0 0 0 0 0 A(:,:,2) = 0 0 0 0 2 0 0 0 0 A(:,:,3) = 0 0 0 0 0 0 0 0 3 B(:,:,1) = 1 0 0 0 1 0 0 0 1 B(:,:,2) = 0 0 0 0 0 0 0 0 0 ### 模-n展开及模-n积 ![输入图片说明](gallery/%E6%A8%A1-n%E5%B1%95%E5%BC%80%E5%AE%9A%E4%B9%891.png) ![输入图片说明](gallery/%E6%A8%A1-n%E5%B1%95%E5%BC%80%E5%AE%9A%E4%B9%892.png) **张量模-n展开代码:**[modeN_unfold](TUCKER_opt/modeN_unfold.m) **使用实例:** ```matlab X=zeros(3,4,2); X(1:24)=1:24; X1=modeN_unfold(X,1) X2=modeN_unfold(X,2) X3=modeN_unfold(X,3) ``` X1 = 1 4 7 10 13 16 19 22 2 5 8 11 14 17 20 23 3 6 9 12 15 18 21 24 X2 = 1 2 3 13 14 15 4 5 6 16 17 18 7 8 9 19 20 21 10 11 12 22 23 24 X3 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ![输入图片说明](gallery/%E6%A8%A1-n%E7%A7%AF.png) **张量模-n展积代码:**[modeN_prod](TUCKER_opt/modeN_prod.m) **使用实例:** ```matlab X=zeros(3,4,2); X(1:24)=1:24; U1=[1,3,5;2,4,6]; U2=[1,0;0,1;1,1]; Y1=modeN_prod(X,U1,1) Y2=modeN_prod(X,U2,3) ``` Y1(:,:,1) = 22 49 76 103 28 64 100 136 Y1(:,:,2) = 130 157 184 211 172 208 244 280 Y2(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 Y2(:,:,2) = 13 16 19 22 14 17 20 23 15 18 21 24 Y2(:,:,3) = 14 20 26 32 16 22 28 34 18 24 30 36 ### 所用特殊张量mat文件 [超对角线数据抹去张量](Goldhill.mat)及[正面切片随机旋转张量](Peppers.mat) ![输入图片说明](gallery/%E7%89%B9%E6%AE%8A%E5%BC%A0%E9%87%8F2.jpg) 实验对象为 DIPUM2E 教材中 256×256 大小的原图([Goldhill](Goldhill.bmp) 及 [peppers](Peppers.bmp)),并对 其添加不同扰动后拼接而成 256×256×256 大小的张量,其中 Goldhill 图像将 图像不同位置增添 50×50 大小的亮度为 0 的矩形区域并拼接而成,而 peppers 则是将图像进行不同程度的旋转后拼接成三维张量的形式。 ![输入图片说明](gallery/oripic.png) ### CP分解 ![输入图片说明](gallery/CP%E5%88%86%E8%A7%A31.png) ![输入图片说明](gallery/CP%E5%88%86%E8%A7%A32.png) **张量CP分解三维代码:**[CP3_ALS](TUCKER_opt/CP3_ALS.m) ![输入图片说明](gallery/CP%E5%88%86%E8%A7%A3%E6%B5%81%E7%A8%8B.png) **使用实例:** ```matlab % 读取数据 file=load('Peppers.mat'); X=file.XData; % 截断值60,精确度1e-3CP分解 [A,B,C,lambda]=CP3_ALS(X,60,1e-3); % 使用模-n积还原拟合张量 % sdiag(lambda)x_1A x_2B x_3C apprX=modeN_prod(modeN_prod(modeN_prod(sdiag(lambda),A,1),B,2),C,3); % 展示拟合结果 imshow(uint8(apprX(:,:,1))) % 展示与原图像差值 figure() imagesc(abs(X(:,:,1)-apprX(:,:,1))) colormap(PYCM().inferno())% 使用python colormap渲染,可删去 colorbar ``` ![输入图片说明](result/demo2_CP_compare.png) ![输入图片说明](result/demo2d_CP.png) **不同截断值分解误差:** ```matlab function demo2_moreT_CP % 读取数据 file=load('Peppers.mat'); X=file.XData; for i=60:10:500 % 截断值60,精确度1e-3CP分解 [A,B,C,lambda]=CP3_ALS(X,i,1e-3); % 使用模-n积还原拟合张量 % sdiag(lambda)x_1A x_2B x_3C apprX=modeN_prod(modeN_prod(modeN_prod(sdiag(lambda),A,1),B,2),C,3); diffX=abs(X-apprX); % 将误差值存入数组 errRMSE(i)=sqrt(sum(diffX.^2,'all')./numel(diffX)); errMEAN(i)=mean(diffX,'all'); errMAX(i)=max(diffX,[],'all'); disp(['the [',num2str(i),']-term fit is completed']) end save demo2_CP_err.mat errRMSE errMEAN errMAX errSet=load('demo2_CP_err.mat'); t=60:10:500; colororder([82,124,179;170,64,64]./255) % 双y轴——左侧图 yyaxis left hold on plot(t,errSet.errMEAN(t),'o-','Color',[82,124,179]./255,'MarkerFaceColor',[82,124,179]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) plot(t,errSet.errRMSE(t),'^-','Color',[62,85,96]./255,'MarkerFaceColor',[62,85,96]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) ax=gca; ax.YMinorTick='on'; % 双y轴——右侧图 yyaxis right plot(t,errSet.errMAX(t),'s-','Color',[170,64,64]./255,'MarkerFaceColor',[170,64,64]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) % 图例 legend('MEAN error','RMSE error','MAX error') % 坐标区域修饰 ax=gca;grid on;box off ax.LineWidth=1.5; ax.GridLineStyle='-.'; ax.GridColor=[1,1,1].*.2; ax.Color=[249,250,245]./255; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.FontName='Cambria'; ax.FontSize=13; end ``` ![输入图片说明](result/demo2err_CP.png) ### HO-SVD及其截断 ![输入图片说明](gallery/HO-SVD.png) ![输入图片说明](gallery/HO-SVD%E6%88%AA%E6%96%AD.png) **张量HO-SVD N维代码:**[HO-SVD](TUCKER_opt/HO_SVD.m) ![输入图片说明](gallery/HO-SVD%E6%B5%81%E7%A8%8B.png) **使用实例:** ```matlab % 读取数据 file=load('Peppers.mat'); X=file.XData; % 截断值60,HO-SVD分解 [G,U]=HO_SVD(X,60); % 使用模-n积还原拟合张量 % Gx_1U_1 x_2U_2 x_3U_3 apprX=modeN_prod(modeN_prod(modeN_prod(G,U{1},1),U{2},2),U{3},3); % 展示拟合结果 imshow(uint8(apprX(:,:,1))) % 展示与原图像差值 figure() imagesc(abs(X(:,:,1)-apprX(:,:,1))) colormap(PYCM().inferno())% 使用python colormap渲染,可删去 colorbar ``` ![输入图片说明](result/demo2_HOSVD_compare.png) ![输入图片说明](result/demo2d_HOSVD.png) **不同截断值分解误差:** ```matlab function demo2_moreT_HOSVD % 读取数据 file=load('Peppers.mat'); X=file.XData; for i=60:10:250 % 截断值i,HO-SVD分解 [G,U]=HO_SVD(X,i); % 使用模-n积还原拟合张量 % Gx_1U_1 x_2U_2 x_3U_3 apprX=modeN_prod(modeN_prod(modeN_prod(G,U{1},1),U{2},2),U{3},3); diffX=abs(X-apprX); % 将误差值存入数组 errRMSE(i)=sqrt(sum(diffX.^2,'all')./numel(diffX)); errMEAN(i)=mean(diffX,'all'); errMAX(i)=max(diffX,[],'all'); disp(['the [',num2str(i),']-term fit is completed']) end save demo2_HOSVD_err.mat errRMSE errMEAN errMAX errSet=load('demo2_HOSVD_err.mat'); t=60:10:250; colororder([82,124,179;170,64,64]./255) % 双y轴——左侧图 yyaxis left hold on plot(t,errSet.errMEAN(t),'o-','Color',[82,124,179]./255,'MarkerFaceColor',[82,124,179]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) plot(t,errSet.errRMSE(t),'^-','Color',[62,85,96]./255,'MarkerFaceColor',[62,85,96]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) ax=gca; ax.YMinorTick='on'; % 双y轴——右侧图 yyaxis right plot(t,errSet.errMAX(t),'s-','Color',[170,64,64]./255,'MarkerFaceColor',[170,64,64]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) % 图例 legend('MEAN error','RMSE error','MAX error') % 坐标区域修饰 ax=gca;grid on;box off ax.LineWidth=1.5; ax.GridLineStyle='-.'; ax.GridColor=[1,1,1].*.2; ax.Color=[249,250,245]./255; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.FontName='Cambria'; ax.FontSize=13; end ``` ![输入图片说明](result/demo2err_HOSVD.png) ### O-SVD及其截断 ![输入图片说明](gallery/O-SVD.png) ![输入图片说明](gallery/O-SVD%E6%88%AA%E6%96%AD.png) **张量O-SVD 代码:**[O-SVD](TUCKER_opt/O_SVD.m) ![输入图片说明](gallery/O-SVD%E6%B5%81%E7%A8%8B.png) **使用实例:** ```matlab % 读取数据 file=load('Peppers.mat'); X=file.XData; % 截断值60,O-SVD分解 [U,S,V,F]=O_SVD(X,60); % 使用模-n积及切片积还原拟合张量 % U *_3S *_3V x_3F apprX=modeN_prod(slice_prod(slice_prod(U,S,3),V,3),F,3); % 展示拟合结果 imshow(uint8(apprX(:,:,1))) % 展示与原图像差值 figure() imagesc(abs(X(:,:,1)-apprX(:,:,1))) colormap(PYCM().inferno())% 使用python colormap渲染,可删去 colorbar ``` ![输入图片说明](result/demo2_OSVD_compare.png) ![输入图片说明](result/demo2d_OSVD.png) **不同截断值分解误差:** ```matlab function demo2_moreT_OSVD % 读取数据 file=load('Peppers.mat'); X=file.XData; for i=60:10:250 % 截断值i,O-SVD分解 [U,S,V,F]=O_SVD(X,i); % 使用模-n积及切片积还原拟合张量 % U *_3S *_3V x_3F apprX=modeN_prod(slice_prod(slice_prod(U,S,3),V,3),F,3); diffX=abs(X-apprX); % 将误差值存入数组 errRMSE(i)=sqrt(sum(diffX.^2,'all')./numel(diffX)); errMEAN(i)=mean(diffX,'all'); errMAX(i)=max(diffX,[],'all'); disp(['the [',num2str(i),']-term fit is completed']) end save demo2_OSVD_err.mat errRMSE errMEAN errMAX errSet=load('demo2_OSVD_err.mat'); t=60:10:250; colororder([82,124,179;170,64,64]./255) % 双y轴——左侧图 yyaxis left hold on plot(t,errSet.errMEAN(t),'o-','Color',[82,124,179]./255,'MarkerFaceColor',[82,124,179]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) plot(t,errSet.errRMSE(t),'^-','Color',[62,85,96]./255,'MarkerFaceColor',[62,85,96]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) ax=gca; ax.YMinorTick='on'; % 双y轴——右侧图 yyaxis right plot(t,errSet.errMAX(t),'s-','Color',[170,64,64]./255,'MarkerFaceColor',[170,64,64]./255,... 'MarkerEdgeColor',[1,1,1],'LineWidth',2,'MarkerSize',12) % 图例 legend('MEAN error','RMSE error','MAX error') % 坐标区域修饰 ax=gca;grid on;box off ax.LineWidth=1.5; ax.GridLineStyle='-.'; ax.GridColor=[1,1,1].*.2; ax.Color=[249,250,245]./255; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.FontName='Cambria'; ax.FontSize=13; end ``` ![输入图片说明](result/demo2err_OSVD.png) ### 复杂度 ![输入图片说明](gallery/%E5%A4%8D%E6%9D%82%E5%BA%A6.png) ### [参考文献](https://gitee.com/slandarer/tensor-decomposition/tree/master/%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE) - [1]T. G. Kolda, B. W. Bader. Tensor decompositions and applications. SIAM review, 2009, 51(3): 455--500. - [2]Misha E. Kilmer, Karen Braman, Ning Hao, Randy C. Hoover. “Third Order Tensors as Operators on Matrices: A Theoretical and Computational Framework with Applications in Imaging,”.SIAM J on Matrix Analysis and Applications, 2013, 34(1): 148-172. - [3]TENSOR-TRAIN DECOMPOSITION Society for Industrial and Applied Mathematics . SIAM J. SCI. COMPUT, 2011, 33(5): 2295–2317. - [4]L. R. Tucker. Some mathematical notes on three-mode factor analysis. Psychometrika, 1966, 31: 279–311. - [5]T. G. Kolda. Multilinear operators for higher-order decompositions. Tech. Report SAND. Sandia National Laboratories, 2006: 2006--2081. - [6]J. D. Carroll, J. J. Chang. Analysis of individual differences in multidimensional scaling via an N-way generalization of ‘Eckart-Young’decomposition. Psychometrika, 1970, 35: 283–319. - [7]R. A. Harshman. Foundations of the PARAFAC procedure: models and conditions for an “explanatory” multi-modal factor analysis, UCLA working papers in phonetics, 1970, 16: 1–84. - [8]C. F. Van Loan. The ubiquitous Kronecker product. J. Comput. Appl. Math, 2000, 123: 85–100. - [9]A. Smilde, R. Bro, P. Geladi. Multi-way analysis: applications in the chemical sciences. Wiley, 2004. - [10] L. De Lathauwer, B. De Moor, J. Vandewalle. A multilinear singular value decomposition. SIAM J. Matrix Anal. A, 2000, 21: 1253–1278. - [11] A. Smilde, R. Bro, P. Geladi. Multi-Way Analysis: Applications in the Chemical Sciences. Wiley. West Sussex. England, 2004. - [12] H. A. L. Kiers. Towards a standardized notation and terminology in multiway analysis. J. Chemometrics, 2000, 14: 105–122. - [13] C. Battaglino, G. Ballard, T. G. Kolda. A Practical Randomized CP Tensor Decomposition. SIAM J. Matrix Analysis and Applications,2018, 39(2): 876-901 - [14] E. C. Chi, T. G. Kolda. On Tensors, Sparsity, and Nonnegative Factorizations. SIAM J. Matrix Analysis, 2012, 33(4): 1272-1299. - [15] S. Hansen, T. Plantenga, T. G. Kolda. Newton-Based Optimization for Kullback-Leibler Nonnegative Tensor Factorizations. Optimization Methods and Software, 2015. - [16] S. Sherman, T. G. Kolda. Estimating Higher-Order Moments Using Symmetric Tensor Decomposition. SIAM J. Matrix Analysis and Applications, 2020, 41 : 1369-1387. - [17] L. De Lathauwer, B. De Moor, J. Vandewalle. A multilinear singular value decomposition. SIAM J. Matrix Anal. Appl, 2000, 21: 1253–1278. - [18] P. M. Kroonenberg, J. De Leeuw. Principal component analysis of three-mode data by means of alternating least squares algorithms. Psychometrika,1980, 45: 69–97. - [19] L. De Lathauwer, B. De Moor, J. Vandewalle. On the best rank-1 and rank-(R1, R2,...,RN ) approximation of higher-order tensors. SIAM J. Matrix Anal. Appl, 2000, 21: 1324–1342. - [20] C. Zeng and M. K. Ng, Decompositions of third-order tensors: HOSVD, T-SVD, and beyond, Numer. Linear Algebra Appl, 2020, 27. e2290. - [21] R. A. Harshman, PARAFAC2: Mathematical and technical notes, UCLA Working Papers in Phonetics, 1972, 22: 30–47. - [22] Minghui Ding, Pengpeng Xie. A randomized singular value decomposition for third-order oriented tensors. arXiv preprint, 2022, arXiv:2203.02761v1 [math.NA]: 4-5.