PyTorch torch.isinf 函数
torch.isinf 是 PyTorch 中用于检查是否是无穷值的函数。返回一个布尔张量,对于每个元素,如果是无穷大(正无穷或负无穷)则为True。
函数定义
torch.isinf(input)
使用示例
实例
import torch
# 创建包含不同数值的张量
x = torch.tensor([1.0, float('inf'), float('-inf'), 2.0])
# 检查是否是无穷值
result = torch.isinf(x)
print(result)
# 创建包含不同数值的张量
x = torch.tensor([1.0, float('inf'), float('-inf'), 2.0])
# 检查是否是无穷值
result = torch.isinf(x)
print(result)
输出结果为:
tensor([False, True, True, False])

Pytorch torch 参考手册