Learn practical skills, build real-world projects, and advance your career

5 Essentially Useful PyTorch functions for Comparison Ops

PyTorch is an open source machine learning library based on the Torch library which is used for applications such as computer vision and natural language processing.

In today’s PyTorch growing community, it is very important to fully utilize all the benefits of this fairly new platform in deep learning. In this assignment, I will be demonstrating the 5 PyTorch functions used for comparison purposes.

  • TORCH.ALLCLOSE
  • TORCH.GE
  • TORCH.TOPK
  • TORCH.SORT
  • TORCH.EQ

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.ALLCLOSE()

The syntax for torch.allclose() is as follows:-

####torch.allclose(input, other, rtol = 1e-05, atol = 1e-08, equal_nan = False) → bool

This function checks if all input and other satisfy the condition:

|input - other| ≤ atol + rtol × ∣other∣

elementwise, for all elements of input and other. The behaviour of this function is analogous to numpy.allclose

In other words, we are checking whether the two tensors are semantically equal or not.

# Example 1 - working 
torch.allclose(torch.tensor([10000., 1e-07]), torch.tensor([10000.1, 1e-08]))
False

In this example, we can clearly see that the first tensor is not equal to the second. This results in the output being False.