PyTorch torch.equal Function
Pytorch torch Reference Manual
torch.equalIt is a function in PyTorch used to check whether two tensors are equal. If the two tensors have the same shape and elements, it returns True.
Function Definition
torch.equal(input, other)
Usage Example
Example
import torch
# Create two identical tensors
x = torch.tensor([1, 2, 3])
y = torch.tensor([1, 2, 3])
# Check if they are equal
result = torch.equal(x, y)
print(result)
# Create different tensors
a = torch.tensor([1, 2, 4])
b = torch.tensor([1, 2, 3])
result2 = torch.equal(a, b)
print(result2)
# Create two identical tensors
x = torch.tensor([1, 2, 3])
y = torch.tensor([1, 2, 3])
# Check if they are equal
result = torch.equal(x, y)
print(result)
# Create different tensors
a = torch.tensor([1, 2, 4])
b = torch.tensor([1, 2, 3])
result2 = torch.equal(a, b)
print(result2)
The output result is:
True False
Other Extensions