Multiply Column Vector and Matrix

Hi,
Can the eight 0.4 in the first column of tensor1 be multiplied by the eight rows of tensor2? As shown by the red and blue circles. Require 0.4 to be multiplied by the three numbers in the first row, and so on. How should this be achieved?

tensor1([[0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000],
        [0.5000, 0.5000]], device='cuda:0')
tensor2([[0.0794, 0.0728, 0.8477],
        [0.1744, 0.0727, 0.7529],
        [0.6121, 0.2235, 0.1644],
        [0.0610, 0.0458, 0.8932],
        [0.5754, 0.1586, 0.2660],
        [0.8185, 0.1012, 0.0802],
        [0.4628, 0.1336, 0.4036],
        [0.3557, 0.0482, 0.5960]], device='cuda:0')

You can add singleton dimensions and just multiply them, e.g. t1[:, None, :] * t2[:, :, None] will give you a batch x 3 x 2 tensor if that is what you want.
If you have reduction axes mixed in, you could look at einsum.

Sorry, I did not understand what you mean. Can you express it in code? Thank!

What is the code above not doing that you want?

I don’t know how to use the method you mentioned. The example code is in the program and an error is reported. I mean, can you write a specific code to achieve the product of two red circles? thank!

Hi @ky_Pa ,
I formulated tensor1 and tensor2 as in your case.
I formulated a tensor temp from tensor1 that has a shape of (8,3) to enable element-wise multiplication.

tensor1 = torch.tensor([[0.4,0.6]]*8)
tensor2 = torch.tensor([[0.0794, 0.0728, 0.8477],
                        [0.1744, 0.0727, 0.7529],
                        [0.6121, 0.2235, 0.1644],
                        [0.0610, 0.0458, 0.8932],
                        [0.5754, 0.1586, 0.2660],
                        [0.8185, 0.1012, 0.0802],
                        [0.4628, 0.1336, 0.4036],
                        [0.3557, 0.0482, 0.5960]])
temp = tensor1[:,0].repeat(1,3).reshape(8,3)
torch.mul(temp, tensor2)
# OR
# np.multiply(temp,tensor2)

Hope it may work for you. :upside_down_face:

(tensor1[:, :, None] * tensor2[:, None, :])[:, 0] gives the exact same result without using repeat. I don’t know if you want only the 0 index, then you would do the indexing before the multiplication.
The key trick here is broadcasting.

Best regards

Thomas

1 Like

Thank you @tom for introducing broadcasting.

It is cool!