How to Add Up the Values of a Tensor in PyTorch with sum()
Adding up values is one of the most common things you'll do with a tensor: a total, a total per row, a count of correct answers. sum() does all three, depending on which dimension you tell it to add along. This guide uses it on a small table of sales in PyTorch 2.15.0.
Syntax
tensor.sum(dim=None, keepdim=False)
| Part | What it means |
|---|---|
dim | Optional. The dimension to add along. Left out, every value is added into one total. For a table, dim=0 adds down each column and dim=1 along each row |
keepdim | Optional. True keeps the dimension that was added up, with a length of 1, so the result keeps the table's number of dimensions |
PyTorch's reference describes the version with dim as returning "the sum of each row of the input tensor in the given dimension dim." The dimension you give is the one that disappears from the result.
The Example
A table of sales: two shops, one per row, over three days, one per column. The script prints the total, the total per day and the total per shop.
Prerequisites
- A uv project with PyTorch and NumPy.
uv add torch numpyadds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.
Step 1: Write the Script
import torch
sales = torch.tensor([[3, 5, 2], [4, 1, 6]])
print(sales.sum())
print(sales.sum(dim=0))
print(sales.sum(dim=1))

The first shop sold 3, 5 and 2 over the three days, the second 4, 1 and 6.
Step 2: Run It
uv run python tensor_sum.py
tensor(21)
tensor([7, 6, 8])
tensor([10, 11])
![The terminal after uv run python tensor_sum.py: tensor(21), tensor([7, 6, 8]) and tensor([10, 11]).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/2026/pytorch-sum-02-output.webp)
sales.sum()added all six values into21, a tensor with no dimensions left.sales.sum(dim=0)added down each column, one total per day: 3 + 4 =7, 5 + 1 =6, 2 + 6 =8.sales.sum(dim=1)added along each row, one total per shop: 3 + 5 + 2 =10and 4 + 1 + 6 =11.
The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Python's own sum(sales) | It printed tensor([7, 6, 8]): it adds the rows together, like dim=0, rather than every value | sales.sum() for the total |
Mixing up dim=0 and dim=1 | dim=0 gives one total per column, not per row | Name the dimension that should disappear: the rows are dim=0 |
FAQ
How do I keep the result's shape for more calculations?
Add keepdim=True: sales.sum(dim=1, keepdim=True) printed tensor([[10], [11]]), a column of two rows rather than a flat row of two values.
How do I count the True values in a tensor?
Add them up: True counts as 1. torch.tensor([True, False, True]).sum() printed tensor(2).
How do I get the total as a Python number?
With item(): sales.sum().item() printed the plain 21. How to Get a Python Number from a PyTorch Tensor with item() shows it.
Conclusion
On a table of two shops over three days, sales.sum() gave the total 21, sales.sum(dim=0) the totals per day 7, 6, 8, and sales.sum(dim=1) the totals per shop 10, 11.
Additional Resources
- torch.sum — PyTorch's reference, with
dim,keepdimanddtype. - Tensor.sum — the method version this guide uses.
- Tensors tutorial — PyTorch's introduction to tensors, including adding up values.
