捷徑

torch.any

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

測試 input 中是否有任何元素評估為 True

注意

此函式符合 NumPy 的行為,即對於除 uint8 之外的所有支援的 dtype,都傳回 dtype 為 bool 的輸出。對於 uint8,輸出的 dtype 本身就是 uint8

範例

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

針對給定維度 diminput 的每一列,如果該列中任何元素的值為 True,則返回 True,否則返回 False

如果 keepdimTrue,則輸出張量的大小與 input 相同,除了維度 dim 的大小為 1。 否則,dim 將被壓縮 (squeeze) (請參閱 torch.squeeze()),導致輸出張量減少 1 個(或 len(dim))維度。

參數
  • input (Tensor) – 輸入張量。

  • dim (intints 的tuple) – 要減少的維度。

  • keepdim (bool) – 輸出張量是否保留 dim

關鍵字參數

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

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources