In this article, we will discuss how to compute the Cosine Similarity between two tensors in Python using PyTorch.
The vector size should be the same and the value of the tensor must be real. we can use CosineSimilarity() method of torch.nn module to compute the Cosine Similarity between two tensors.
CosineSimilarity() method
CosineSimilarity() method computes the Cosine Similarity between two tensors and returns the computed cosine similarity value along with dim. if the input tensor is in 1D then we can compute the cosine similarity only along with dim=0 and if the input tensor is in 2D then we can compute the cosine similarity along with both dim=0 or 1. The below syntax is used to compute the Cosine Similarity between two tensors.
Syntax: torch.nn.CosineSimilarity(dim)
Parameters:
- dim: This is dimension where cosine similarity is computed by default the value of dim is 1.
Return: This method returns the computed cosine similarity value along with dim.
Example 1:
The following program is to understand how to compute the Cosine Similarity between two 1D tensors.
Python3
# Import required library import torch # define two 1D tensors tens_1 = torch.tensor([ 0.5 , 0.3 , 1.2 , 0.33 ]) tens_2 = torch.tensor([ 0.3 , 0.2 , 1.3 , 1.4 ]) # print above defined two tensors print ( "\n First Tensor: " , tens_1) print ( "\n Second Tensor: " , tens_2) # compute cosine similarity cosi = torch.nn.CosineSimilarity(dim = 0 ) output = cosi(tens_1, tens_2) # display the output tensor print ( "\n Computed Cosine Similarity: " , output) |
Output:
Example 2:
The following program is to know how to compute the Cosine Similarity between two 2D tensors.
Python3
# Import required library import torch # define first 2D tensor tens_1 = torch.tensor([[ 0.2245 , 0.2959 , 0.3597 , 0.6766 ], [ - 2.2268 , 0.6469 , 0.3765 , 0.7898 ], [ 0.4577 , 0.3228 , 0.4699 , 0.2389 ]]) # define second 2D tensor tens_2 = torch.tensor([[ 0.2423 , 0.4667 , 0.4434 , 0.3598 ], [ - 0.6679 , 0.6932 , 0.5387 , 0.2245 ], [ 0.8277 , 0.2597 , 0.9834 , 0.9987 ]]) # print above defined two tensors print ( "\n\n First Tensor: \n" , tens_1) print ( "\n Second Tensor: \n" , tens_2) # compute cosine similarity in dim=0 cos_1 = torch.nn.CosineSimilarity(dim = 0 ) output_1 = cos_1(tens_1, tens_2) # display the output tensor print ( "\n\nComputed Cosine Similarity in dim=0: " , output_1) # compute cosine similarity in dim=1 cos_2 = torch.nn.CosineSimilarity(dim = 1 ) output_2 = cos_2(tens_1, tens_2) # display the output tensor print ( "\n\nComputed Cosine Similarity in dim=1: " , output_2) |
Output: