DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

sum and nansum in PyTorch

Buy Me a Coffee

*Memos:

sum() can get the 0D or more D tensor of zero or more sum's elements, normally treating one or more NaNs(Not a Numbers) 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(input) with torch or using a tensor(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): *Memos:
    • keepdim= must be used with dim=.
    • My post explains keepdim argument.
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input or a tensor.
    • dtype= must be used.
    • My post explains dtype argument.
  • Normally, the arithmetic operation with a NaN results in a NaN.
  • The empty 1D or more D input tensor or tensor without dim or with the deepest dim gets a zero.
import torch

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

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(10)

my_tensor = torch.tensor([1, 2, torch.nan, 4])

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

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

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(36)

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([6, 8, 10, 12])

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([10, 26])

my_tensor = torch.tensor([[1, 2, torch.nan, 4], [torch.nan, 6, 7, 8]])

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

torch.sum(input=my_tensor, dim=0)
# tensor([nan, 8., nan, 12.])

torch.sum(input=my_tensor, dim=1)
# tensor([nan, nan])

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

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

my_tensor = torch.tensor([[1, 2, torch.nan, 4], [torch.nan, 6, 7, 8]])

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

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

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

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

my_tensor = torch.tensor([])

torch.sum(input=my_tensor)
# tensor(0.)
Enter fullscreen mode Exit fullscreen mode

nansum() can get the 0D or more D tensor of zero or more sum's elements, treating nan as zero from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • nansum() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float 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): *Memos:
    • keepdim= must be used with dim=.
    • My post explains keepdim argument.
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input or a tensor.
    • dtype= must be used.
    • My post explains dtype argument.
  • Normally, the arithmetic operation with a NaN results in a NaN.
  • The empty 1D or more D input tensor or tensor without dim or with the deepest dim gets a zero.
import torch

my_tensor = torch.tensor([1., 2., torch.nan, 4.])

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

my_tensor = torch.tensor([[1., 2., torch.nan, 4.],
                          [torch.nan, 6., 7., 8.]])
torch.nansum(input=my_tensor)
torch.nansum(input=my_tensor, dim=(0, 1))
torch.nansum(input=my_tensor, dim=(0, -1))
torch.nansum(input=my_tensor, dim=(1, 0))
torch.nansum(input=my_tensor, dim=(1, -2))
torch.nansum(input=my_tensor, dim=(-1, 0))
torch.nansum(input=my_tensor, dim=(-1, -2))
torch.nansum(input=my_tensor, dim=(-2, 1))
torch.nansum(input=my_tensor, dim=(-2, -1))
# tensor(28.)

torch.nansum(input=my_tensor, dim=0)
torch.nansum(input=my_tensor, dim=-2)
torch.nansum(input=my_tensor, dim=(0,))
torch.nansum(input=my_tensor, dim=(-2,))
# tensor([1., 8., 7., 12.])

torch.nansum(input=my_tensor, dim=1)
torch.nansum(input=my_tensor, dim=-1)
torch.nansum(input=my_tensor, dim=(1,))
torch.nansum(input=my_tensor, dim=(-1,))
# tensor([7., 21.])

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

torch.nansum(input=my_tensor)
# tensor(36)

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.nansum(input=my_tensor)
# tensor(4)

my_tensor = torch.tensor([])

torch.nansum(input=my_tensor)
# tensor(0.)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)