TUTORIALAI & dataFETCHED

PyTorch torch.equal 函数

PyTorch torch.equal 函数 Pytorch torch 参考手册 torch.equal 是 PyTorch 中用于检查两个张量是否相等的函数。如果两个张量具有相同的形状和元素,则返回True。 函数定义 torch.equal(input, other) 使用示例 实例 [mycode4 type='python'] import torch # 创建两个相同的张量 x = ..

Updated 7/16/2026

Tutorial detail pages default to noindex.

PyTorch torch.equal 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.equal 是 PyTorch 中用于检查两个张量是否相等的函数。如果两个张量具有相同的形状和元素,则返回True。

函数定义

torch.equal(input, other)

使用示例

实例

import torch

# 创建两个相同的张量
x = torch.tensor([1, 2, 3])
y = torch.tensor([1, 2, 3])

# 检查是否相等
result = torch.equal(x, y)
print(result)

# 创建不同的张量
a = torch.tensor([1, 2, 4])
b = torch.tensor([1, 2, 3])
result2 = torch.equal(a, b)
print(result2)

输出结果为:

True
False

Pytorch torch 参考手册 Pytorch torch 参考手册