Global reduction
Aggregate every value in a tensor into one scalar-like tensor.
PyTorch · Function
Returns the sum of all elements in a tensor, or reduces selected dimension(s). Use keepdim to retain reduced axes and dtype to control the accumulation and result type.
torch.sum reduces tensor values by addition. Without dim it produces one tensor containing the total; with dim it reduces the specified dimension or dimensions, optionally preserving reduced axes with keepdim, and dtype can control the computation and result type.
torch.sum(input, *, dtype=None) → Tensor
torch.sum(input, dim, keepdim=False, *, dtype=None) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | Tensor to reduce. |
dim | int or tuple/list of ints, optional | Dimension or dimensions over which to sum. A list reduces all listed dimensions. |
keepdim | bool, optional | Whether reduced dimensions remain as size-one axes. |
dtype | torch.dtype, optional | Desired data type for the returned tensor; the input is cast before reduction. |
A Tensor containing the total sum or the sums along the selected dimension(s).
import torch
x = torch.arange(24, dtype=torch.float32).reshape(2, 3, 4)
result = torch.sum(x, dim=1, keepdim=True)
print(result.shape) # torch.Size([2, 1, 4])This sums the second dimension while retaining it as a size-one dimension.
Aggregate every value in a tensor into one scalar-like tensor.
Compute per-feature, per-row, or per-batch totals along selected axes.
Keep tensor rank compatible with later broadcasting operations.
Use a wider or specific type when summing integer data.
With no dim, torch.sum returns the sum of every element in the input tensor. With dim, it reduces only the selected dimension or dimensions.
Follow-up: How can multiple axes be reduced? Pass dim as a list of dimensions, such as [1, 2].
keepdim=True leaves each reduced axis in the result with length one. With the default keepdim=False, those axes are removed.
Follow-up: Why retain singleton dimensions? They preserve rank and make broadcasting with the original shape easier.
dtype specifies the desired type of the returned tensor, and the input is cast to that type before summation. This can help prevent overflow and enforce a required result type.
Follow-up: What happens when dtype is omitted? The result type can be automatically promoted, for example from int32 to int64.
torch.Tensor.sumtorch.squeezetorch.cumsum