mindspore.ops.select
- mindspore.ops.select(condition, input, other)[源代码]
根据条件判断tensor中的元素的值,来决定输出中的相应元素是从 input (如果元素值为True)还是从 other (如果元素值为False)中选择。
该算法可以被定义为:
- 参数:
condition (Tensor[bool]) - 条件tensor。
input (Union[Tensor, int, float]) - 第一个被选择的tensor或数字。
other (Union[Tensor, int, float]) - 第二个被选择的tensor或数字。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> # case1: Both `input` and `other` are tensor >>> cond = mindspore.tensor([True, False]) >>> x = mindspore.tensor([2,3], mindspore.float32) >>> y = mindspore.tensor([1,2], mindspore.float32) >>> output1 = mindspore.ops.select(cond, x, y) >>> print(output1) [2. 2.] >>> # case2: Both `input` and `other` are number >>> output2 = mindspore.ops.select(cond, input=1, other=2) >>> print(output2) [1 2] >>> # case3: `input` is tensor and `other` is number >>> output3 = mindspore.ops.select(cond, x, other=3) >>> print(output3) [2. 3.]