*My post explains Dot, Matrix and Element-wise multiplication in PyTorch.
<Dot multiplication>
- dot() can multiply 1D tensors:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
torch.dot(tensor1, tensor2) # tensor(53)
tensor1.dot(tensor2) # tensor(53)
-
@
or matmul() can multiply 1D or more D tensors:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 @ tensor2 # tensor(53)
torch.matmul(tensor1, tensor2) # tensor(53)
tensor1.matmul(tensor2) # tensor(53)
<Matrix multiplication>
- mv() can multiply a 2D and 1D tensor:
import torch
tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor2 = torch.tensor([5, 0, 8]) # 1D tensor
torch.mv(tensor1, tensor2) # tensor([42, 56])
tensor1.mv(tensor2) # tensor([42, 56])
- mm() can multiply 2D tensors:
import torch
tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor2 = torch.tensor([[5, 0, 8, 6], # 2D tensor
[3, 6, 1, 7],
[1, 4, 9, 2]])
torch.mm(tensor1, tensor2)
# tensor([[35, 58, 59, 69], [51, 26, 85, 73]])
tensor1.mm(tensor2)
# tensor([[35, 58, 59, 69], [51, 26, 85, 73]])
- bmm() can multiply 3D tensors:
import torch
tensor1 = torch.tensor([[[2, 7]], [[8, 3]]]) # 3D tensor
tensor2 = torch.tensor([[[5, 9], [3, 6]], # 3D tensor
[[7, 2], [1, 4]]])
torch.bmm(tensor1, tensor2) # tensor([[[31, 60]], [[59, 28]]])
tensor1.bmm(tensor2) # tensor([[[31, 60]], [[59, 28]]])
-
@
ormatmul()
can multiply 1D or more D tensors by dot or matrix multiplication:
import torch
tensor1 = torch.tensor([[2, 7], [8, 3]]) # 2D tensor
tensor2 = torch.tensor([[[[5, 9], [3, 6]], [[7, 2], [1, 4]]],
[[[6, 0], [4, 6]], [[2, 9], [8, 1]]]])
# 4D tensor
tensor1 @ tensor2
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
# [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
torch.matmul(tensor1, tensor2)
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
# [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
tensor1.matmul(tensor2)
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
# [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
<Element-wise calculation>
-
*
or mul() can do multiplication with 0D or more D tensors. *mul()
and multiply() are the same becausemultiply()
is the alias ofmul()
:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 * tensor2 # tensor([12, 21, 20])
torch.mul(tensor1, tensor2) # tensor([12, 21, 20])
tensor1.mul(tensor2) # tensor([12, 21, 20])
-
/
or div() can do division with 0D or more D tensors. *div()
, divide() and true_divide() withrounding_mode=None
are the same becausedivide()
andtrue_divide()
withrounding_mode=None
are the aliases ofdiv()
:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 / tensor2 # tensor([0.3333, 2.3333, 0.8000])
torch.div(tensor1, tensor2) # tensor([0.3333, 2.3333, 0.8000])
tensor1.div(tensor2) # tensor([0.3333, 2.3333, 0.8000])
-
+
or add() can do addition with 0D or more D tensors:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 + tensor2 # tensor([8, 10, 9])
torch.add(tensor1, tensor2) # tensor([8, 10, 9])
tensor1.add(tensor2) # tensor([8, 10, 9])
-
-
or sub() can do subtraction with 0D or more D tensors. *sub()
and subtract() are the same becausesubtract()
is the alias ofsub()
:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 - tensor2 # tensor([-4, 4, -1])
torch.subtract(tensor1, tensor2) # tensor([-4, 4, -1])
tensor1.subtract(tensor2) # tensor([-4, 4, -1])
-
%
or remainder() can do modulo(mod) calculation with 0D or more D tensors:
import torch
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor
tensor1 % tensor2 # tensor([2, 1, 4])
torch.remainder(tensor1, tensor2) # tensor([2, 1, 4])
tensor1.remainder(tensor2) # tensor([2, 1, 4])
Top comments (0)