sum¶
Documentation¶
-
treetensor.torch.
sum
(input, *args, reduce=None, **kwargs)[source]¶ In
treetensor
, you can get thesum
result of a whole tree with this function.Example:
>>> import torch >>> import treetensor.torch as ttorch >>> ttorch.sum(torch.tensor([1.0, 2.0, 1.5])) # the same as torch.sum tensor(4.5000) >>> ttorch.sum(ttorch.tensor({ ... 'a': [1.0, 2.0, 1.5], ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, ... })) tensor(11.) >>> ttorch.sum(ttorch.tensor({ ... 'a': [1.0, 2.0, 1.5], ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, ... }), reduce=False) <Tensor 0x7fd45b534898> ├── a --> tensor(4.5000) └── b --> <Tensor 0x7fd45b5344e0> └── x --> tensor(6.5000) >>> ttorch.sum(ttorch.tensor({ ... 'a': [1.0, 2.0, 1.5], ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, ... }), dim=0) <Tensor 0x7f3640703128> ├── a --> tensor(4.5000) └── b --> <Tensor 0x7f3640703080> └── x --> tensor([3.1000, 3.4000])
Torch Version Related
This documentation is based on torch.sum in torch v2.4.1+cu121. Its arguments’ arrangements depend on the version of pytorch you installed.
If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.
1 | python -c 'import torch;print(torch.__version__)' |
The arguments and keyword arguments supported in torch v2.4.1+cu121 is listed below.
Description From Torch v2.4.1+cu121¶
-
torch.
sum
(input, *, dtype=None) → Tensor¶ Returns the sum of all elements in the
input
tensor.- Args:
input (Tensor): the input tensor.
- Keyword args:
- dtype (
torch.dtype
, optional): the desired data type of returned tensor. If specified, the input tensor is casted to
dtype
before the operation is performed. This is useful for preventing data type overflows. Default: None.
- dtype (
Example:
>>> a = torch.randn(1, 3) >>> a tensor([[ 0.1133, -0.9567, 0.2958]]) >>> torch.sum(a) tensor(-0.5475)
-
torch.
sum
(input, dim, keepdim=False, *, dtype=None) → Tensor
Returns the sum of each row of the
input
tensor in the given dimensiondim
. Ifdim
is a list of dimensions, reduce over all of them.If
keepdim
isTrue
, the output tensor is of the same size asinput
except in the dimension(s)dim
where it is of size 1. Otherwise,dim
is squeezed (seetorch.squeeze()
), resulting in the output tensor having 1 (orlen(dim)
) fewer dimension(s).- Args:
input (Tensor): the input tensor. dim (int or tuple of ints): the dimension or dimensions to reduce. keepdim (bool): whether the output tensor has
dim
retained or not.- Keyword args:
- dtype (
torch.dtype
, optional): the desired data type of returned tensor. If specified, the input tensor is casted to
dtype
before the operation is performed. This is useful for preventing data type overflows. Default: None.
- dtype (
Example:
>>> a = torch.randn(4, 4) >>> a tensor([[ 0.0569, -0.2475, 0.0737, -0.3429], [-0.2993, 0.9138, 0.9337, -1.6864], [ 0.1132, 0.7892, -0.1003, 0.5688], [ 0.3637, -0.9906, -0.4752, -1.5197]]) >>> torch.sum(a, 1) tensor([-0.4598, -0.1381, 1.3708, -2.6217]) >>> b = torch.arange(4 * 5 * 6).view(4, 5, 6) >>> torch.sum(b, (2, 1)) tensor([ 435., 1335., 2235., 3135.])