April 13, 2024, 10:05 p.m. | Super Kai (Kazuya Ito)

DEV Community dev.to

sum() can get one or more sum values as shown below:


*Memos:




  • sum() can be called both from torch and a tensor.

  • The 2nd argument is one or more dimensions with torch.

  • The 1st argument is one or more dimensions with a tensor.



import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.sum(my_tensor)
my_tensor.sum()
# tensor(65)

torch.sum(my_tensor, 0)
my_tensor.sum(0)
torch.sum(my_tensor, (0,))
my_tensor.sum((0,))
torch.sum(my_tensor, -2)
my_tensor.sum(-2)
torch.sum(my_tensor, (-2,))
my_tensor.sum((-2,))
# tensor([14, 17, 19, 15]) …

dimensions function import memos prod pytorch sum tensor torch values

Data Architect

@ University of Texas at Austin | Austin, TX

Data ETL Engineer

@ University of Texas at Austin | Austin, TX

Lead GNSS Data Scientist

@ Lurra Systems | Melbourne

Senior Machine Learning Engineer (MLOps)

@ Promaton | Remote, Europe

Sr. VBI Developer II

@ Atos | Texas, US, 75093

Wealth Management - Data Analytics Intern/Co-op Fall 2024

@ Scotiabank | Toronto, ON, CA