評價此頁

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])