mindspore.ops.UnsortedSegmentSum

View Source On Gitee
class mindspore.ops.UnsortedSegmentSum[source]

Computes the sum of a tensor along segments.

Refer to mindspore.ops.unsorted_segment_sum() for more details.

Inputs:
  • input_x (Tensor) - Input Tensor contains the data to be summed. The shape is \((x_1, x_2, ..., x_R)\).

  • segment_ids (Tensor) - The label indicates the segment to which each element belongs. Set the shape as \((x_1, x_2, ..., x_N)\), where 0 < N <= R.

  • num_segments (int) - Set \(z\) as num_segments, it can be an int or 0-D Tensor.

Outputs:

Tensor, the shape is \((z, x_{N+1}, ..., x_R)\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor
>>> from mindspore import ops
>>> import mindspore
>>> input_x = Tensor([1, 2, 3, 4], mindspore.float32)
>>> segment_ids = Tensor([0, 0, 1, 2], mindspore.int32)
>>> num_segments = 4
>>> output = ops.UnsortedSegmentSum()(input_x, segment_ids, num_segments)
>>> print(output)
[3. 3. 4. 0.]
>>> input_x = Tensor([1, 2, 3, 4, 2, 5], mindspore.float32)
>>> segment_ids = Tensor([0, 0, 1, 2, 3, 4], mindspore.int32)
>>> num_segments = 6
>>> output = ops.UnsortedSegmentSum()(input_x, segment_ids, num_segments)
>>> print(output)
[3. 3. 4. 2. 5. 0.]