mindspore.ops.UnsortedSegmentMax
- class mindspore.ops.UnsortedSegmentMax[源代码]
沿分段计算输入Tensor的最大值。
更多参考详见
mindspore.ops.unsorted_segment_max()
。- 输入:
input_x (Tensor) - shape: \((x_1, x_2, ..., x_R)\) 。 数据类型支持float16、float32或int32。
segment_ids (Tensor) - shape为 \((x_1)\) 的一维张量,值必须是非负数。 数据类型支持int32。
num_segments (int) - 分段的数量。
- 输出:
Tensor,若 num_segments 值为 N ,则shape为 \((N, x_2, ..., x_R)\) 。
- 支持平台:
Ascend
GPU
CPU
样例:
>>> # case 1: Only have two num_segments, where is 0 and 1, and segment_ids=[0, 1, 1] >>> # num_segments = 2 indicates that there are two types of segment_id, >>> # the first number '0' in [0, 1, 1] indicates input_x[0], >>> # the second number '1' in [0, 1, 1] indicates input_x[1], >>> # the third number '1' in [0, 1, 1] indicates input_x[2], >>> # input_x[0], which is [1, 2, 3] will not be compared to other segment_id. >>> # Only the same segment_id will be compared. >>> from mindspore import Tensor >>> from mindspore import ops >>> import numpy as np >>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [4, 2, 1]]).astype(np.float32)) >>> segment_ids = Tensor(np.array([0, 1, 1]).astype(np.int32)) >>> num_segments = 2 >>> unsorted_segment_max = ops.UnsortedSegmentMax() >>> output = unsorted_segment_max(input_x, segment_ids, num_segments) >>> print(output) [[1. 2. 3.] [4. 5. 6.]] >>> >>> # case 2: The segment_ids=[0, 0, 1, 1]. >>> # [1, 2, 3] will compare with [4, 2, 0], >>> # and [4, 5, 6] will compare with [4, 2, 1]. >>> input_x = Tensor(np.array([[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]]).astype(np.float32)) >>> segment_ids = Tensor(np.array([0, 0, 1, 1]).astype(np.int32)) >>> num_segments = 2 >>> unsorted_segment_max = ops.UnsortedSegmentMax() >>> output = unsorted_segment_max(input_x, segment_ids, num_segments) >>> print(input_x.shape) (4, 3) >>> print(output) [[4. 2. 3.] [4. 5. 6.]] >>> # case 3: If the input_x have three dimensions even more, what will happen? >>> # The shape of input_x is (2, 4, 3), >>> # and the length of segment_ids should be the same as the first dimension of input_x. >>> # Because the segment_ids are different, input_x[0] will not be compared to input_x[1]. >>> input_x = Tensor(np.array([[[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]], ... [[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]]]).astype(np.float32)) >>> segment_ids = Tensor(np.array([0, 1]).astype(np.int32)) >>> num_segments = 2 >>> unsorted_segment_max = ops.UnsortedSegmentMax() >>> output = unsorted_segment_max(input_x, segment_ids, num_segments) >>> print(input_x.shape) (2, 4, 3) >>> print(output) [[[1. 2. 3.] [4. 2. 0.] [4. 5. 6.] [4. 2. 1.]] [[1. 2. 3.] [4. 2. 0.] [4. 5. 6.] [4. 2. 1.]]] >>> # case 4: It has the same input with the 3rd case. >>> # Because num_segments is equal to 2, there are two segment_ids, but currently only one 0 is used. >>> # the segment_id i is absent in the segment_ids, then output[i] will be filled with >>> # the smallest possible value of the input_x's type. >>> segment_ids = Tensor(np.array([0, 0]).astype(np.int32)) >>> output = unsorted_segment_max(input_x, segment_ids, num_segments) >>> print(output) [[[ 1.0000000e+00 2.0000000e+00 3.0000000e+00] [ 4.0000000e+00 2.0000000e+00 0.0000000e+00] [ 4.0000000e+00 5.0000000e+00 6.0000000e+00] [ 4.0000000e+00 2.0000000e+00 1.0000000e+00]] [[-3.4028235e+38 -3.4028235e+38 -3.4028235e+38] [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38] [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38] [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38]]]