Zubnet AIAprenderWiki › Cosine Similarity
Fundamentos

Cosine Similarity

Cosine Distance, Vector Similarity
Una medida de similitud entre dos vectores basada en el ángulo entre ellos, ignorando su magnitud. Una cosine similarity de 1 significa que los vectores apuntan en la misma dirección (significado idéntico). 0 significa que son perpendiculares (no relacionados). -1 significa direcciones opuestas. Es la métrica de similitud estándar para comparar embeddings de texto en búsqueda semántica, RAG y sistemas de recomendación.

Por qué importa

Cada vez que haces búsqueda semántica, usas RAG o comparas embeddings, la cosine similarity es (probablemente) la métrica decidiendo qué es «similar». Entenderla te ayuda a debuguear calidad de retrieval, elegir entre cosine y alternativas (dot product, distancia euclidiana), y entender por qué algunas búsquedas pierden coincidencias obvias.

Deep Dive

The formula: cos(θ) = (A · B) / (||A|| × ||B||), where A · B is the dot product and ||A||, ||B|| are the vector magnitudes (lengths). By dividing by magnitudes, cosine similarity measures direction only — a vector [1, 2, 3] is identical in cosine similarity to [2, 4, 6] because they point the same way. This normalization is why cosine works well for embeddings: the direction encodes meaning, while magnitude can vary based on text length or model quirks.

Cosine vs. Dot Product

If embeddings are already normalized to unit length (magnitude 1), cosine similarity equals the dot product — and dot product is faster to compute (no division). Most embedding models output normalized vectors for exactly this reason. When using a vector database, check whether your embeddings are normalized: if yes, use dot product (faster). If not, use cosine similarity (correct regardless of normalization).

Limitations

Cosine similarity treats all dimensions equally, but some embedding dimensions may be more important than others. It also measures overall direction similarity, which can miss nuanced differences. Two sentences about "Python programming" and "Python the snake" might have moderately high cosine similarity because they share the "Python" concept. More sophisticated similarity measures (learned metrics, cross-encoder reranking) can capture finer distinctions at higher computational cost.

Conceptos relacionados

← Todos los términos
← Corpus Cross-Attention →