PyTorch element-wise product of vectors / matrices / tensors

Tom Hale picture Tom Hale · Nov 19, 2018 · Viewed 13.8k times · Source

In PyTorch, how do I get the element-wise product of two vectors / matrices / tensors?

For googlers, this is product is also known as:

  • Hadamard product
  • Schur product
  • Entrywise product

Answer

Tom Hale picture Tom Hale · Nov 19, 2018

Given two tensors A and B you can use either:

Note: for matrix multiplication, you want to use A @ B which is equivalent to torch.matmul().