Graph.sum(input_tensor, axis=None, keepdims=False, *, name=None)

Sum the elements in a tensor (or a list of tensors with the same shape) along one or multiple of its axes.


  • input_tensor (np.ndarray or Tensor or list [Tensor ]) – The tensor whose elements you want to sum. If you pass a list of tensors, they must all have the same shape, and are interpreted as being stacked along a new first dimension (for example, if you pass two 2D tensors of shape (3, 4), the result is equivalent to passing the stacked 3D tensor of shape (2, 3, 4)).
  • axis (int or list [ int ] or None , optional) – The dimension or dimensions along which you want to sum the tensor. Defaults to None, in which case this node sums along all axes of the tensor.
  • keepdims (bool , optional) – Whether or not to retain summed axes in the output tensor. If True, each dimension in axis has size 1 in the result; otherwise, the dimensions in axis are removed from the result. Defaults to False.
  • name (str or None , optional) – The name of the node.


The tensor obtained by summing the input tensor along the specified axes (or, if axis was None, the tensor obtained by summing the input tensor along all of the specified axes).

Return type



Graph.einsum : Tensor contraction via Einstein summation convention.


Sum elements of an array.

>>> a = np.array([1, 2, 3])
>>> graph.sum(a, 0, name="sum_a")
<Tensor: name="sum_a", operation_name="sum", shape=()>
>>> result = bo.execute_graph(graph=graph, output_node_names="sum_a")
>>> result["output"]["sum_a"]["value"]

Sum elements of a 2D array along its first dimension.

>>> b = np.array([[1, 2, 3], [4, 5, 6]])
>>> graph.sum(b, 0, name="sum_b")
<Tensor: name="sum_b", operation_name="sum", shape=(3,)>
>>> result = bo.execute_graph(graph=graph, output_node_names="sum_b")
>>> result["output"]["sum_b"]["value"]
array([5, 7, 9])

Was this useful?