sum(input, dim=None, keepdim=False) → Tensor¶
Computes the sum of row of elements in a tensor in the given dimension. If the dimension is None, sum of all elements will be caculated.
If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed
oneflow.squeeze(), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
input (oneflow.Tensor) – the Input Tensor
dim (int or tuple of ints, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) >>> flow.sum(input) tensor(21., dtype=oneflow.float32) >>> flow.sum(input, dim=0) tensor([5., 7., 9.], dtype=oneflow.float32) >>> flow.sum(input, dim=1) tensor([ 6., 15.], dtype=oneflow.float32)