Home > tt2 > core > tt_als.m

tt_als

PURPOSE ^

Several ALS sweeps for the approximation in the TT-format

SYNOPSIS ^

function [res] = tt_als(x,y,nswp)

DESCRIPTION ^

Several ALS sweeps for the approximation in the TT-format
   [RES]=TT_ALS(X,Y,NSWP) Compute NSWP sweeps of the ALS method for the
   approximation of a tensor X, starting from a tensor Y. The output RES is
   the improved approximation of X


 TT-Toolbox 2.2, 2009-2012

This is TT Toolbox, written by Ivan Oseledets et al.
Institute of Numerical Mathematics, Moscow, Russia
webpage: http://spring.inm.ras.ru/osel

For all questions, bugs and suggestions please mail
ivan.oseledets@gmail.com
---------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [res] = tt_als(x,y,nswp)
0002 %Several ALS sweeps for the approximation in the TT-format
0003 %   [RES]=TT_ALS(X,Y,NSWP) Compute NSWP sweeps of the ALS method for the
0004 %   approximation of a tensor X, starting from a tensor Y. The output RES is
0005 %   the improved approximation of X
0006 %
0007 %
0008 % TT-Toolbox 2.2, 2009-2012
0009 %
0010 %This is TT Toolbox, written by Ivan Oseledets et al.
0011 %Institute of Numerical Mathematics, Moscow, Russia
0012 %webpage: http://spring.inm.ras.ru/osel
0013 %
0014 %For all questions, bugs and suggestions please mail
0015 %ivan.oseledets@gmail.com
0016 %---------------------------
0017 
0018 %Make old format out of the new one
0019 x=core(x); y=core(y); 
0020 d=size(x,1);
0021 %The first step is to compute left-to-right sweep of summation, yielding
0022 %phi-matrices
0023 phi=cell(d,1); %To store phi matrices
0024 
0025 [q,rv]=qr(y{1},0);
0026 y{1}=q;
0027 %X(i1,a)*Y(i1,b)
0028 phi{1}=y{1}'*x{1}; %will be ry x rx
0029 for i=2:d-1
0030     %fprintf('i=%d/%d \n',i,d-1);
0031     y{i} = ten_conv(y{i},2,rv');
0032     ncur=size(y{i},1);
0033     r2=size(y{i},2);
0034     r3=size(y{i},3);
0035     core1=permute(y{i},[1,2,3]);
0036     core1=reshape(core1,[ncur*r2,r3]);
0037     [y{i},rv]=qr(core1,0);
0038     rnew=min(r3,ncur*r2);
0039     y{i}=reshape(y{i},[ncur,r2,rnew]);
0040     y{i}=permute(y{i},[1,2,3]);
0041    n=size(x{i},1); rx1=size(x{i},2);  rx2=size(x{i},3);
0042    ry1=size(y{i},2); ry2=size(y{i},3);
0043    %phi x is ry1 x rx1 core_x is n x rx1 x rx2
0044    phi{i}=phi{i-1}*reshape(permute(x{i}, [2,1,3]),[rx1,n*rx2]);
0045    %phi is now ry1 x n x rx2, core_y is n x ry1 x ry2
0046    phi{i}=permute(reshape(phi{i},[ry1,n,rx2]),[2,1,3]); % n x ry1 x rx2
0047    phi{i}=reshape(y{i},[n*ry1,ry2])'*reshape(phi{i},[n*ry1,rx2]);
0048 end
0049 %Now we are able to recompute Y_d
0050 %mat_x{d} is X(i_d,rx) and phi is  ry x rx
0051 for s=1:nswp
0052 
0053 y{d}= x{d}*phi{d-1}';
0054 
0055 %Now we can start right-to-left sweep that combines computation of
0056 %phi matrices with Y right-to-left orthogonalization
0057 [q,rv]=qr(y{d},0);
0058 y{d}=q;
0059 %mat_x{d} is n x rx, mat_y{d} is n x ry phi will be ry x rx
0060 phi{d}=y{d}'*x{d};
0061 
0062 
0063 
0064 for i=d-1:-1:2
0065     %fprintf('i=%d/%d \n',i,d-1);
0066    %First solve for y{i} then orthogonalize it
0067    %Since the left phi matrix for y dissappears we have to
0068    %convolve the current core X(a1,i1,a2) with phi(i-1) from the left
0069    %and with phi(i) from the right
0070    %y{i}=ten_conv(x{i},3,phi{i+1}'); %is n x rx1 x ry2
0071    phi0=ten_conv(x{i},3,phi{i+1}');
0072    %printf('true \n'); y{i}
0073    %return
0074    
0075    y{i}=ten_conv(phi0,2,phi{i-1}'); %is n x ry1 x ry2
0076    %Orthogonalize y
0077     ncur=size(y{i},1);
0078     r2=size(y{i},2);
0079     r3=size(y{i},3);
0080     core1=permute(y{i},[1,3,2]);
0081     core1=reshape(core1,[ncur*r3,r2]);
0082     [y{i},rv]=qr(core1,0);
0083     rnew=min(r2,ncur*r3);
0084     y{i}=reshape(y{i},[ncur,r3,rnew]);
0085     y{i}=permute(y{i},[1,3,2]);
0086     
0087     %Compute new phi{i}
0088     ncur=size(x{i},1); rx1=size(x{i},2);
0089     ry1=size(y{i},2); ry2=size(phi{i+1},1);
0090     
0091     %X is ncur x rx1 x rx2, phi is ry2 x rx2
0092   % keyboard;
0093     %phi0=reshape(x{i},[ncur*rx1,rx2])*phi{i+1}';
0094     %phi0=reshape(x{i},[ncur*rx1,rx2])*phi{i+1}';
0095     
0096     %phi0 is ncur x rx1 x ry2, y{i} is ncur x ry1 x ry2
0097     phi0=permute(reshape(phi0,[ncur,rx1,ry2]),[1,3,2]); 
0098     phi{i}=reshape(permute(y{i},[1,3,2]),[ncur*ry2,ry1])'*reshape(phi0,[ncur*ry2,rx1]); 
0099     %fprintf('true phi{%i} norm: %e \n',i,norm(phi{i}(:)));
0100 
0101 end
0102 y{1} = x{1}*phi{2}';
0103 %Now (finally!) we have this left-to-right sweep
0104 [q,rv]=qr(y{1},0);
0105 y{1}=q;
0106 %X(i1,a)*Y(i1,b)
0107 phi{1}=y{1}'*x{1}; %will be ry x rx
0108 %phi{1}
0109 for i=2:d-1
0110     %fprintf('i=%d/%d \n',i,d-1);
0111    y{i}=ten_conv(x{i},3,phi{i+1}');
0112    y{i}=ten_conv(y{i},2,phi{i-1}');
0113    %norm(y{i}(:))
0114    %return
0115    %Orthogonalize new core
0116    ncur=size(y{i},1);
0117    r2=size(y{i},2);
0118    r3=size(y{i},3);
0119    core1=permute(y{i},[1,2,3]);
0120    core1=reshape(core1,[ncur*r2,r3]);
0121    [y{i},rv]=qr(core1,0);
0122    rnew=min(r3,ncur*r2);
0123    y{i}=reshape(y{i},[ncur,r2,rnew]);
0124    y{i}=permute(y{i},[1,2,3]);
0125    %Compute new phi
0126    n=size(x{i},1); rx1=size(x{i},2);  rx2=size(x{i},3);
0127    ry1=size(y{i},2); ry2=size(y{i},3);
0128    %phi x is ry1 x rx1 core_x is n x rx1 x rx2
0129    phi{i}=phi{i-1}*reshape(permute(x{i}, [2,1,3]),[rx1,n*rx2]);
0130    %phi is now ry1 x n x rx2, core_y is n x ry1 x ry2
0131    phi{i}=permute(reshape(phi{i},[ry1,n,rx2]),[2,1,3]); % n x ry1 x rx2
0132    phi{i}=reshape(y{i},[n*ry1,ry2])'*reshape(phi{i},[n*ry1,rx2]);
0133 end
0134 %Now we are able to recompute Y_d
0135 %mat_x{d} is X(i_d,rx) and phi is  ry x rx
0136 end
0137 y{d}= x{d}*phi{d-1}';
0138 res=y;
0139 res=tt_tensor(res);
0140 return
0141 end

Generated on Wed 08-Feb-2012 18:20:24 by m2html © 2005