DEV Community

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

Posted on

cumsum() and cumprod() in PyTorch

Buy Me a Coffee

*Memos:

cumsum() can get the 0D or more D tensor of zero or more cumulative sum's elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • cumsum() 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(Required-Type:int).
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, it is inferred from input or a tensor.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

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

torch.cumsum(input=my_tensor, dim=0)
my_tensor.cumsum(dim=0)
torch.cumsum(input=my_tensor, dim=-1)
# tensor([1, 3, 6, 10])

my_tensor = torch.tensor([[1, 2, 3, 4],
                          [5, 6, 7, 8]])
torch.cumsum(input=my_tensor, dim=0)
torch.cumsum(input=my_tensor, dim=-2)
# tensor([[1, 2, 3, 4],
#         [6, 8, 10, 12]])

torch.cumsum(input=my_tensor, dim=1)
torch.cumsum(input=my_tensor, dim=-1)
# tensor([[1, 3, 6, 10],
#         [5, 11, 18, 26]])

my_tensor = torch.tensor([[[1, 2], [3, 4]],
                          [[5, 6], [7, 8]]])
torch.cumsum(input=my_tensor, dim=0)
torch.cumsum(input=my_tensor, dim=-3)
# tensor([[[1, 2], [3, 4]],
#         [[6, 8], [10, 12]]])

torch.cumsum(input=my_tensor, dim=1)
torch.cumsum(input=my_tensor, dim=-2)
# tensor([[[1, 2], [4, 6]],
#         [[5, 6], [12, 14]]])

torch.cumsum(input=my_tensor, dim=2)
torch.cumsum(input=my_tensor, dim=-1)
# tensor([[[1, 3], [3, 7]],
#         [[5, 11], [7, 15]]])

my_tensor = torch.tensor([[[1., 2.], [3., 4.]],
                          [[5., 6.], [7., 8.]]])
torch.cumsum(input=my_tensor, dim=0)
# tensor([[[1., 2.], [3., 4.]],
#         [[6., 8.], [10., 12.]]])

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.cumsum(input=my_tensor, dim=0)
# tensor([[[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]],
#         [[6.+0.j, 8.+0.j], [10.+0.j, 12.+0.j]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.cumsum(input=my_tensor, dim=0)
# tensor([[[1, 0], [1, 0]],
#         [[1, 1], [1, 1]]])
Enter fullscreen mode Exit fullscreen mode

cumprod() can get the 0D or more D tensor of zero or more cumulative product's elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • cumprod() 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(Required-Type:int).
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, it is inferred from input or a tensor.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

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

torch.cumprod(input=my_tensor, dim=0)
my_tensor.cumprod(dim=0)
torch.cumprod(input=my_tensor, dim=-1)
# tensor([1, 2, 6, 24])

my_tensor = torch.tensor([[1, 2, 3, 4],
                          [5, 6, 7, 8]])
torch.cumprod(input=my_tensor, dim=0)
torch.cumprod(input=my_tensor, dim=-2)
# tensor([[1, 2, 3, 4],
#         [5, 12, 21, 32]])

torch.cumprod(input=my_tensor, dim=1)
torch.cumprod(input=my_tensor, dim=-1)
# tensor([[1, 2, 6, 24],
#         [5, 30, 210, 1680]])

my_tensor = torch.tensor([[[1, 2], [3, 4]],
                          [[5, 6], [7, 8]]])
torch.cumprod(input=my_tensor, dim=0)
torch.cumprod(input=my_tensor, dim=-3)
# tensor([[[1, 2], [3, 4]],
#         [[5, 12], [21, 32]]])

torch.cumprod(input=my_tensor, dim=1)
torch.cumprod(input=my_tensor, dim=-2)
# tensor([[[1, 2], [3, 8]],
#         [[5, 6], [35, 48]]])

torch.cumprod(input=my_tensor, dim=2)
torch.cumprod(input=my_tensor, dim=-1)
# tensor([[[1, 2], [3, 12]],
#         [[5, 30], [7, 56]]])

my_tensor = torch.tensor([[[1., 2.], [3., 4.]],
                          [[5., 6.], [7., 8.]]])
torch.cumprod(input=my_tensor, dim=0)
# tensor([[[1., 2.], [3., 4.]],
#         [[5., 12.], [21., 32.]]])

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.cumprod(input=my_tensor, dim=0)
# tensor([[[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]],
#         [[ 5.+0.j, 12.+0.j], [21.+0.j, 32.+0.j]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.cumprod(input=my_tensor, dim=0)
# tensor([[[1, 0], [1, 0]],
#         [[0, 0], [0, 0]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)