torch.any#
- torch.any(input: Tensor, *, out: Optional[Tensor]) Tensor #
测试
input
中的任何元素是否求值为 True。注意
此函数在所有支持的数据类型(uint8 除外)中匹配 NumPy 的行为,返回 bool 类型的输出。对于 uint8,输出的数据类型是 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, 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])