torch.all¶
- torch.all(input: Tensor) Tensor ¶
測試
input
中是否所有元素評估為 True。注意
此函式符合 NumPy 的行為,為所有支援的 dtype(除了 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
對於給定維度
dim
中input
的每一列,如果該列中的所有元素評估結果為 True,則返回 True,否則返回 False。如果
keepdim
為True
,則輸出張量的大小與input
相同,除了在維度dim
中其大小為 1。 否則,dim
將被壓縮(參見torch.squeeze()
),導致輸出張量的維度減少 1 個(或len(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)