Vector-Tensor element-wise multiplication in Pytorch
I am trying to extract the luminance from a tensor representing an image in Pytorch, and so I need to multiply element-wise a vector of size 3 (for the three RGB value weights) by a 3xNxN tensor representing the image such that I obtain a NxN matrix in the end where the three channels of the tensor have been summed with the weights given in the vector.

I guess there exists Pytorch operations that would help me do that without loops but I haven't found them.

You have to reshape your 3 dimensional RGB vector to be broadcastable to 3xNxN like this:

rgb = rgb.reshape(-1, 1, 1)


So it will have shape (3, 1, 1)

Now you can multiply it with original image and sum along first dimension:

result = torch.sum(rgb * image, dim=0)

