Multiply one tensor of channel 1 with a tensors with multiple channel across all channel


(Jean Paul Ainam) #1

Hi. I have a tensor y of shape torch.Size([32, 1, 4, 2]) and x of shape torch.Size([32, 2048, 4, 2])
I want to multiply x*y across all x channel. I have tried

# case 1
xy = x*y  # shape torch.Size([32, 2048, 4, 2])

# case 2
b, c, h, w = x.size()   # shape  torch.Size([32, 2048, 4, 2])
b_att, c_att, h_att, w_att = y.size()   # shape torch.Size([32, 1, 4, 2])
x = x.view(b, c, h_att, h // h_att, w_att, w // w_att)
xy= x * y.view(b, 1, h_att, 1, w_att, 1)  # shape torch.Size([32, 2048, 4, 1, 2, 1])
# Bring back the initial shape
xy = xy.view(b, c, h, w)  # shape  torch.Size([32, 2048, 4, 2])

The two methods don’t give any error. Which method is correct?
I just want to make sure the multiplication is really done across all the channel of x because y is one channel
Thank you


#2

As both methods return the same result, I would prefer the first one.