Cosine similarity measures how similar two vectors are by computing the cosine of the angle between them — returning a value between -1 (opposite) and 1 (identical direction). PyTorch provides torch.nn.CosineSimilarity() in the torch.nn module and torch.nn.functional.cosine_similarity() for a functional API approach. Both work on 1D and 2D tensors along a specified dimension.
Quick Answer: To calculate cosine similarity in PyTorch, use torch.nn.CosineSimilarity(dim=0)(tensor1, tensor2) for 1D vectors, or torch.nn.functional.cosine_similarity(t1, t2, dim=1) for row-wise comparison of 2D tensors.
CosineSimilarity: Class vs Functional API
PyTorch offers two ways to compute cosine similarity:
| API | Usage | Best For |
|---|---|---|
torch.nn.CosineSimilarity(dim) |
Create an object, then call it with two tensors | Reusing the same similarity function in a model or loop |
F.cosine_similarity(t1, t2, dim) |
Direct function call — no object creation needed | One-off computations, cleaner scripts |
Both return the same result. The functional form (import torch.nn.functional as F) is often preferred for its brevity.
Example 1: Calculate Cosine Similarity Between 1D Tensors
For 1D tensors, use dim=0 — similarity is computed across all elements.
Step 1: Import PyTorch
import torch

Step 2: Define Two 1D Tensors
Create two 1D tensors of equal size with real values:
Tens1 = torch.tensor([0.7, 0.1, 1.4, 0.9])
Tens2 = torch.tensor([1.6, 0.2, 1.3, 1.8])

Note: Both tensors must have the same size and contain real values.
Step 3: Print the Tensor Elements
print(Tens1)
print(Tens2)

Step 4: Compute Cosine Similarity
Use CosineSimilarity(dim=0) to compute similarity element-wise across a 1D tensor:
cos_sim = torch.nn.CosineSimilarity(dim=0)

output = cos_sim(Tens1, Tens2)

print("Cosine Similarity:", output)

Example 2: Calculate Cosine Similarity Between 2D Tensors
For 2D tensors, you can compute cosine similarity column-wise (dim=0) or row-wise (dim=1).
Step 1: Import PyTorch
import torch

Step 2: Define Two 2D Tensors
T1 = torch.randn(3, 4)
T2 = torch.randn(3, 4)

Step 3: Print the Tensor Elements
print(T1)
print(T2)

Step 4: Compute Cosine Similarity Along dim=0 (Column-wise)
cos_sim0 = torch.nn.CosineSimilarity(dim=0)
output = cos_sim0(T1, T2)
print("Cosine Similarity in dim 0:", output)

Computing along dim=1 compares tensors row-wise instead:
cos_sim1 = torch.nn.CosineSimilarity(dim=1)
output = cos_sim1(T1, T2)
print("Cosine Similarity in dim 1:", output)

Note: For the complete working code, access the Google Colab Notebook.
Conclusion
To calculate cosine similarity in PyTorch, use torch.nn.CosineSimilarity(dim=0) for 1D tensors and specify dim=0 (column-wise) or dim=1 (row-wise) for 2D tensors. Both tensors must have the same size and contain real values. For a more concise alternative, use the functional API: torch.nn.functional.cosine_similarity(t1, t2, dim=1) — it produces the same result without creating a separate object.