How to perform an scalar element-wise multiplication?

Suppose to have a coefficients tensor with a shape (A,B) and another tensor of values with shape (A,B,C,D…) how can i do an scalar element-wise multiplication such that the results will be each subtensor of shape (C,D,…) multiplied by each element in the coefficients tensor?

To be clear with A=3, B=3, C=2 and D=2
the coefficients tensor can be something like
C = [[0.1 0.2 0.3]
[0.4 0.5 0.6]
[0.7 0.8 0.9]
and the values tensor (V) will be a 3x3x2x2 tensor with each 3x3 “cell” being a 2x2 matrix
the results of this operation should be each 2x2 matrix scalarly multiplied by the respective element in C so that
V[0][0] = V[0][0] * C[0][0]
V[0][1] = V[0][1] * C[0][1]
V[0][2] = V[0][2] * C[0][2]
V[1][0] = V[1][0] * C[1][0]
and so on…

Where each of this * is a scalar multiplication between an element of the matrix C and a matrix of the tensor V

>>> C = torch.randn(3,3)                                                                                                                                                                      
>>> V = torch.randn(3,3,2,2)
>>> C*V
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 3                                                                                       
>>> torch.dot(C,V)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dot: Expected 1-D argument self, but got 2-D
>>> torch.mul(C,V)                                                                                                                                                                            
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 3

Maybe somewhere in the docs there is the answer to my question but i could find it, thanks in advice!

1 Like

I think you are on the right track.
Automatic broadcasting doesn’t work in your example, as the dimensions are missing.
Try to unsqueeze C and it should work:

A, B, C, D = 3, 3, 2, 2
c = torch.ones(A, B) * 2
v = torch.randn(A, B, C, D)

d = c[:, :, None, None] * v
print((d[0, 0] == v[0, 0]* 2).all())
4 Likes

It works perfectly! I didn’t know this trick :grinning: , many thanks!