License YOLO
Back to Ultralytics Glossary

Cosine Similarity

Cosine similarity scores how alike two vectors are by the angle between them, ignoring length. The formula, related measures, search uses, and code.

Cosine similarity measures how alike two vectors are by the cosine of the angle between them: 1 when they point in the same direction, 0 when they are perpendicular, and −1 when they point in opposite directions. It ignores vector length and compares direction only, which suits data whose direction carries the meaning, such as word counts from documents of different lengths or the embeddings that neural networks produce for text and images. That makes it the default score for comparing items in semantic search, retrieval-augmented generation (RAG), and recommendation systems.

How Cosine Similarity Is Calculated#

Cosine similarity divides the dot product of two vectors by the product of their lengths: cos(θ) = (A·B) / (‖A‖·‖B‖), where ‖A‖ is the Euclidean length, the square root of the sum of squared entries. Scaling either vector by a positive number leaves the score unchanged. Vectors with no negative entries, such as word counts or TF-IDF weights, always score between 0 and 1. This example compares a vector with a longer copy of itself, with a perpendicular vector, and with its opposite:

import numpy as np


def cosine(u, v):
    """Return the cosine of the angle between two vectors."""
    return u @ v / (np.linalg.norm(u) * np.linalg.norm(v))


a = np.array([1.0, 2.0, 3.0])
b = 10 * a  # same direction, ten times longer
c = np.array([3.0, 0.0, -1.0])  # perpendicular to a

print(f"cos(a, b) = {cosine(a, b):.2f}, Euclidean distance = {np.linalg.norm(a - b):.1f}")
print(f"cos(a, c) = {cosine(a, c):.2f}, cos(a, -a) = {cosine(a, -a):.2f}")

The scaled copy scores a perfect 1.00 even though it lies 33.7 units away in Euclidean distance, the perpendicular vector scores 0.00, and the opposite vector scores −1.00.

After vectors are normalized to unit length, cosine similarity equals their dot product, and squared Euclidean distance equals 2 − 2·cos(θ), so ranking neighbors by cosine similarity, dot product, or Euclidean distance gives the same order. Vector databases and search libraries therefore normalize embeddings once when indexing them, and scoring a query against a whole collection becomes a single matrix-vector product, the core operation of vector search. Deep learning frameworks provide the calculation directly, as in PyTorch's cosine_similarity function.

Applications in AI#

  • Retrieval for language models: A query and a library of document chunks are embedded with the same model, and the chunks with the highest cosine similarity to the query are passed to the language model as context.
  • Face verification: Systems trained with losses such as ArcFace, which adds an angular margin between identities, compare two face embeddings by cosine similarity and accept a match above a tuned threshold.
  • Zero-shot classification with CLIP: CLIP labels an image by computing the cosine similarity between its embedding and the embeddings of prompts such as "a photo of a dog", scaling the scores by a learned temperature, and applying a softmax across the prompts.
  • Contrastive learning: Losses such as SimCLR's NT-Xent divide the cosine similarity of two embeddings by a temperature, pulling views of the same image together and pushing other images apart.
  • Item-based recommendations: Collaborative filtering compares items by the cosine similarity of their rating vectors, so products bought by the same customers count as similar.

Comparing Image Embeddings in Code#

YOLO26 models return one embedding per image through embed(), which skips the prediction head and average-pools the output of the model's second-to-last layer. This example compares embeddings of two sample images from a YOLO26 image classification model:

import torch.nn.functional as F
from ultralytics import YOLO

# Load a YOLO26 classification model pretrained on ImageNet
model = YOLO("yolo26n-cls.pt")

# Generate one embedding vector per image
embeddings = model.embed(["https://ultralytics.com/images/bus.jpg", "https://ultralytics.com/images/zidane.jpg"])

# Cosine similarity between the two image embeddings
similarity = F.cosine_similarity(embeddings[0], embeddings[1], dim=0)
print(f"Cosine similarity: {similarity.item():.4f}")

The printed score falls between −1 and 1, and higher values mean the network represents the two images more alike.

  • Cosine distance: Defined as 1 minus cosine similarity and ranging from 0 to 2. It violates the triangle inequality, so it is not a true distance metric, while the angle itself, θ, is one.
  • Dot product: Includes both vectors' lengths, so a long vector outscores a short one pointing in the same direction. Many two-tower retrieval models are trained to score with the raw dot product, where a vector's length can carry information such as an item's overall popularity.
  • Euclidean distance: Measures the straight-line gap between two points and grows with differences in length, so a short and a long document on the same topic land far apart in word-count space.
  • K-nearest neighbors (KNN): An algorithm that needs a similarity or distance to find neighbors, and text and embedding applications often give it cosine similarity.
  • Pearson correlation: Equals the cosine similarity of the two vectors after subtracting each vector's own mean from its entries.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning