cumsum() and cumprod() in PyTorch

Buy Me a Coffee☕

*Memos:

My post explains prod() and cartesian_prod().

My post explains mean() and nanmean().

My post explains median() and nanmedian().

My post explains cummin() and cummax().

cumsum() can get the 0D or more D tensor of zero o…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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]]])

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]]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-20T13:52:07+00:00) cumsum() and cumprod() in PyTorch. Retrieved from https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/

MLA
" » cumsum() and cumprod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Saturday July 20, 2024, https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Saturday July 20, 2024 » cumsum() and cumprod() in PyTorch., viewed ,<https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » cumsum() and cumprod() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/
CHICAGO
" » cumsum() and cumprod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/
IEEE
" » cumsum() and cumprod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/. [Accessed: ]
rf:citation
» cumsum() and cumprod() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/20/cumsum-and-cumprod-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.