Cosine similarity is utilized to find the similarity of two tensors/vectors. PyTorch provides a “torch.nn” module that has a “CosineSimilarity()” function to calculate the cosine similarity between two particular tensors. This function returns the computed cosine similarity value along the defined dimension. Moreover, users can calculate the cosine similarity between 1D tensors along “dim=0” only and for 2D tensors, the cosine similarity can be calculated along “dim=0” or “dim=1”.

This write-up will exemplify the methods to find/calculate the cosine similarity between two specific tensors in PyTorch.

How to Calculate/Find Cosine Similarity Between Two Tensors in PyTorch?

To compute or find cosine similarity between two PyTorch tensors, follow the provided steps:

  • Import PyTorch library
  • Create two desired tensors 
  • Print tensors elements 
  • Compute the cosine similarity

Go through the following examples for a practical demonstration:

Example 1: Calculate Cosine Similarity Between 1D Tensors in PyTorch

In the first section, we will define two 1D tensors and compute their cosine similarity. 

Step 1: Import PyTorch Library

First, install the “torch” library to compute cosine similarity:

import torch

Step 2: Define Two 1D Tensors 

Next, define two desired tensors and display their elements. Here, we are defining the following “Tens1” and “Tens2” tensors from a list via the “torch.tensor()” function:

Tens1 = torch.tensor([0.7, 0.1, 1.4, 0.9])

Tens2 = torch.tensor([1.6, 0.2, 1.3, 1.8])

Note: Make sure that the size of both tensors is the same and they must have real values.

Step 3: Print Tensors Elements 

Then, print the elements of the above-created 1D tensors:

print(Tens1)

print(Tens2)

The below output shows the elements of “Tens1” and “Tens2” tensors:

Step 4: Compute Cosine Similarity

Now, use the “CosineSimilarity()” method with the “dim=0” parameter to calculate the cosine similarity between 1D tensors:

cos_sim = torch.nn.CosineSimilarity(dim=0)

After that, calculate the cosine similarity by specifying both “Tens1” and “Tens2” 1D tensors:

output = cos_sim(Tens1, Tens2)

Finally, display the calculated cosine similarity that is stored in the “output” variable:

print("Cosine Similarity:",output)

The below output displays the cosine similarity value of 1D tensors:

Example 2: Calculate Cosine Similarity Between 2D Tensors in PyTorch

In the second section, we will define two 2D tensors and compute their cosine similarity along different dimensions. 

Step 1: Import PyTorch Library

First, install the “torch” library to compute cosine similarity:

import torch

Step 2: Define Two 2D Tensors

Then, define and print two 2D tensors. For example, we are defining two “T1” and “T2” 2D tensor with random values using the “torch.randn()” function:

T1 = torch.randn(3,4)

T2 = torch.randn(3,4)

Step 3: Print Tensors Elements 

After that, display the elements of the above-created 2D tensors:

print(T1)

print(T2)

In the below output, the elements of “T1” and “T2” 2D tensors can be seen:

Step 4: Compute Cosine Similarity 

Now, define the method using the “CosineSimilarity()” with the “dim=0” parameter to compute the cosine similarity of 2D tensor column-wise and display it:

cos_sim0 = torch.nn.CosineSimilarity(dim=0)
output = cos_sim0(T1, T2)

print("Cosine Similarity in dim 0:",output)

The below output shows the cosine similarity of 2D tensors row-wise:

To calculate the cosine similarity in row-wise, use the “CosineSimilarity()” with the “dim=1” parameter and display it:

cos_sim1 = torch.nn.CosineSimilarity(dim=1)
output = cos_sim1(T1, T2)

print("Cosine Similarity in dim 1:",output)

According to the below output, the cosine similarity between “T1” and “T2” tensors in “dim=1” has been calculated successfully:

We have efficiently explained the methods of calculating the cosine similarity between 1D and 2D tensors in PyTorch using different examples.

Note: Click on the provided link to access our Google Colab Notebook.

Conclusion

To calculate the cosine similarity between two desired PyTorch tensors, first, install the “torch” library. Then, define two desired tensors, and display their elements. After that, use the “CosineSimilarity()” function to find the cosine similarity between tensors. Next, compute the cosine similarity and print it. This write-up has exemplified the methods to find/calculate the cosine similarity between two specific tensors in PyTorch.