评价此页

torch.any#

torch.any(input: Tensor, *, out: Optional[Tensor]) Tensor#

测试 input 中的任何元素是否评估为 True

注意

此函数与 NumPy 的行为相匹配,即对于所有支持的 dtype(uint8 除外),返回 bool 类型的输出。对于 uint8,输出的 dtype 是 uint8 本身。

参数

input (Tensor) – 输入张量。

关键字参数

out (Tensor, optional) – 输出张量。

示例

>>> a = torch.rand(1, 2).bool()
>>> a
tensor([[False, True]], dtype=torch.bool)
>>> torch.any(a)
tensor(True, dtype=torch.bool)
>>> a = torch.arange(0, 3)
>>> a
tensor([0, 1, 2])
>>> torch.any(a)
tensor(True)
torch.any(input, dim, keepdim=False, *, out=None) Tensor

对于给定维度 dim 中的 input 的每一行,如果该行中的任何元素评估为 True,则返回 True,否则返回 False

如果 keepdimTrue,则输出张量的大小与 input 相同,只有在 dim 维度上大小为 1。否则,dim 将被挤压(参见 torch.squeeze()),导致输出张量维度减少 1(或 len(dim))个。

参数
  • input (Tensor) – 输入张量。

  • dim (inttuple of ints, optional) – 要规约的维度或维度。如果为 None,则规约所有维度。

  • keepdim (bool, optional) – 输出张量是否保留 dim。默认为 False

关键字参数

out (Tensor, optional) – 输出张量。

示例

>>> a = torch.randn(4, 2) < 0
>>> a
tensor([[ True,  True],
        [False,  True],
        [ True,  True],
        [False, False]])
>>> torch.any(a, 1)
tensor([ True,  True,  True, False])
>>> torch.any(a, 0)
tensor([True, True])