mindspore.ops.accumulate_n

View Source On Gitee
mindspore.ops.accumulate_n(x)[source]

Computes accumulation of all input tensors element-wise.

mindspore.ops.accumulate_n() is similar to mindspore.ops.addn(), but there is a significant difference between them: accumulate_n will not wait for all of its inputs to be ready before summing. That is to say, accumulate_n is able to save memory when inputs are ready at different time since the minimum temporary storage is proportional to the output size rather than the input size.

Parameters

x (Union(tuple[Tensor], list[Tensor])) – The input tuple or list is made up of multiple tensors whose dtype is number to be added together. Each element of tuple or list should have the same shape.

Returns

Tensor, has the same shape and dtype as each entry of x.

Raises
  • TypeError – If x is neither tuple nor list.

  • ValueError – If there is an input element with a different shape.

Supported Platforms:

Ascend GPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> y = Tensor(np.array([4, 5, 6]), mindspore.float32)
>>> output = ops.accumulate_n([x, y, x, y])
>>> print(output)
[10. 14. 18.]