Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTorch element-wise product of vectors / matrices / tensors

Tags:

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
like image 837
Tom Hale Avatar asked Nov 19 '18 06:11

Tom Hale


People also ask

How do you multiply tensors element-wise?

mul() method is used to perform element-wise multiplication on tensors in PyTorch. It multiplies the corresponding elements of the tensors. We can multiply two or more tensors. We can also multiply scalar and tensors.

How do you subtract two tensors in PyTorch?

To perform element-wise subtraction on tensors, we can use the torch. sub() method of PyTorch. The corresponding elements of the tensors are subtracted. We can subtract a scalar or tensor from another tensor.

What is Torch tensor ()?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.


1 Answers

Given two tensors A and B you can use either:

  • A * B
  • torch.mul(A, B)
  • A.mul(B)

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

like image 65
Tom Hale Avatar answered Sep 30 '22 19:09

Tom Hale