chainer.functions.sum

chainer.functions.sum(x, axis=None, keepdims=False)[source]

Sum of array elements over a given axis.

Parameters:
  • x (Variable or numpy.ndarray or cupy.ndarray) – Elements to sum. A \((s_1, s_2, ..., s_N)\) -shaped float array.
  • axis (None, int, or tuple of int) – Axis along which a sum is performed. The default (axis = None) is perform a sum over all the dimensions of the input array.
  • keepdims (bool) – If True, the specified axes are remained as axes of length one.
Returns:

Output variable.

Return type:

Variable

Example

>>> x = np.arange(6).reshape(2,3).astype('f')
>>> x
array([[0., 1., 2.],
       [3., 4., 5.]], dtype=float32)
>>> y = F.sum(x)
>>> y.shape
()
>>> y.data
array(15., dtype=float32)
>>> y = F.sum(x, axis=1)
>>> y.shape
(2,)
>>> y.data
array([ 3., 12.], dtype=float32)
>>> y = F.sum(x, keepdims=True)
>>> y.shape
(1, 1)
>>> y.data
array([[15.]], dtype=float32)