liveBook · Manning

Cosine similarity

As explained in Math and Architectures of Deep Learning

A dot product computed on unit-length vectors so that the score is insensitive to vector length.

Cosine similarity compares vectors by direction rather than raw magnitude. In document retrieval, TF-IDF represents each document as a weighted feature vector, then uses the vectors’ directional agreement to measure document similarity. section 4.6.1 section 2.5.6

For vectors a and b, cosine similarity is the dot product divided by their lengths: cos(θ) = (a · b) / (||a|| ||b||). Equivalently, it is the dot product of unit-length versions of the vectors. The angle supplies the interpretation: aligned vectors score positively, perpendicular vectors score zero, and opposite vectors score negatively. section 2.5.6

In retrieval, the score has practical consequences:

  • TF-IDF down-weights common terms and emphasizes distinctive terms before cosine comparison. section 4.6.1
  • Cosine similarity detects direct vocabulary overlap, but misses semantic relationships expressed through different words. section 4.6.2
  • Latent semantic analysis addresses this limitation by comparing document projections onto topic directions rather than only their original term vectors. section 4.6.2 section 4.7

A misleading zero

Documents d5 and d6 receive cosine similarity zero because one contains “gun” and the other “violence,” with no direct term overlap. Human readers may still recognize a relationship because those terms co-occur elsewhere; cosine similarity alone does not capture that secondary evidence. section 4.6.2

Equation 4.13

Use the equation figure to inspect the normalized dot-product calculation. section 4.6.1

How Math and Architectures of Deep Learning covers it

How deep it goes
Explains normalization, angular geometry, and retrieval limitations, then connects the limitation to latent semantic analysis.
Examples
Uses TF-IDF document vectors and the d5–d6 gun-versus-violence mismatch to expose direct-overlap limits.
What it assumes
Assumes vectors and dot products, while explaining unit-vector normalization and angular interpretation.
What you can do after
Apply cosine similarity to compare weighted document vectors and recognize when direct term matching misses latent semantic relations.
How it connects
Connects dot product, TF-IDF, document retrieval, latent semantic analysis, principal components, and topic modeling.

Also explained in

Compare how 14 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.