This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*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 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
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Required-Type:int
). - There is
dtype
argument withtorch
(Optional-Type:dtype): *Memos:- If
dtype
is not given, it is inferred frominput
or a tensor. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
out
argument withtorch
(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 withtorch
or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Required-Type:int
). - There is
dtype
argument withtorch
(Optional-Type:dtype): *Memos:- If
dtype
is not given, it is inferred frominput
or a tensor. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
out
argument withtorch
(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)
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.