torch.all¶
- torch.all(input: 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.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, 可选) – 输出张量。
示例
>>> 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)