Calculates the sum of the elements of a matrix or a tensor. Notice that the sum of boolean elements is equivalent to logical OR operation.


  • input: A matrix or a tensor.

  • dimension: Specifies the dimension along which sum will be calculated. -1 means sum of all elements. For matrices, dimension 0 is rows (output will be 1-by-N) and 1 is columns (output will be N-by-1). For tensors, the specified dimension will be collapsed. For example, if the input is a 1×2×3×4 tensor and dimension is 1, the output will be a 1×3×4 tensor. If the size of the specified dimension is one, sum is equal to reshaping the tensor. If dimension is greater or equal to the number of dimensions in the input tensor or matrix, the output will be an empty tensor or matrix.


  • output: A matrix or a tensor of the same type as the input. If dimension is -1, the output will be a scalar value.