I have two tensors of shape (16, 300) and (16, 300) where 16 is the batch size and 300 is some representation vector. I want to compute the element-wise batch matrix multiplication to produce a matrix (2d tensor) whose dimension will be (16, 300). So, in short I want to do 16 element-wise multiplication of two 1d-tensors.

I can do this using a for loop but is there any way, I can do it using torch API?

torch.bmm does matrix multiplication, not element-wise multiplication, so it can’t fulfill my purpose. (*) operator with a for loop is working for me. Btw, I also tried torch.cmul() but it is not working. Can you tell me why? Its giving me the error - AttributeError: module 'torch' has no attribute 'cmul'. What I am missing?

If you have tensor a and b both of shape (16, 300) and you want to get a tensor of shape (16, 300) by element-wise multiplication, I assume you can do a*b?

How can I do element-wise mul between a matrix and a vector, say 3x5 mat and 3x1 vec, as in numpy? Pytorch’s torch.mul told me that the sizes are inconsistent.