Multiply Column Vector and Matrix

username: ky_Pa
cooked: <p>Hi,<br>
Can the eight 0.4 in the first column of tensor1 be multiplied by the eight rows of tensor2? As shown by the red and blue circles. Require 0.4 to be multiplied by the three numbers in the first row, and so on. How should this be achieved?</p>
<pre><code class="lang-auto">tensor1([[0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000]], device='cuda:0')
tensor2([[0.0794, 0.0728, 0.8477],
        [0.1744, 0.0727, 0.7529],
        [0.6121, 0.2235, 0.1644],
        [0.0610, 0.0458, 0.8932],
        [0.5754, 0.1586, 0.2660],
        [0.8185, 0.1012, 0.0802],
        [0.4628, 0.1336, 0.4036],
        [0.3557, 0.0482, 0.5960]], device='cuda:0')
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fbb28b040797cce8fe18492d3cb7f7e401da2bbe.png" data-download-href="https://discuss.pytorch.org/uploads/default/fbb28b040797cce8fe18492d3cb7f7e401da2bbe" title="微信图片_20210715154451"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fbb28b040797cce8fe18492d3cb7f7e401da2bbe.png" alt="微信图片_20210715154451" data-base62-sha1="zUCn330wP5MXFElL9QJElOQ2IEK" width="690" height="488" data-dominant-color="42464A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20210715154451</span><span class="informations">786×556 59.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: tom
cooked: <p>You can add singleton dimensions and just multiply them, e.g. <code>t1[:, None, :] * t2[:, :, None]</code> will give you a batch x 3 x 2 tensor if that is what you want.<br>
If you have reduction axes mixed in, you could look at einsum.</p>

username: ky_Pa
cooked: <p>Sorry, I did not understand what you mean. Can you express it in code? Thank!</p>

username: tom
cooked: <p>What is the code above not doing that you want?</p>

username: ky_Pa
cooked: <aside class="quote no-group" data-username="tom" data-post="2" data-topic="126810">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/48/3162_2.png" class="avatar"> tom:</div>
<blockquote>
<p>t1[:, None, :] * t2[:, :, None]</p>
</blockquote>
</aside>
<p>I don’t know how to use the method you mentioned. The example code is in the program and an error is reported. I mean, can you write a specific code to achieve the product of two red circles? thank!</p>

username: rajkumar_galaxy
cooked: <p>Hi <a class="mention" href="/u/ky_pa">@ky_Pa</a> ,<br>
I formulated tensor1 and tensor2 as in your case.<br>
I formulated a tensor temp from tensor1 that has a shape of (8,3) to enable element-wise multiplication.</p>
<pre><code class="lang-auto">tensor1 = torch.tensor([[0.4,0.6]]*8)
tensor2 = torch.tensor([[0.0794, 0.0728, 0.8477],
                        [0.1744, 0.0727, 0.7529],
                        [0.6121, 0.2235, 0.1644],
                        [0.0610, 0.0458, 0.8932],
                        [0.5754, 0.1586, 0.2660],
                        [0.8185, 0.1012, 0.0802],
                        [0.4628, 0.1336, 0.4036],
                        [0.3557, 0.0482, 0.5960]])
temp = tensor1[:,0].repeat(1,3).reshape(8,3)
torch.mul(temp, tensor2)
# OR
# np.multiply(temp,tensor2)
</code></pre>
<p>Hope it may work for you. <img src="https://discuss.pytorch.org/images/emoji/apple/upside_down_face.png?v=9" title=":upside_down_face:" class="emoji" alt=":upside_down_face:"></p>

username: tom
cooked: <p><code>(tensor1[:, :, None] * tensor2[:, None, :])[:, 0]</code> gives the exact same result without using repeat. I don’t know if you want only the <code>0</code> index, then you would do the indexing before the multiplication.<br>
The key trick here is <a href="https://pytorch.org/docs/stable/notes/broadcasting.html" rel="noopener nofollow ugc">broadcasting</a>.</p>
<p>Best regards</p>
<p>Thomas</p>

username: rajkumar_galaxy
cooked: <p>Thank you <a class="mention" href="/u/tom">@tom</a> for introducing broadcasting.</p>
<aside class="quote no-group" data-username="tom" data-post="7" data-topic="126810">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/48/3162_2.png" class="avatar"> tom:</div>
<blockquote>
<p><code>(tensor1[:, :, None] * tensor2[:, None, :])[:, 0]</code></p>
</blockquote>
</aside>
<p>It is cool!</p>