简体   繁体   中英

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]

Dot product is the summation of multiplication of values in two vectors:

So I am guessing you want to multiply all values along the channel dimension and need to find the summation of the result, please correct me if my understanding is wrong.

import torch

t1 = torch.rand(10, 3, 240, 320)
t2 = torch.rand(10, 3, 240, 320)

# Multiply two tensors and sum along the channel dimension
multp  = t1 * t2 
summed = multp.sum(dim = 1, keepdim = True)

print(summed.shape)  # torch.Size([10, 1, 240, 320])    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM