📄 m1_multiply_doc.html
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Multiplying tensors</title> <meta name="generator" content="MATLAB 7.2"> <meta name="date" content="2007-01-10"> <meta name="m-file" content="M1_multiply_doc"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. On Gecko-based browsers, the shrink-to-fit doesn't work. */ p,h1,h2,div.content div { /* for MATLAB's browser */ width: 600px; /* for Mozilla, but the "width" tag overrides it anyway */ max-width: 600px; /* for IE */ width:expression(document.body.clientWidth > 620 ? "600px": "auto" );}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>Multiplying tensors</h1> <introduction></introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Tensor times vector (ttv for tensor)</a></li> <li><a href="#13">Sparse tensor times vector (ttv for sptensor)</a></li> <li><a href="#18">Kruskal tensor times vector (ttv for ktensor)</a></li> <li><a href="#23">Tucker tensor times vector (ttv for ttensor)</a></li> <li><a href="#28">Tensor times matrix (ttm for tensor)</a></li> <li><a href="#33">Sparse tensor times matrix (ttm for sptensor)</a></li> <li><a href="#41">Kruskal tensor times matrix (ttm for ktensor)</a></li> <li><a href="#46">Tucker tensor times matrix (ttm for ttensor)</a></li> <li><a href="#51">Tensor times tensor (ttt for tensor)</a></li> <li><a href="#56">Sparse tensor times sparse tensor (ttt for sptensor)</a></li> <li><a href="#62">Inner product (innerprod)</a></li> <li><a href="#64">Contraction on tensors (contract for tensor)</a></li> <li><a href="#73">Relationships among ttv, ttm, and ttt</a></li> <li><a href="#81">Frobenius norm of a tensor</a></li> </ul> </div> <h2>Tensor times vector (ttv for tensor)<a name="1"></a></h2> <p>Compute a tensor times a vector (or vectors) in one (or more) modes.</p><pre class="codeinput">rand(<span class="string">'state'</span>,0);X = tenrand([5,3,4,2]); <span class="comment">%<-- Create a dense tensor.</span>A = rand(5,1); B = rand(3,1); C = rand(4,1); D = rand(2,1); <span class="comment">%<-- Some vectors.</span></pre><pre class="codeinput">Y = ttv(X, A, 1) <span class="comment">%<-- X times A in mode 1.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 2 Y(:,:,1) = 1.6875 1.9480 0.9951 0.9505 0.8258 0.9495 1.4104 0.6771 1.4496 0.8295 1.5943 1.6259 Y(:,:,2) = 1.8369 1.3352 1.0743 1.4354 1.0471 1.2250 1.5317 1.2519 1.4225 1.2897 1.1595 0.5775</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, 1) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 2 Y(:,:,1) = 1.6875 1.9480 0.9951 0.9505 0.8258 0.9495 1.4104 0.6771 1.4496 0.8295 1.5943 1.6259 Y(:,:,2) = 1.8369 1.3352 1.0743 1.4354 1.0471 1.2250 1.5317 1.2519 1.4225 1.2897 1.1595 0.5775</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, [1 2 3 4]) <span class="comment">%<-- All-mode multiply produces a scalar.</span></pre><pre class="codeoutput">Y = 7.8707</pre><pre class="codeinput">Y = ttv(X, {D,C,B,A}, [4 3 2 1]) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y = 7.8707</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y = 7.8707</pre><pre class="codeinput">Y = ttv(X, {C,D}, [3 4]) <span class="comment">%<-- X times C in mode-3 & D in mode-4.</span></pre><pre class="codeoutput">Y is a tensor of size 5 x 3 Y(:,:) = 1.0157 1.1081 1.5654 1.3799 1.2137 1.0599 1.5625 1.1830 1.0658 1.2323 1.2410 1.4481 1.4374 0.9573 0.8644</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, [3 4]) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 5 x 3 Y(:,:) = 1.0157 1.1081 1.5654 1.3799 1.2137 1.0599 1.5625 1.1830 1.0658 1.2323 1.2410 1.4481 1.4374 0.9573 0.8644</pre><pre class="codeinput">Y = ttv(X, {A,B,D}, [1 2 4]) <span class="comment">%<-- 3-way multiplication.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 4.9369 4.5013 4.4941 3.8857</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, [1 2 4]) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 4.9369 4.5013 4.4941 3.8857</pre><pre class="codeinput">Y = ttv(X, {A,B,D}, -3) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 4.9369 4.5013 4.4941 3.8857</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, -3) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 4.9369 4.5013 4.4941 3.8857</pre><h2>Sparse tensor times vector (ttv for sptensor)<a name="13"></a></h2> <p>This is the same as in the dense case, except that the result may be either dense or sparse (or a scalar).</p><pre class="codeinput">X = sptenrand([5,3,4,2],5); <span class="comment">%<-- Create a sparse tensor.</span></pre><pre class="codeinput">Y = ttv(X, A, 1) <span class="comment">%<-- X times A in mode 1. Result is sparse.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 3 x 4 x 2 with 5 nonzeros (1,3,1) 0.0014 (2,2,1) 0.3357 (3,1,1) 0.5973 (3,1,2) 0.0005 (3,3,1) 0.0039</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, [1 2 3 4]) <span class="comment">%<-- All-mode multiply.</span></pre><pre class="codeoutput">Y = 0.1196</pre><pre class="codeinput">Y = ttv(X, {C,D}, [3 4]) <span class="comment">%<-- X times C in mode-3 & D in mode-4.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 5 x 3 with 5 nonzeros (2,3) 0.0009 (3,2) 0.0612 (3,3) 0.0959 (4,1) 0.0064 (4,3) 0.0149</pre><pre class="codeinput">Y = ttv(X, {A,B,D}, -3) <span class="comment">%<-- 3-way multiplication. Result is *dense*!</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 0.1512 0.1064 0.0014 0</pre><h2>Kruskal tensor times vector (ttv for ktensor)<a name="18"></a></h2> <p>The special structure of a ktensor allows an efficient implementation of vector multiplication. The result is a ktensor or a scalar. </p><pre class="codeinput">X = ktensor([10;1],rand(5,2),rand(3,2),rand(4,2),rand(2,2)); <span class="comment">%<-- Ktensor.</span>Y = ttv(X, A, 1) <span class="comment">%<-- X times A in mode 1. Result is a ktensor.</span></pre><pre class="codeoutput">Y is a ktensor of size 3 x 4 x 2 Y.lambda = [ 5.9997 1.1433 ] Y.U{1} = 0.6927 0.4418 0.0841 0.3533 0.4544 0.1536 Y.U{2} = 0.6756 0.5548 0.6992 0.1210 0.7275 0.4508 0.4784 0.7159 Y.U{3} = 0.8928 0.2548 0.2731 0.8656</pre><pre class="codeinput">norm(full(Y) - ttv(full(X),A,1)) <span class="comment">%<-- Result is the same as dense case.</span></pre><pre class="codeoutput">ans = 8.8340e-016</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}) <span class="comment">%<-- All-mode multiply -- scalar result.</span></pre><pre class="codeoutput">Y = 4.8677</pre><pre class="codeinput">Y = ttv(X, {C,D}, [3 4]) <span class="comment">%<-- X times C in mode-3 & D in mode-4.</span></pre><pre class="codeoutput">Y is a ktensor of size 5 x 3 Y.lambda = [ 6.0729 0.78558 ] Y.U{1} = 0.6124 0.5869 0.6085 0.0576 0.0158 0.3676 0.0164 0.6315 0.1901 0.7176 Y.U{2} = 0.6927 0.4418 0.0841 0.3533 0.4544 0.1536</pre><pre class="codeinput">Y = ttv(X, {A,B,D}, [1 2 4]) <span class="comment">%<-- 3-way multiplication.</span></pre><pre class="codeoutput">Y is a ktensor of size 4 Y.lambda = [ 3.6628 0.93892 ] Y.U{1} = 0.6756 0.5548 0.6992 0.1210 0.7275 0.4508 0.4784 0.7159</pre><h2>Tucker tensor times vector (ttv for ttensor)<a name="23"></a></h2> <p>The special structure of a ttensor allows an efficient implementation of vector multiplication. The result is a ttensor or a scalar. </p><pre class="codeinput">X = ttensor(tenrand([2,2,2,2]),rand(5,2),rand(3,2),rand(4,2),rand(2,2));Y = ttv(X, A, 1) <span class="comment">%<-- X times A in mode 1.</span></pre><pre class="codeoutput">Y is a ttensor of size 3 x 4 x 2 Y.core is a tensor of size 2 x 2 x 2 Y.core(:,:,1) = 1.3171 0.2658 1.0694 0.9612 Y.core(:,:,2) = 1.3377 1.4308 0.3816 0.7186 Y.U{1} = 0.8729 0.9669 0.2379 0.6649 0.6458 0.8704 Y.U{2} = 0.0099 0.8903 0.1370 0.7349 0.8188 0.6873 0.4302 0.3461 Y.U{3} = 0.1660 0.1911 0.1556 0.4225</pre><pre class="codeinput">norm(full(Y) - ttv(full(X),A, 1)) <span class="comment">%<-- Same as dense case.</span></pre><pre class="codeoutput">ans = 3.9154e-016</pre><pre class="codeinput">Y = ttv(X, {A,B,C,D}, [1 2 3 4]) <span class="comment">%<-- All-mode multiply -- scalar result.</span></pre><pre class="codeoutput">Y = 3.8758</pre><pre class="codeinput">Y = ttv(X, {C,D}, [3 4]) <span class="comment">%<-- X times C in mode-3 & D in mode-4.</span></pre><pre class="codeoutput">Y is a ttensor of size 5 x 3 Y.core is a tensor of size 2 x 2 Y.core(:,:) = 0.6489 0.3358 0.5348 0.3779 Y.U{1} = 0.3651 0.4586 0.3932 0.8699 0.5915 0.9342 0.1197 0.2644 0.0381 0.1603 Y.U{2} = 0.8729 0.9669 0.2379 0.6649 0.6458 0.8704</pre><pre class="codeinput">Y = ttv(X, {A,B,D}, [1 2 4]) <span class="comment">%<-- 3-way multiplication.</span></pre><pre class="codeoutput">Y is a ttensor of size 4 Y.core is a tensor of size 2 Y.core(:) = 2.3205 2.3598 Y.U{1} = 0.0099 0.8903 0.1370 0.7349 0.8188 0.6873 0.4302 0.3461</pre><h2>Tensor times matrix (ttm for tensor)<a name="28"></a></h2> <p>Compute a tensor times a matrix (or matrices) in one (or more) modes.</p><pre class="codeinput">X = tensor(rand(5,3,4,2));A = rand(4,5); B = rand(4,3); C = rand(3,4); D = rand(3,2);</pre><pre class="codeinput">Y = ttm(X, A, 1); <span class="comment">%<-- X times A in mode-1.</span>Y = ttm(X, {A,B,C,D}, 1); <span class="comment">%<-- Same as above.</span>Y = ttm(X, A', 1, <span class="string">'t'</span>) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 x 3 x 4 x 2 Y(:,:,1,1) = 1.0365 0.6095 0.7110 1.9302 1.4742 2.0003 1.7555 1.2961 1.7017 1.8896 1.4325 1.5902 Y(:,:,2,1) = 0.6694 0.9350 0.8098 1.4311 2.0724 1.5604 1.2080 1.5796 1.4965 1.2773 1.7966 1.4659 Y(:,:,3,1) = 1.1284 1.1872 1.2511 1.8427 1.8095 1.8762 1.6982 1.5964 1.5908 1.8864 1.8810 1.8543 Y(:,:,4,1) = 0.9565 1.0452 0.8766 1.7992 1.8762 1.8659 1.4832 1.6716 1.9043 1.6718 1.8121 1.7510 Y(:,:,1,2) = 1.1974 0.8965 0.8668 1.5665 2.1589 1.3825 1.3373 2.0494 1.1534 1.5943 2.0267 1.4569 Y(:,:,2,2) = 1.0229 1.3605 1.0827 2.3149 2.1127 1.9503 2.1861 1.8910 1.5869 2.0542 1.9491 1.9094 Y(:,:,3,2) = 0.7033 0.8874 0.5347 1.4749 1.4350 1.3381 1.5048 1.3274 1.2796 1.2465 1.5395 1.1617 Y(:,:,4,2) = 1.3135 0.2809 0.9096 2.4720 1.0792 1.5503 2.2423 0.9677 1.1401 2.3171 0.8680 1.4500</pre><pre class="codeinput">Y = ttm(X, {A,B,C,D}, [1 2 3 4]); <span class="comment">%<-- 4-way mutliply.</span>Y = ttm(X, {D,C,B,A}, [4 3 2 1]); <span class="comment">%<-- Same as above.</span>Y = ttm(X, {A,B,C,D}); <span class="comment">%<-- Same as above.</span>Y = ttm(X, {A',B',C',D'}, <span class="string">'t'</span>) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 x 4 x 3 x 3 Y(:,:,1,1) = 2.4869 4.5774 4.3080 2.4909 4.7042 8.5104 8.0518 4.6694 4.1588 7.5379 7.1537 4.1590 4.4802 8.1581 7.6647 4.4226 Y(:,:,2,1) = 2.4107 4.4549 4.1826 2.4144 4.8310 8.7053 8.2015 4.7393 4.2267 7.6101 7.2157 4.1903 4.4979 8.1691 7.6629 4.4153 Y(:,:,3,1) = 1.8798 3.4093 3.2097 1.8545 3.3879 6.1536 5.8167 3.3717 3.0143 5.4614 5.1902 3.0207 3.2654 5.9270 5.5773 3.2215 Y(:,:,1,2) = 1.4376 2.7014 2.5398 1.4693
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -