Hugging Face, End to EndLesson 51 of 75

Module 6 · Embeddings, and search that understands

Cosine, dot product, and the normalisation that connects them

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 4 of 9 in this chapter

This chapter’s goalBuild and measure a semantic search over your own documents: choosing an embedding model against your language and text length, chunking deliberately, normalising vectors for the similarity you use, adding a cross-encoder reranker, and reporting recall@k on questions you wrote yourself.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:08Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Three measures, and when they differ

Cosine similarity is the cosine of the angle between two vectors. It ignores length entirely and ranges from −1 to 1, though in practice text embeddings rarely go below about 0 because the vectors occupy a narrow cone of the space.

Dot product multiplies element-wise and sums. It is cosine multiplied by both vectors’ lengths, so it rewards longer vectors.

Euclidean distance is straight-line distance. Smaller is better, which inverts every comparison and is a routine source of reversed results.

The connection that makes this simple: on unit-length vectors, ranking by cosine, dot product and Euclidean distance gives the identical order. Normalise once at encode time and the choice stops mattering:

Python
v = model.encode(texts, normalize_embeddings=True)
scores = v @ q          # dot product == cosine, and it is the fastest

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Cosine, dot product, and the normalisation that connects them’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/similarity-and-distance (Accessed: 29 November 2026).