Multipy two tensors

I want to multiply two tensors x and y, such that z is the output. How to do this?

X = torch.tensor([[[1,2,3,4],[5,6,7,8],[9,9,9,9]],[[10,20,30,40],[50,60,70,80],[90,90,90,90]]])

y = torch.tensor([[10,100,1000],[2,4,6]])

X = torch.tensor([[[10,20,30,40],[500,600,700,800],[9000,9000,9000,9000]],[[20,40,60,80],[200,240,280,320],[540,540,540,540]]])

Hi,

the following will get what you want,

Z = X*y.unsqueeze(2)

Which are equal,

>>> X*y.unsqueeze(2)
tensor([[[  10,   20,   30,   40],
         [ 500,  600,  700,  800],
         [9000, 9000, 9000, 9000]],

        [[  20,   40,   60,   80],
         [ 200,  240,  280,  320],
         [ 540,  540,  540,  540]]])
>>> Z
tensor([[[  10,   20,   30,   40],
         [ 500,  600,  700,  800],
         [9000, 9000, 9000, 9000]],

        [[  20,   40,   60,   80],
         [ 200,  240,  280,  320],
         [ 540,  540,  540,  540]]])

1 Like