Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.equal(): Check If Two Tensors are Equal

Home torch.equal(): Check If Two Tensors are Equal
PyTorch torch.equal() method to compare tensors
  • Written by krunallathiya21
  • July 22, 2025
  • 0 Com
PyTorch

The torch.equal() method checks if two tensors are exactly equal in terms of shape and element values (even if their dtypes differ like int64 or float32). It returns True if the tensors are identical, and False otherwise.

tensor.equal() method

In the latest version of PyTorch, dtype differences are now allowed as long as values match exactly (e.g., 1 == 1.0). Broadcasting is not allowed; shapes must match exactly.

Syntax

torch.equal(input, other)

Parameters

Argument Description
input (Tensor) It is the first tensor to compare.
other (Tensor) It is the second tensor to compare.

Identical tensors

Let’s define two identical tensors and check if it is equal.
import torch

# Two identical tensors
tensor1 = torch.tensor([11, 2, 31])
tensor2 = torch.tensor([11, 2, 31])

print(torch.equal(tensor1, tensor2))

# Output: True

In the above code, both tensors have the same shape ([3]), data type (torch.int64), device (CPU), and values.

Different values

Different values while comparing two tensors

If the input tensors have the same size but different values, the .equal() method returns False.

import torch

first_tensor = torch.tensor([1, 2, 3])
second_tensor = torch.tensor([1, 2, 4])

print(torch.equal(first_tensor, second_tensor))

# Output: False

In the above code, only the last elements of both tensors are different. The first_tensor has element 3, and the second_tensor has element 4. So, they are not the same and returned False.

Different Data Types

If the first tensor has an integer type and the second tensor has a float type, it will return True as long as the values are the same in both input tensors.

import torch

int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)
float_tensor = torch.tensor([1, 2, 3], dtype=torch.float32)

print(torch.equal(int_tensor, float_tensor))

# Output: True

Empty tensors

If you compare two empty tensors, it will return True.

import torch

empty_tensor_one = torch.tensor([], dtype=torch.float32)
empty_tensor_two = torch.tensor([], dtype=torch.float32)

print(torch.equal(empty_tensor_one, empty_tensor_two))  

# Output: True

Multi-dimensional tensors

Multi-dimensional tensors are compared element-wise, and all properties (shape and device) must match.

import torch

tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[1, 2], [3, 4]])

print(torch.equal(tensor1, tensor2))

# Output: True
That’s all!
Post Views: 15
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
image
image
image
image
image
logo

Address: 121 King Street, Melbourne Victoria 3000 Australia.

hamela@example.com

+36 (0) 1779 228 338..

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact
SUPPORT
  • Content Strategy
  • Copywriting
  • Content Marketing
  • Web Design
QUICK LINKS
  • Marketplace
  • Documentation
  • Customers
  • Carrers
INSTAGRAM

Copyright by @Themesflat  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS