sum(), prod() and cartesian_prod() in PyTorch

Super Kai (Kazuya Ito) - Apr 23 - - Dev Community

sum() can get the 0D or more D tensor of one or more sums from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • sum() can be used with torch or a tensor.
  • The 1st argument with torch or using a tensor is input(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int, tuple of int or list of int).
  • The 3rd argument with torch or the 2nd argument with a tensor is keepdim(Optional-Default:False-Type:bool) which keeps the dimension of input tensor: *Memos:
    • keepdim= must be used with dim=.
    • My post explains keepdim argument.
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, dtype is inferred from input or dtype of set_default_dtype() is used for floating-point numbers.
    • My post explains dtype argument.
import torch

my_tensor = torch.tensor([0, 1, 2, 3])

torch.sum(input=my_tensor)
my_tensor.sum()
torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor(6)

my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]])

torch.sum(input=my_tensor)
torch.sum(input=my_tensor, dim=(0, 1))
torch.sum(input=my_tensor, dim=(0, -1))
torch.sum(input=my_tensor, dim=(1, 0))
torch.sum(input=my_tensor, dim=(1, -2))
torch.sum(input=my_tensor, dim=(-1, 0))
torch.sum(input=my_tensor, dim=(-1, -2))
torch.sum(input=my_tensor, dim=(-2, 1))
torch.sum(input=my_tensor, dim=(-2, -1))
# tensor(28)

torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-2)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-2,))
# tensor([4, 6, 8, 10])

torch.sum(input=my_tensor, dim=1)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(1,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor([6, 22])

my_tensor = torch.tensor([[0., 1., 2., 3.], [4., 5., 6., 7.]])

torch.sum(input=my_tensor)
# tensor(28.)

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j],
                          [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]])
torch.sum(input=my_tensor)
# tensor(28.+0.j)

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.sum(input=my_tensor)
# tensor(4)
Enter fullscreen mode Exit fullscreen mode

prod() can get the one or more product values of a 0D or more D tensor from a 0D or more D tensor as shown below:

*Memos:

  • prod() can be used with torch or a tensor.
  • The 1st argument with torch or using a tensor is input(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int).
  • The 3rd argument with torch or the 2nd argument with a tensor is keepdim(Optional-Default:False-Type:bool) which keeps the dimension of the input tensor. *keepdim= must be used with dim=.
  • There is dtype argument(torch.dtype) (Optional) with torch. *Memos:
import torch

my_tensor = torch.tensor([0, 1, 2, 3])

torch.prod(input=my_tensor)
my_tensor.prod()
torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-1)
# tensor(0)

torch.prod(input=my_tensor, dim=0, keepdim=True)
# tensor([0])

my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]])

torch.prod(input=my_tensor)
# tensor(0)

torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-2)
# tensor([0, 5, 12, 21])

torch.prod(input=my_tensor, dim=1)
torch.prod(input=my_tensor, dim=-1)
# tensor([0, 840])

torch.prod(input=my_tensor, dim=0, keepdim=True)
# tensor([[0, 5, 12, 21]])

my_tensor = torch.tensor([[0., 1., 2., 3.],
                          [4., 5., 6., 7.]])
torch.prod(input=my_tensor, dtype=torch.float64)
torch.prod(input=my_tensor, dtype=float)
# tensor(0., dtype=torch.float64)

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j],
                          [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]])
torch.prod(input=my_tensor, dtype=torch.complex64)
# tensor(0.+0.j)

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.prod(input=my_tensor)
# tensor(0)

torch.prod(input=my_tensor, dtype=torch.bool)
# tensor(False)
Enter fullscreen mode Exit fullscreen mode

cartesian_prod() can do cartesian product with one or more 1D tensors as shown below:

*Memos:

  • cartesian_prod() can be used with torch but not with a tensor.
  • The 1st or more arguments(tensor of int, float, complex or bool) with torch are *tensors(Required at least one tensor). *Memos:
    • Don't use *tensors= or tensors= with torch.
    • Tensors must be the same type.
import torch

my_tensor = torch.tensor([0, 1, 2, 3])

torch.cartesian_prod(my_tensor)
# tensor([0, 1, 2, 3])

my_tensor = torch.tensor([0., 1., 2., 3.])

torch.cartesian_prod(my_tensor)
# tensor([0., 1., 2., 3.])

my_tensor = torch.tensor([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])

torch.cartesian_prod(my_tensor)
# tensor([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])

my_tensor = torch.tensor([True, False, True, False])

torch.cartesian_prod(my_tensor)
# tensor([True, False, True, False])

tensor1 = torch.tensor([0, 1, 2, 3])
tensor2 = torch.tensor([4, 5])

torch.cartesian_prod(tensor1, tensor2)
# tensor([[0, 4],
#         [0, 5],
#         [1, 4],
#         [1, 5],
#         [2, 4],
#         [2, 5],
#         [3, 4],
#         [3, 5]])

tensor1 = torch.tensor([0, 1, 2, 3])
tensor2 = torch.tensor([4, 5])
tensor3 = torch.tensor([6, 7, 8])

torch.cartesian_prod(tensor1, tensor2, tensor3)
# tensor([[0, 4, 6],
#         [0, 4, 7],
#         [0, 4, 8],
#         [0, 5, 6],
#         [0, 5, 7],
#         [0, 5, 8],
#         [1, 4, 6],
#         [1, 4, 7],
#         [1, 4, 8],
#         [1, 5, 6],
#         [1, 5, 7],
#         [1, 5, 8],
#         [2, 4, 6],
#         [2, 4, 7],
#         [2, 4, 8],
#         [2, 5, 6],
#         [2, 5, 7],
#         [2, 5, 8],
#         [3, 4, 6],
#         [3, 4, 7],
#         [3, 4, 8],
#         [3, 5, 6],
#         [3, 5, 7],
#         [3, 5, 8]])
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .