mindspore.ops.any

mindspore.ops.any(input, axis=None, keep_dims=False)[源代码]

默认情况下,通过对维度中所有元素进行“逻辑或”来减少 input 的维度。也可以沿轴减少 input 的维度。通过控制 keep_dim 来确定输出和输入的维度是否相同。

参数:
  • input (Tensor) - 输入Tensor,shape是 \((N, *)\) ,其中 \(*\) 表示任意数量的附加维度。

  • axis (Union[int, tuple(int), list(int)], 可选) - 要减少的维度。只允许常量值。假设 input 的秩为r,取值范围[-r,r)。默认值:None,缩小所有维度。

  • keep_dims (bool, 可选) - 如果为True,则保留缩小的维度,大小为1。否则移除维度。默认值:False。

返回:

Tensor,数据类型是bool。

  • 如果 axis 为None,且 keep_dims 为False,则输出一个零维Tensor,表示输入Tensor中所有元素进行“逻辑或”。

  • 如果 axis 为int,例如取值为2,并且 keep_dims 为False,则输出的shape为 \((input_1, input_3, ..., input_R)\)

  • 如果 axis 为tuple(int)或list(int),例如取值为(2, 3),并且 keep_dims 为False,则输出Tensor的shape为 \((input_1, input_4, ..., input_R)\)

异常:
  • TypeError - keep_dims 不是bool类型。

  • TypeError - input 不是Tensor。

  • TypeError - axis 不是以下数据类型之一:int、tuple或list。

支持平台:

Ascend GPU CPU

样例:

>>> x = Tensor(np.array([[True, False], [True, True]]))
>>> # case 1: Reduces a dimension by the "logical OR" of all elements in the dimension.
>>> output = ops.any(x, keep_dims=True)
>>> print(output)
[[ True]]
>>> print(output.shape)
(1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = ops.any(x, axis=0)
>>> print(output)
[ True True]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = ops.any(x, axis=1)
>>> print(output)
[ True True]