快捷方式

torch.all

torch.all(input: Tensor) Tensor

測試 input 中的所有元素是否都評估為 True

注意

此函式在返回輸出的 dtype 時,與 NumPy 的行為一致,對於除 uint8 之外的所有支援的 dtype,返回 bool 型別。對於 uint8,輸出的 dtype 仍為 uint8

示例

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

對於給定維度 diminput 的每一行,如果該行中的所有元素都評估為 True 則返回 True,否則返回 False

如果 keepdimTrue,則輸出張量的大小與 input 相同,但在維度 dim 處大小為 1。否則,dim 會被壓縮(參見 torch.squeeze()),導致輸出張量的維度減少 1 (或 len(dim))。

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

  • dim (int or tuple of ints) – 要縮減的維度或多個維度。

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

關鍵字引數

out (Tensor, optional) – 輸出張量。

示例

>>> a = torch.rand(4, 2).bool()
>>> a
tensor([[True, True],
        [True, False],
        [True, True],
        [True, True]], dtype=torch.bool)
>>> torch.all(a, dim=1)
tensor([ True, False,  True,  True], dtype=torch.bool)
>>> torch.all(a, dim=0)
tensor([ True, False], dtype=torch.bool)

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源