mindspore.ops.sparse_segment_mean

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

Computes a Tensor such that \(output_i = \frac{\sum_j x_{indices[j]}}{N}\) where mean is over \(j\) such that \(segment\_ids[j] == i\) and \(N\) is the total number of values summed. If the mean is empty for a given segment ID \(i\), \(output[i] = 0\).

Note

  • On CPU, values in segment_ids are always validated to be sorted, and an error is thrown for indices that are not increasing. Moreover, values in indices are validated to be bounded, and an error is thrown when indices are out of range[0, x.shape[0]).

  • On GPU, this does not throw an error for unsorted segment_ids and out-of-bound indices. Out-of-order segment_ids result in safe but unspecified behavior, while out-of-range indices will be ignored.

Parameters
  • x (Tensor) – A Tensor, and its rank must be greater than or equal to 1.

  • indices (Tensor) – A 1-D Tensor, with int32 or int64 data type.

  • segment_ids (Tensor) – A 1-D Tensor, must have the same dtype as indices. Values should be sorted and can be repeated.

Returns

Tensor, whose dtype and rank is the same as x. The first dimension is equal to the value of the last element of segment_ids plus one, and the other dimensions are the same as those of x.

Raises
  • TypeError – If x, indices or segment_ids is not a Tensor.

  • TypeError – If the dtype of x is not one of the following dtype: float16, float32, float64.

  • TypeError – If the dtype of indices and segment_ids are not one of the following dtype: int32, int64.

  • TypeError – If the dtype of indices and segment_ids are not the same.

  • ValueError – If the shape of x, indices or segment_ids don't meet the parameter description.

  • ValueError – If the size of indices and segment_ids are not the same.

Supported Platforms:

GPU CPU

Examples

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