PyTorch torch.isclose 函数
torch.isclose 是 PyTorch 中用于检查两个张量是否接近的函数。返回一个布尔张量,表示两个张量中对应元素是否在给定容差范围内接近。
函数定义
torch.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False)
使用示例
实例
import torch
# 创建两个相近的张量
x = torch.tensor([1.0, 2.0, 3.0])
y = torch.tensor([1.0, 2.0, 3.0 + 1e-9])
# 检查对应元素是否接近
result = torch.isclose(x, y)
print(result)
# 创建两个相近的张量
x = torch.tensor([1.0, 2.0, 3.0])
y = torch.tensor([1.0, 2.0, 3.0 + 1e-9])
# 检查对应元素是否接近
result = torch.isclose(x, y)
print(result)
输出结果为:
tensor([True, True, True])

Pytorch torch 参考手册