Learn how cosine similarity measures vector similarity in AI. Calculate visual embeddings with Ultralytics YOLO26 and scale with Ultralytics Platform.
Cosine similarity is a fundamental mathematical metric used in machine learning (ML) and artificial intelligence (AI) to measure how similar two multi-dimensional arrays or vectors are, regardless of their size or magnitude. By calculating the angle between two points in a vector space, it determines if they are pointing in roughly the same direction. This angular approach is critical for processing data where the orientation matters more than the overall length, making it highly effective for comparing abstract data representations like embeddings.
To calculate this metric, you compute the dot product of two vectors and divide it by the product of their individual magnitudes (lengths). The resulting score always falls within a fixed range from -1 to 1:
In many modern deep learning frameworks designed for computer vision (CV), you can easily access optimized functions for this mathematical operation, such as PyTorch's functional module or TensorFlow metrics.
It is helpful to distinguish cosine similarity from other frequently used data analytics measurements to understand when to use it:
Cosine similarity acts as the core engine for numerous modern software products, bridging the gap between raw data and human intent.
You can extract high-dimensional feature vectors directly from visual data using state-of-the-art vision models. The following Python code demonstrates how to load an Ultralytics YOLO26 model for image classification, generate embeddings for two images, and perform a cosine similarity calculation to measure their visual resemblance.
import torch
import torch.nn.functional as F
from ultralytics import YOLO
# Load a pre-trained YOLO26 classification model
model = YOLO("yolo26n-cls.pt")
# Generate embedding vectors for two separate images
results = model.embed(["bus.jpg", "car.jpg"])
# Calculate the cosine similarity between the two visual embeddings
similarity = F.cosine_similarity(torch.tensor(results[0]), torch.tensor(results[1]), dim=0)
print(f"Visual Similarity Score: {similarity.item():.4f}")
For developers aiming to scale these semantic search capabilities, training highly accurate base models is paramount. The Ultralytics Platform streamlines this pipeline by offering robust tools for data annotation, scalable cloud training, and seamless model deployment, ensuring your underlying embeddings are as accurate and meaningful as possible.
Begin your journey with the future of machine learning