Columnwise/elementwise operations

Is it possible to perform a column-wise operation element-wise? For example, if I have the following tensors

A = torch.ones(3, 2)
B = torch.ones(3)

I would like to multiply each column (elementwise) of A with with B, which would result in a (3x2) matrix of 2s, or add each column of A with B which would result in a (3x2) matrix of 3s.

I recognize that I can do this procedure with a for loop, but want to know if there’s a way to do it “within” pytorch.

You can use torch.mul()
Here you can find more info

I don’t think torch.mul works if two tensors are not broadcastable.

For example,
x = torch.ones(3,2)
y = torch.ones(3)
torch.mul(x,y)

Gives an error:
RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1
I am yet not able to find solution to how to multiply all columns of one tensor with other tensor

Ok found the solution: