torch.is_nonzero¶
- torch.is_nonzero(input)¶
如果
input
是一个单元素张量,并且在类型转换后不等于零,则返回 True。即不等于torch.tensor([0.])
或torch.tensor([0])
或torch.tensor([False])
。如果torch.numel() != 1
(即使在稀疏张量的情况下),也会抛出RuntimeError
。- 参数
input (张量) – 输入张量。
示例
>>> torch.is_nonzero(torch.tensor([0.])) False >>> torch.is_nonzero(torch.tensor([1.5])) True >>> torch.is_nonzero(torch.tensor([False])) False >>> torch.is_nonzero(torch.tensor([3])) True >>> torch.is_nonzero(torch.tensor([1, 3, 5])) Traceback (most recent call last): ... RuntimeError: bool value of Tensor with more than one value is ambiguous >>> torch.is_nonzero(torch.tensor([])) Traceback (most recent call last): ... RuntimeError: bool value of Tensor with no values is ambiguous