# Dot product along a dimension

I have two tensors of shape [B, 3 , 240, 320] where B represents the batch size 3 represents the channels, 240 the height(H), 320 the width(W).

I need to find the dot product along the channels dimension(3 channels) thus the resulting tensor would be of shape [B, 1 , 240, 320].
My tensors have float32 elements in gpu(cuda to backprop).

Can you all please suggest how I can do that?

Thanks!

More clarification:

Let’s say we have B=10, H=100, W=200.
So from the above would be common for both the first and seconds tensors. If we keep B, H, W constant we get a 1D vector as the resultant tensor(with 3 elements). I need to take the dot product of these two vectors. Thus the resultant tensor is of dimension [B, 1, 240, 320]

``````A = torch.rand(2, 3, 4, 4)
B = torch.rand(2, 4, 4)
C = (A * B.unsqueeze(1)).sum(1, keepdims=True)
``````
``````A = torch.rand(2, 3, 4, 4)
B = torch.rand(3)
C = (A, B[None, :, None, None]).sum(1, keepdims=True)
``````
``````A = torch.rand(2, 3, 4, 4)
B = torch.rand(2, 3, 4, 4)
C = (A * B).sum(1, keepdims=True)
``````