In PyTorch, how do I get the element-wise product of two vectors / matrices / tensors?
For googlers, this is product is also known as:
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.
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.
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With