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
对于给定维度
dim
中input
的每一行,如果该行中的任何元素的值为 True,则返回 True,否则返回 False。如果
keepdim
为True
,则输出张量的大小与input
相同,除了在维度dim
中大小为 1。否则,dim
将被压缩(参见torch.squeeze()
),从而使输出张量的维度减少 1 个(或len(dim)
个)。- 参数
- 关键字参数
out (Tensor, 可选) – 输出张量。
示例
>>> 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])