PyTorch torch.allclose 函数
torch.allclose 是 PyTorch 中用于检查两个张量是否所有元素都接近的函数。它用于判断两个张量在给定容差范围内是否相等。
函数定义
torch.allclose(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.allclose(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.allclose(x, y)
print(result)
输出结果为:
True

Pytorch torch 参考手册