Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
Five comparision operations in PyTorch tensors
In this notebook five comparision operation functions are discussed as a part of Assignment-1
in the course Deep Learning with PyTorch:Zero to GANs
offered by Jovian.ai in colloboration with freecodecamp.org. The five functions are as follows:
- torch.eq
- torch.equal
- torch.ge
- torch.le
- torch.lt
Before we begin, let's install and import PyTorch
# Uncomment and run the appropriate command for your operating system, if required
# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# Windows
#!pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# MacOS
# !pip install numpy torch torchvision torchaudio
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Requirement already satisfied: numpy in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.19.4)
Requirement already satisfied: torch==1.7.0+cpu in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.7.0+cpu)
Requirement already satisfied: torchvision==0.8.1+cpu in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (0.8.1+cpu)
Requirement already satisfied: torchaudio==0.7.0 in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (0.7.0)
Requirement already satisfied: future in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (from torch==1.7.0+cpu) (0.18.2)
Requirement already satisfied: dataclasses in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (from torch==1.7.0+cpu) (0.6)
Requirement already satisfied: typing-extensions in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (from torch==1.7.0+cpu) (3.7.4.3)
Requirement already satisfied: numpy in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.19.4)
Requirement already satisfied: torch==1.7.0+cpu in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.7.0+cpu)
Requirement already satisfied: pillow>=4.1.1 in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (from torchvision==0.8.1+cpu) (8.0.1)
Requirement already satisfied: torch==1.7.0+cpu in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.7.0+cpu)
Requirement already satisfied: numpy in c:\users\dnabh\anaconda3\envs\jovian\lib\site-packages (1.19.4)
# Import torch and other required modules
import torch
Function 1 - torch.eq
Computes element-wise equality
The second argument can be a number or a tensor whose shape is broadcastable with the first argument.
Syntax : torch.eq(input, other, *, out=None) → Tensor
Parameters :
- input(tensor) : The tensor to compare
- other(tensor / float) : the tensor or value to compare
- Keyword Arguments : out (Tensor, optional) – the output tensor.
Returns A boolean tensor that is True where input is equal to other and False elsewhere
# Example 1 - working
A = torch.tensor([[1, 2], [3, 4.]])
B = torch.tensor([[1, 5], [3, 4]])
torch.eq(A,B)
tensor([[ True, False],
[ True, True]])