mindspore.ops.sparse_segment_mean

mindspore.ops.sparse_segment_mean(x, indices, segment_ids)[source]

Computes the mean of sparse segments in the input tensor.

\[output_i = \frac{\sum_j x_{indices[j]}}{N}\]

where N is the number of elements where \(segment\_ids[j] == i\) . If segment_ids doesn't contain i, then \(output[i] = 0\) .

Note

  • On CPU, values in segment_ids must be sorted and indices must be within range[0, x.shape[0]).

  • On GPU, unsorted segment_ids may result in undefined but safe behavior.Out-of-range indices will be ignored.

Parameters
  • x (Tensor) – The input tensor with at least one dimension.

  • indices (Tensor) – The specified indices, a 1-D tensor.

  • segment_ids (Tensor) – A 1-D tensor, must be sorted and can contain duplicates.

Returns

Tensor

Supported Platforms:

GPU CPU

Examples

>>> import mindspore
>>> x = mindspore.tensor([[0, 1, 2], [1, 2, 3], [3, 6, 7]], dtype=mindspore.float32)
>>> indices = mindspore.tensor([0, 1, 2], dtype=mindspore.int32)
>>> segment_ids = mindspore.tensor([1,2,2], dtype=mindspore.int32)
>>> out = mindspore.ops.sparse_segment_mean(x, indices, segment_ids)
>>> print(out)
[[0. 0. 0.]
 [0. 1. 2.]
 [2. 4. 5.]]