How to broadcast a 1D tensor with a 4D tensor?


(henrykuo) #1

Suppose the size of a is (128,) and the size of b is (128, 3, 64, 64). How to do broadcast multiplication with these two tensors?


(Kelam Goutam) #2

I guess u need to convert tensor A to size (128,1) and then multiply with tensor B.

output  = A. unsqueeze(1) * B 

(henrykuo) #3

@kelam_goutam Thanks for your reply. It works if the size of b is (128,3), i.e, b is 2-dimensional tensor, but doesn’t work when the dimension of b is more than 2 dimensions.


(Justus Schock) #4

the most generic way would be to do something like this (which is not broadcasting, but explicit expanding):

output = a.expand_as(b)*b

Edit:
if you want to do it with broadcasting, you can simply do

output = a * b

if the shapes cannot be broadcasted, an error will be raised, but you will also receive this error if you use the explicit approach


(henrykuo) #5

Thanks for your apply. I tried the solution but it does not work as I expected.
A related question. Suppose I have a = torch.tensor([1,2,3]), how to expand it to

tensor([[[ 1, 1, 1],
[ 1, 1, 1]],

       [[ 2,  2,  2],
       [ 2,  2,  2]],

       [[ 3,  3,  3],
       [ 3,  3,  3]]])

Thanks!


(Kelam Goutam) #6

A crude way to solve your problem could be the extension of my previous solution.

output = A.unsqueeze(1).unsqueeze(2).unsqueeze(3) * B

This basically makes A a 4D tensor.


(henrykuo) #7

@kelam_goutam Thanks!


(colesbury) #8

These also work:

output = a.reshape(128, 1, 1, 1) * b

or

output = a.reshape(-1, 1, 1, 1) * b