1 Star 7 Fork 5

slandarer / 张量分解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

张量分解

@author: slandarer

目录

张量切片及纤维

输入图片说明 输入图片说明

单位张量与超对角单位张量

输入图片说明

单位张量生成函数:teye.m

超对角张量生成函数:sdiag.m

使用实例:

% 超对角线为[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积

输入图片说明 输入图片说明

张量模-n展开代码:modeN_unfold

使用实例:

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

输入图片说明 张量模-n展积代码:modeN_prod

使用实例:

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文件

超对角线数据抹去张量正面切片随机旋转张量 输入图片说明 实验对象为 DIPUM2E 教材中 256×256 大小的原图(Goldhillpeppers),并对 其添加不同扰动后拼接而成 256×256×256 大小的张量,其中 Goldhill 图像将 图像不同位置增添 50×50 大小的亮度为 0 的矩形区域并拼接而成,而 peppers 则是将图像进行不同程度的旋转后拼接成三维张量的形式。 输入图片说明

CP分解

输入图片说明 输入图片说明 张量CP分解三维代码:CP3_ALS 输入图片说明 使用实例:

% 读取数据
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

输入图片说明 输入图片说明 不同截断值分解误差:

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

输入图片说明

HO-SVD及其截断

输入图片说明 输入图片说明 张量HO-SVD N维代码:HO-SVD 输入图片说明 使用实例:

% 读取数据
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

输入图片说明 输入图片说明 不同截断值分解误差:

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

输入图片说明

O-SVD及其截断

输入图片说明 输入图片说明 张量O-SVD 代码:O-SVD 输入图片说明 使用实例:

% 读取数据
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

输入图片说明 输入图片说明 不同截断值分解误差:

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

输入图片说明

复杂度

输入图片说明

参考文献

  • [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.

空文件

简介

TUCKER积相关张量分解的实例数据与代码 展开 收起
MATLAB 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/slandarer/tensor-decomposition.git
git@gitee.com:slandarer/tensor-decomposition.git
slandarer
tensor-decomposition
张量分解
master

搜索帮助