sum

dragon.vm.torch.sum(
  input,
  dim=None,
  keepdim=False,
  out=None
)[source]

Compute the sum value of elements along the given dimension.

dim could be negative or None:

x = torch.tensor([[1, 2, 3], [4, 5, 6]])

# A negative dimension is the last-k dimension
print(torch.sum(x, dim=1))
print(torch.sum(x, dim=-1))  # Equivalent

# If dimension is None, reduce input as a vector
# and return a scalar result
print(torch.sum(x))  # 21

# Also, dimension could be a sequence of integers
print(torch.sum(x, [0, 1]))  # 21
Parameters:
  • input (dragon.vm.torch.Tensor) – The input tensor.
  • dim (Union[int, Sequence[int]], optional) – The dimension to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimension or not.
  • out (dragon.vm.torch.Tensor, optional) – The output tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.