⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m1_multiply_doc.html

📁 张量分析工具
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</pre><p>Sometimes the product may be too large to reside in memory.  For example, try the following: X = sptenrand([100 100 100 100],            1e4); A = rand(1000,100); ttm(X,A,1);  %&lt;-- too large for memory         </p>         <h2>Kruskal tensor times matrix (ttm for ktensor)<a name="41"></a></h2>         <p>The special structure of a ktensor allows an efficient implementation of matrix multiplication. The arguments are the same            as for the dense case.         </p><pre class="codeinput">X = ktensor({rand(5,1) rand(3,1) rand(4,1) rand(2,1)});</pre><pre class="codeinput">Y = ttm(X, A, 1);         <span class="comment">%&lt;-- X times A in mode-1.</span>Y = ttm(X, {A,B,C,D}, 1); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, A', 1, <span class="string">'t'</span>)    <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 x 3 x 4 x 2	Y.lambda = [ 1 ]	Y.U{1} = 		    0.7712		    2.1158		    2.0046		    1.8224	Y.U{2} = 		    0.3790		    0.3373		    0.3146	Y.U{3} = 		    0.9471		    0.5352		    0.7020		    0.8750	Y.U{4} = 		    0.9862		    0.8853</pre><pre class="codeinput">Y = ttm(X, {A,B,C,D}, [1 2 3 4]); <span class="comment">%&lt;-- 4-way mutliply.</span>Y = ttm(X, {D,C,B,A}, [4 3 2 1]); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,C,D});            <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A',B',C',D'}, <span class="string">'t'</span>)    <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 x 4 x 3 x 3	Y.lambda = [ 1 ]	Y.U{1} = 		    0.7712		    2.1158		    2.0046		    1.8224	Y.U{2} = 		    0.3761		    0.7098		    0.6619		    0.3805	Y.U{3} = 		    1.4859		    1.4836		    1.0471	Y.U{4} = 		    1.2018		    0.7014		    0.6494</pre><pre class="codeinput">Y = ttm(X, {C,D}, [3 4]);    <span class="comment">%&lt;-- X times C in mode-3 &amp; D in mode-4.</span>Y = ttm(X, {A,B,C,D}, [3 4]) <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ktensor of size 5 x 3 x 3 x 3	Y.lambda = [ 1 ]	Y.U{1} = 		    0.5430		    0.9035		    0.8613		    0.6315		    0.2624	Y.U{2} = 		    0.3790		    0.3373		    0.3146	Y.U{3} = 		    1.4859		    1.4836		    1.0471	Y.U{4} = 		    1.2018		    0.7014		    0.6494</pre><pre class="codeinput">Y = ttm(X, {A,B,D}, [1 2 4]);   <span class="comment">%&lt;-- 3-way multiply.</span>Y = ttm(X, {A,B,C,D}, [1 2 4]); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,D}, -3);        <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,C,D}, -3)       <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 x 4 x 4 x 3	Y.lambda = [ 1 ]	Y.U{1} = 		    0.7712		    2.1158		    2.0046		    1.8224	Y.U{2} = 		    0.3761		    0.7098		    0.6619		    0.3805	Y.U{3} = 		    0.9471		    0.5352		    0.7020		    0.8750	Y.U{4} = 		    1.2018		    0.7014		    0.6494</pre><h2>Tucker tensor times matrix (ttm for ttensor)<a name="46"></a></h2>         <p>The special structure of a ttensor allows an efficient implementation of matrix multiplication.</p><pre class="codeinput">X = ttensor(tensor(rand(2,2,2,2)),{rand(5,2) rand(3,2) rand(4,2) rand(2,2)});</pre><pre class="codeinput">Y = ttm(X, A, 1);         <span class="comment">%&lt;-- computes X times A in mode-1.</span>Y = ttm(X, {A,B,C,D}, 1); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, A', 1, <span class="string">'t'</span>)    <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ttensor of size 4 x 3 x 4 x 2	Y.core is a tensor of size 2 x 2 x 2 x 2		Y.core(:,:,1,1) = 	    0.4048    0.3855	    0.6271    0.8479		Y.core(:,:,2,1) = 	    0.5268    0.3935	    0.8074    0.9617		Y.core(:,:,1,2) = 	    0.0301    0.7144	    0.9537    0.6465		Y.core(:,:,2,2) = 	    0.4467    0.8352	    0.1746    0.9701	Y.U{1} = 		    0.7287    1.1203		    1.8017    1.4937		    1.3952    1.1431		    1.5770    1.4536	Y.U{2} = 		    0.0144    0.7729		    0.4858    0.4881		    0.4164    0.5226	Y.U{3} = 		    0.7820    0.6629		    0.5912    0.9971		    0.1264    0.3462		    0.1097    0.1761	Y.U{4} = 		    0.0679    0.3348		    0.3094    0.3762</pre><pre class="codeinput">Y = ttm(X, {A,B,C,D}, [1 2 3 4]); <span class="comment">%&lt;-- 4-way multiply.</span>Y = ttm(X, {D,C,B,A}, [4 3 2 1]); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,C,D});            <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A',B',C',D'}, <span class="string">'t'</span>)    <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ttensor of size 4 x 4 x 3 x 3	Y.core is a tensor of size 2 x 2 x 2 x 2		Y.core(:,:,1,1) = 	    0.4048    0.3855	    0.6271    0.8479		Y.core(:,:,2,1) = 	    0.5268    0.3935	    0.8074    0.9617		Y.core(:,:,1,2) = 	    0.0301    0.7144	    0.9537    0.6465		Y.core(:,:,2,2) = 	    0.4467    0.8352	    0.1746    0.9701	Y.U{1} = 		    0.7287    1.1203		    1.8017    1.4937		    1.3952    1.1431		    1.5770    1.4536	Y.U{2} = 		    0.4505    0.6010		    0.5530    1.2351		    0.5263    1.1635		    0.2970    0.6779	Y.U{3} = 		    0.6823    0.9247		    0.9196    1.1786		    0.2928    0.5203	Y.U{4} = 		    0.1592    0.4312		    0.1505    0.2692		    0.1157    0.2421</pre><pre class="codeinput">Y = ttm(X, {C,D}, [3 4]);    <span class="comment">%&lt;-- X times C in mode-3 &amp; D in mode-4</span>Y = ttm(X, {A,B,C,D}, [3 4]) <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ttensor of size 5 x 3 x 3 x 3	Y.core is a tensor of size 2 x 2 x 2 x 2		Y.core(:,:,1,1) = 	    0.4048    0.3855	    0.6271    0.8479		Y.core(:,:,2,1) = 	    0.5268    0.3935	    0.8074    0.9617		Y.core(:,:,1,2) = 	    0.0301    0.7144	    0.9537    0.6465		Y.core(:,:,2,2) = 	    0.4467    0.8352	    0.1746    0.9701	Y.U{1} = 		    0.1350    0.5122		    0.2510    0.0035		    0.9100    0.2269		    0.6765    0.9785		    0.6232    0.8613	Y.U{2} = 		    0.0144    0.7729		    0.4858    0.4881		    0.4164    0.5226	Y.U{3} = 		    0.6823    0.9247		    0.9196    1.1786		    0.2928    0.5203	Y.U{4} = 		    0.1592    0.4312		    0.1505    0.2692		    0.1157    0.2421</pre><pre class="codeinput">Y = ttm(X, {A,B,D}, [1 2 4]);   <span class="comment">%&lt;-- 3-way multiply</span>Y = ttm(X, {A,B,C,D}, [1 2 4]); <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,D}, -3);        <span class="comment">%&lt;-- Same as above.</span>Y = ttm(X, {A,B,C,D}, -3)       <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a ttensor of size 4 x 4 x 4 x 3	Y.core is a tensor of size 2 x 2 x 2 x 2		Y.core(:,:,1,1) = 	    0.4048    0.3855	    0.6271    0.8479		Y.core(:,:,2,1) = 	    0.5268    0.3935	    0.8074    0.9617		Y.core(:,:,1,2) = 	    0.0301    0.7144	    0.9537    0.6465		Y.core(:,:,2,2) = 	    0.4467    0.8352	    0.1746    0.9701	Y.U{1} = 		    0.7287    1.1203		    1.8017    1.4937		    1.3952    1.1431		    1.5770    1.4536	Y.U{2} = 		    0.4505    0.6010		    0.5530    1.2351		    0.5263    1.1635		    0.2970    0.6779	Y.U{3} = 		    0.7820    0.6629		    0.5912    0.9971		    0.1264    0.3462		    0.1097    0.1761	Y.U{4} = 		    0.1592    0.4312		    0.1505    0.2692		    0.1157    0.2421</pre><h2>Tensor times tensor (ttt for tensor)<a name="51"></a></h2><pre class="codeinput">X = tensor(rand(4,2,3)); Y = tensor(rand(3,4,2));Z = ttt(X,Y); <span class="comment">%&lt;-- Outer product of X and Y.</span>size(Z)</pre><pre class="codeoutput">ans =     4     2     3     3     4     2</pre><pre class="codeinput">Z = ttt(X,X,1:3) <span class="comment">%&lt;-- Inner product of X with itself.</span></pre><pre class="codeoutput">Z =   10.8350</pre><pre class="codeinput">Z = ttt(X,Y,[1 2 3],[2 3 1]) <span class="comment">%&lt;-- Inner product of X &amp; Y.</span></pre><pre class="codeoutput">Z =    9.3217</pre><pre class="codeinput">Z = innerprod(X,Y) <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Z =    8.3864</pre><pre class="codeinput">Z = ttt(X,Y,[1 3],[2 1]) <span class="comment">%&lt;-- Product of X &amp; Y along specified dims.</span></pre><pre class="codeoutput">Z is a tensor of size 2 x 2	Z(:,:) = 	    4.6551    3.8834	    4.4565    4.6666</pre><h2>Sparse tensor times sparse tensor (ttt for sptensor)<a name="56"></a></h2><pre class="codeinput">X = sptenrand([4 2 3],3); Y = sptenrand([3 4 2],3);Z = ttt(X,Y) <span class="comment">%&lt;--Outer product of X and Y.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 4 x 2 x 3 x 3 x 4 x 2 with 9 nonzeros	(1,2,1,1,1,1)    0.0241	(1,2,1,3,2,2)    0.0505	(1,2,1,3,4,2)    0.0700	(2,2,1,1,1,1)    0.1406	(2,2,1,3,2,2)    0.2947	(2,2,1,3,4,2)    0.4084	(4,2,3,1,1,1)    0.1106	(4,2,3,3,2,2)    0.2319	(4,2,3,3,4,2)    0.3214</pre><pre class="codeinput">norm(full(Z)-ttt(full(X),full(Y))) <span class="comment">%&lt;-- Same as dense.</span></pre><pre class="codeoutput">ans =     0</pre><pre class="codeinput">Z = ttt(X,X,1:3) <span class="comment">%&lt;-- Inner product of X with itself.</span></pre><pre class="codeoutput">Z =    0.5856</pre><pre class="codeinput">X = sptenrand([2 3],1); Y = sptenrand([3 2],1);Z = ttt(X, Y) <span class="comment">%&lt;-- Sparse result.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 2 x 3 x 3 x 2 with 1 nonzeros	(2,2,1,2)    0.2167</pre><pre class="codeinput">X = sptenrand([2 3],20); Y = sptenrand([3 2],20);Z = ttt(X, Y) <span class="comment">%&lt;-- Dense result.</span></pre><pre class="codeoutput">Z is a tensor of size 2 x 3 x 3 x 2	Z(:,:,1,1) = 	    0.8652    0.4460    0.3551	    0.8837    0.6871    0.8792	Z(:,:,2,1) = 	    0.1967    0.1014    0.0807	    0.2009    0.1562    0.1998	Z(:,:,3,1) = 	    0.7741    0.3991    0.3177	    0.7907    0.6148    0.7867	Z(:,:,1,2) = 	    0.6209    0.3201    0.2549	    0.6343    0.4931    0.6310	Z(:,:,2,2) = 	    0.0666    0.0344    0.0274	    0.0681    0.0529    0.0677	Z(:,:,3,2) = 	    0.4093    0.2110    0.1680	    0.4181    0.3250    0.4159</pre><pre class="codeinput">Z = ttt(X,Y,[1 2],[2 1]) <span class="comment">%&lt;-- inner product of X &amp; Y</span></pre><pre class="codeoutput">Z =    2.3874</pre><h2>Inner product (innerprod)<a name="62"></a></h2>         <p>The function <tt>innerprod</tt> efficiently computes the inner product between two tensors X and Y.  The code does this efficiently depending on what types            of tensors X and Y.         </p><pre class="codeinput">X = tensor(rand(2,2,2))Y = ktensor({rand(2,2),rand(2,2),rand(2,2)})</pre><pre class="codeoutput">X is a tensor of size 2 x 2 x 2	X(:,:,1) = 	    0.8923    0.4009	    0.9919    0.3407	X(:,:,2) = 	    0.3167    0.6157	    0.3643    0.5975Y is a ktensor of size 2 x 2 x 2	Y.lambda = [ 1  1 ]	Y.U{1} = 		    0.6852    0.2803		    0.9738    0.1740	Y.U{2} = 		    0.9219    0.3751		    0.2602    0.6447	Y.U{3} = 		    0.3165    0.2332		    0.1924    0.2198</pre><pre class="codeinput">z = innerprod(X,Y)</pre><pre class="codeoutput">z =    0.7764</pre><h2>Contraction on tensors (contract for tensor)<a name="64"></a></h2>         <p>The function <tt>contract</tt> sums the entries of X along dimensions I and J.  Contraction is a generalization of matrix trace. In other words, the trace            is performed along the two-dimensional slices defined by dimensions I and J. It is possible to implement tensor multiplication            as an outer product followed by a contraction.         </p><pre class="codeinput">X = sptenrand([4 3 2],5);Y = sptenrand([3 2 4],5);</pre><pre class="codeinput">Z1 = ttt(X,Y,1,3); <span class="comment">%&lt;-- Normal tensor multiplication</span></pre><pre class="codeinput">Z2 = contract(ttt(X,Y),1,6); <span class="comment">%&lt;-- Outer product + contract</span></pre><pre class="codeinput">norm(Z1-Z2) <span class="comment">%&lt;-- Should be zero</span></pre><pre class="codeoutput">ans =     0</pre><p>Using <tt>contract</tt> on either sparse or dense tensors gives the same result         </p><pre class="codeinput">X = sptenrand([4 2 3 4],20);Z1 = contract(X,1,4)        <span class="comment">% sparse version of contract</span></pre><pre class="codeoutput">Z1 is a tensor of size 2 x 3	Z1(:,:) = 	         0         0    0.6799	    0.9183    0.1945    0.5504</pre><pre class="codeinput">Z2 = contract(full(X),1,4)  <span class="comment">% dense version of contract</span></pre><pre class="codeoutput">Z2 is a tensor of size 2 x 3	Z2(:,:) = 	         0         0    0.6799	    0.9183    0.1945    0.5504</pre><pre class="codeinput">norm(full(Z1) - Z2) <span class="comment">%&lt;-- Should be zero</span></pre><pre class="codeoutput">ans =     0</pre><p>The result may be dense or sparse, depending on its density.</p><pre class="codeinput">X = sptenrand([4 2 3 4],8);Y = contract(X,1,4) <span class="comment">%&lt;-- should be sparse</span></pre><pre class="codeoutput">Y is a sparse tensor of size 2 x 3 with 2 nonzeros	(1,2)    0.3194

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -