YouTip LogoYouTip

Pytorch Torch Cumsum

# PyTorch torch.cumsum Function * * PyTorch torch Reference Manual](#) `torch.cumsum` is a PyTorch function for calculating cumulative sums. It returns the cumulative sum along a specified dimension, representing the sum of all elements from the start to the current position. ### Function Definition torch.cumsum(input, dim, dtype=None) * * * ## Example Usage ## Example import torch # Calculate cumulative sum x = torch.tensor([1,2,3,4,5]) result = torch.cumsum(x, dim=0) print("Input:", x) print("Cumulative sum:", result) # Output: tensor([1, 3, 6, 10, 15]) # Explanation: 1, 1+2=3, 1+2+3=6, 1+2+3+4=10, 1+2+3+4+5=15 # 2D tensor x = torch.tensor([[1,2,3],[4,5,6]], dtype=torch.float32) # Column-wise cumulative sum result_col = torch.cumsum(x, dim=0) print("nColumn-wise cumulative sum:") print(result_col) # tensor([[ 1, 2, 3], # [ 5, 7, 9]]) # Row-wise cumulative sum result_row = torch.cumsum(x, dim=1) print("nRow-wise cumulative sum:") print(result_row) # tensor([[ 1, 3, 6], # [ 4, 9, 15]]) * * PyTorch torch Reference Manual](#)
← Pytorch Torch Deg2RadPytorch Torch Cummin β†’