Tensor multiplication along certain axis

Samue1: <p>I have two tensors. <code>A</code> has shape <code>(N, C, H, W)</code> and <code>B</code> has shape <code>(C)</code>. Now I want to multiply both tensors along <code>C</code>.</p>
<p>Currently I use <code>torch.einsum("ijkl,j-&gt;ijkl", A, B)</code> and it seems to work.</p>
<p>I would like to know if there is a better or more intuitive way to do this? Mabe with <code>.view()</code>?</p>
<p>Thanks!</p>

Azerus: <p>Hi,</p>
<p>Yes, you can use view() safely here, or reshape.</p>
<details>
<summary>
See this for the differences between view and reshape</summary>
<blockquote>
<p>View is quick to perform but will fails on non-contiguous tensors.<br>
Reshape always works. When possible, the returned tensor will be a view of <code>input</code>. Otherwise, it will be a copy.</p>
<p>But I don’t think it’s possible for 1D tensor to be non-contiguous, so you should not have to care about that here.</p>
</blockquote>
</details>
<p>The broadcasting is performed starting from last dimension, therefor you need B to get the shape <code>(C, 1, 1)</code> before the multiplication.</p>
<p>So basically, <code>torch.einsum("ijkl,j-&gt;ijkl", A, B)</code> should be equivalent to <code>A*(B.view(-1, 1, 1))</code> and equivalent to <code>A*(B.unsqueeze(-1).unsqueeze(-1))</code>.</p>
<p>I would tend to use <code>A*(B.view(-1, 1, 1))</code> here which is short, clear, and efficient.</p>

Samue1: <p>Thank you! That looks much better to my eyes! But I don’t understand when to use <code>.view()</code> and when to use <code>.reshape()</code>. The explanation didn’t help. Can you please explain the difference in layman’s terms?</p>

Azerus: <p>Without going into details, the data of a tensor (whatever its shape is) is always stored internally as a kind of 1d array. The shape of the tensor is fixed by how is defined the mapping of indices to memory pointers that we call the memory layout. For instance, when you do <code>b = a.view(some_shape)</code>, <code>a</code> and <code>b</code> will share the same data to avoid doing a costly copy, but may have different memory layout. In Pytorch, contiguous refer to some specific memory layout, so a tensor can be “contiguous” or “non contiguous”.<br>
Contiguous tensor are usually more convenient as few operations won’t work with non contiguous tensor, <code>view</code> is one example of operation that won’t work on non contiguous tensor in input. The operation <code>reshape</code> has the same behavior than <code>view</code> except that it will work with non-contiguous data in witch case the data will be copied.</p>
<p>But you should not worry too much about the contiguous property of your tensor. Most of the time you won’t have any problems, and if an error is raised at some point because an operation was expecting a contiguous input tensor you can simply fix it by changing <code>operation(tensor)</code> to <code>operation(tensor.contiguous())</code> to make it work.</p>
<p>So, in short when you want a reshaped tensor <code>b</code> from <code>a</code>:</p>
<ul>
<li>If <code>a</code> is contiguous and you want <code>a</code> and <code>b</code> to share the same storage, use <code>b = a.view(some_shape)</code>. So if you do an in-place operation afterward on <code>a</code>, <code>b</code> will also be modified.</li>
<li>If <code>a</code> is contiguous and you don’t want <code>a</code> and <code>b</code> to share the same storage, use <code>b = a.view(some_shape).clone()</code> or <code>b = a.reshape(some_shape).clone()</code>.</li>
<li>If <code>a</code> is not contiguous, use <code>b = a.reshape(some_shape)</code> or <code>a.contiguous().view(some_shape)</code>, <code>a</code> and <code>b</code> won’t share the same storage.</li>
<li>If <code>a</code> may be contiguous or non contiguous, use <code>b = a.reshape(some_shape)</code>. But you cannot know if the storage of <code>a</code> and <code>b</code> will share the same data or not.</li>
<li>If <code>a</code> may be contiguous or non contiguous and you don’t want <code>a</code> and <code>b</code> to possibly share the same data  use <code>b = a.reshape(some_shape).clone()</code>
</li>
</ul>
<p>For more information on what mean contiguous in pytorch, you can look to <a href="https://discuss.pytorch.org/t/contigious-vs-non-contigious-tensor/30107/2">this thread</a>. I invite you also to read <a href="https://pytorch.org/docs/stable/tensor_view.html" rel="noopener nofollow ugc">this page doc about view</a>.</p>
<p>I know the contiguous thing can be confusing, I hope this answer helped</p>

Samue1: <p>Thank you so much! This helped me a lot!</p>