Hi Yang!



 optyang:

I have a tensor x1 4x3x2x2, and a tensor x2 4x1. I would like tensor x1 and x2 multiply for each element along axis 0 (which has a dimension of 4). Each such multiplication would be between a tensor 3x2x2 and a scalar, so the result would be a tensor 4x3x2x2.


If I understand what you are asking, you could either transpose and use
broadcasting:
(x1.transpose (0, 3) * x2.squeeze()).transpose (0, 3)

or use torch.einsum (“Einstein summation”):
torch.einsum ('ijkl, im -> ijkl', x1, x2)

Best.
K. Frank