Vectorization and broadcasting

i have two tensors:
alpha.size() ->[3]
image.size() -> [16, 3, 28, 28].

I want to elementwise add alpha[i] to all elements in image[16, i, 28, 28].

what is the (in best case most efficient) vectorized way to do so?

In general, you can add two n-dimensional vectors where n_1 == n_2. Element-wise addition where n_1 != n_2 is not possible, unless you pad the shorter vector with 0’s and force n_1 == n_2.

i want to broadcast alpha[i] to the size [16,1,32,32] and add it to the ith channel of the image. don’t want to pad here. is there a vectorized way to achieve that?

Hi Milan!

Yes, you may use broadcasting to “vectorize” your element-wise addition.
You just need to use unsqueeze() to add the singleton dimensions that
will then get broadcast:

image + alpha.unsqueeze (-1).unsqueeze (-1)

Equivalently, I sometimes find the index-with-None syntax stylistically
more readable, depending on the use case:

image + alpha[:, None, None]
# or
image + alpha[None, :, None, None]

Best.

K. Frank

1 Like