akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
Multiplication of tensors of different dimensions

username: oasjd7
cooked: <p>I want to multiply different scalar values for each image.</p>
<p>For example, the image have [4,3,10,10] shape. (batch size, channel, w,h).</p>
<p><code>0.1 * image</code> works fine but,</p>
<p><code>[0.1, 0.2, 0.3, 0.4] * image</code> doesn’t work because the dimensions are different.</p>
<p>How can I multiply this ?</p>

username: ptrblck
cooked: <p>You can <code>unsqueeze</code> the additional dimensions to multiply the list entries with each sample in the batch:</p>
<pre><code class="lang-python">x = torch.randn(4,3,10,10)
f = torch.tensor([0.1, 0.2, 0.3, 0.4])
res = f[:, None, None, None] * x
</code></pre>

username: KURUVILLA_ABRAHAM
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="104842">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>unsqueeze</p>
</blockquote>
</aside>
<p>hello partick ,<br>
is my tensor is below <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>f = torch.zeros(1, 1, 3, dtype=torch.float)<br>
f[:,:,0] = 255</p>
<p>and i want to multiply with a above</p>
<p>x = torch.randn(4,3,10,10)<br>
res = x*f<br>
what would be the correct way to do the same?</p>

username: ptrblck
cooked: <p>The current shapes are incompatible, since <code>dim2</code> in <code>f</code> won’t be broadcasted to <code>x</code>.<br>
Since you are using 3 elements in <code>f</code>, you could align <code>dim2</code> to <code>dim1</code> in <code>x</code> which would work:</p>
<pre><code class="lang-python">f = torch.zeros(1, 1, 3, dtype=torch.float)
f[:,:,0] = 255
f = f.squeeze(1).unsqueeze(2).unsqueeze(3)

x = torch.randn(4,3,10,10)
res = x*f
</code></pre>

username: KURUVILLA_ABRAHAM
cooked: <p>Hello patrick.</p>
<p>Thanks</p>
<p>What if if we have mutiple elements in f and we want to mutuply each time with elements of x</p>
<p>In this case if x is batch size of 4</p>
<p>And f is of2</p>
<p>So it should be 8 elements in total .</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="KURUVILLA_ABRAHAM" data-post="5" data-topic="104842">
<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/kuruvilla_abraham/48/45695_2.png" class="avatar"> KURUVILLA_ABRAHAM:</div>
<blockquote>
<p>In this case if x is batch size of 4</p>
<p>And f is of2</p>
<p>So it should be 8 elements in total .</p>
</blockquote>
</aside>
<p>I don’t quite understand the use case. If you want to apply an elementwise multiplication you would either have to make sure the number of elements match in both tensors or are broadcastable.</p>