liveBook · Manning

Cosine similarity

As explained in Graph Neural Networks in Action

Cosine similarity is a measure of similarity between vectors based on the cosine of the angle between them.

Cosine similarity compares vector embeddings by measuring how closely their directions align. Applied to node embeddings, it supports finding related items rather than generating predictions. section 3.1.5

Use cosine similarity to build a practical similarity workflow:

  • Generate node embeddings with the trained graph model in evaluation mode and without gradient computation. section 3.1.5
  • Create a similarity matrix by comparing every pair of embeddings with SciKit Learn’s cosine_similarity function. section 3.1.5
  • Sort one node’s similarity scores in descending order, remove the selected node itself, and use the next highest entries as recommendations. section 3.1.5

Product bundle generation

Selecting node 123 identifies the Funko POP Television: Adventure Time Marceline Vinyl Figure. Ranking its embedding similarities produces a bundle of related Toys & Games products. section 3.1.5

How Graph Neural Networks in Action covers it

How deep it goes
Defines cosine similarity operationally through embedding comparison, matrix construction, and ranked nearest-neighbor retrieval.
Examples
Demonstrates a Marceline product bundle produced by ranking similarities for node 123.
What it assumes
Assumes a trained graph model and node embeddings, while demonstrating evaluation-mode inference before similarity computation.
What you can do after
Apply it via SciKit Learn and NumPy to rank products nearest to a chosen graph node.
How it connects
Connects cosine similarity to embeddings, similarity matrices, graph convolutional networks, GraphSAGE, and recommendation.

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 Graph Neural Networks in Action. The section links point to the parts of the book it draws on.