Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compute pairwise distance between two vectors in PyTorch?
A vector in PyTorch is a 1D tensor. To compute pairwise distance between two vectors, we can use the PairwiseDistance() function. It uses p-norm to compute the pairwise distance. PairwiseDistance is basically a class provided by the torch.nn module.
The size of both the vectors must be same.
Pairwise distance can be computed for both real and complex-valued inputs.
The vectors must be in [N,D] shape, where N is the batch dimension and D is the vector dimension.
Syntax
torch.nn.PairwiseDistance(p=2)
The default p is set to 2.
Steps
You could use the following steps to compute the pairwise distance between two vectors
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define two vectors or two batches of vectors and print them. You can define real or complex-valued tensors.
v1 = torch.randn(3,4) v2 = torch.randn(3,4)
Create an instance of PairwiseDistance to compute the pairwise distance between the two vectors.
pdist = torch.nn.PairwiseDistance(p=2)
Compute the pairwise distance between the above-defined vectors.
output = pdist (v1, v2)
Print the computed tensor with the pairwise distance values.
print("Pairwise Distance:", output)
Example 1
In this program, we compute the pairwise distance between two 1D vectors. Notice that we have unsqueezed the vectors to make them batched.
# python3 program to compute pairwise distance between
# the two 1D vectors/ tensors
import torch
# define first vector
v1 = torch.tensor([1.,2.,3.,4.]) # size is [4]
# unsqueeze v1 to make it of size [1,4]
v1 = torch.unsqueeze(v1,0)
print("Size of v1:",v1.size())
# define and unsqueeze second vector
v2 = torch.tensor([2.,3.,4.,5.])
v2 = torch.unsqueeze(v2, 0)
print("Size of v2:",v2.size())
print("Vector v1:
", v1)
print("Vector v2:
", v2)
# create an instance of the PairwiseDistance
pdist = torch.nn.PairwiseDistance(p=2)
# compute the distance
output = pdist(v1, v2)
# display the distance
print("Pairwise Distance:
",output)
Output
Size of v1: torch.Size([1, 4]) Size of v2: torch.Size([1, 4]) Vector v1: tensor([[1., 2., 3., 4.]]) Vector v2: tensor([[2., 3., 4., 5.]]) Pairwise Distance: tensor([2.0000])
Example 2
In this program, we compute the pairwise distance between two batches of 1D vectors.
# python3 program to compute pairwise distance between
# a batch vectors/ tensors
import torch
# define first batch of 3 vectors
v1 = torch.rand(3,4)
print("Size of v1:",v1.size())
# define second batch of 3 vectors
v2 = torch.rand(3,4)
print("Size of v2:",v2.size())
print("Vector v1:
", v1)
print("Vector v2:
", v2)
# define function to compute pairwise distance
pdist = torch.nn.PairwiseDistance(p=2)
# compute the distance
output = pdist(v1, v2)
# display the distances
print("Pairwise Distance:
",output)
Output
Size of v1: torch.Size([3, 4]) Size of v2: torch.Size([3, 4]) Vector v1: tensor([[0.7245, 0.7953, 0.6502, 0.9976], [0.1185, 0.6365, 0.3543, 0.3417], [0.7827, 0.3520, 0.5634, 0.0534]]) Vector v2: tensor([[0.6419, 0.2966, 0.4424, 0.6570], [0.5991, 0.4173, 0.5387, 0.1531], [0.8377, 0.6622, 0.8260, 0.8249]]) Pairwise Distance: tensor([0.6441, 0.5904, 0.8737])