Search

Search papers and lessons

Node Embedding

Level 3 · 12 min read

A node embedding gives every node of a graph a short vector of numbers, learned so that nodes that sit close together in the graph get vectors that point the same way.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Neural network basics
  • Linear algebra
  • Representation learning
  • ReadReady to readNot yet

TL;DR

A node embedding turns each node of a graph into a short list of numbers, called a vector. The vectors are learned so that nodes which are close in the graph have vectors that point in similar directions. A computer can then work with the vectors, for example to find similar items or predict missing links, without reading the graph's structure each time.

Builds on: Graph, Representation Learning · Leads to: Graph Convolutional Network

Where it's used

Product recommendation. A shop can connect two products when customers often buy them together. A node embedding gives each product a vector, and the shop recommends the products whose vectors are most similar to the one a shopper is viewing. The graph is large, so the vectors are a compact summary that the recommender can compare quickly.

Biology. Proteins that interact can be drawn as a graph. Embeddings of the proteins are then used to predict what a protein does. The GraphSAGE paper names identifying protein functions among its target tasks, and it classifies protein roles in protein-protein interaction graphs.

Fraud and link prediction. In a graph of accounts and transactions, an account whose vector sits far from its usual neighbours may be unusual. Link prediction asks which missing edges are likely. Both tasks reduce to comparing vectors, which is cheap once the embeddings exist.

How it works

Take the smallest case, a path of four nodes: A joined to B, B joined to C, and C joined to D. We will not learn the vectors here. We will check what a good set of vectors looks like and how the score of a set of vectors is measured.

flowchart LR
  G[Graph] --> W[Random walks from each node]
  W --> P[Pairs of a node and a nearby node on a walk]
  P --> M[Choose vectors that make nearby pairs score high]
  M --> Z[One vector per node]
  1. Simulate short random walks. From A the walk A, B, C is one possibility. From D the walk D, C, B is another. Check: each step moves along an edge, so every walk stays in the graph.

  2. Read off nearby pairs. Within each walk, call two nodes a pair if they are at most one step apart. The walk A, B, C gives the pairs (A, B) and (B, C). The walk D, C, B gives (D, C) and (C, B). Check: B and C are paired twice, and A and D are never paired.

  3. Give each node a vector in two dimensions. Try zA=(1,0)z_A = (1, 0), zB=(0.8,0.6)z_B = (0.8, 0.6), zC=(0,1)z_C = (0, 1) and zD=(−0.6,0.8)z_D = (-0.6, 0.8). Check the lengths: each vector has length 1, so the dot products below measure direction only.

  4. Score pairs with the dot product. zA⋅zB=0.8z_A \cdot z_B = 0.8, which is high, and zB⋅zC=0.6z_B \cdot z_C = 0.6, also high. For the far pair, zA⋅zD=−0.6z_A \cdot z_D = -0.6, which is low. Check: the nodes that are paired in walks have large dot products, and the distant pair has a negative one.

  5. Turn the scores into probabilities. For node B, the scores against A, B, C and D are 0.8, 1.0, 0.6 and 0.0. Passing them through a softmax gives a probability that each node appears near B. The probability that A appears near B is about 0.29, and for C it is about 0.23. Check: the four probabilities sum to 1.

  6. Name the goal. A good embedding makes the probability of the observed nearby pairs as large as possible. Compared with giving every node the same vector, which gives every probability 1/41/4, the vectors above score better. Check: the uniform vectors give a log-probability of 2ln⁡(1/4)≈−2.772 \ln(1/4) \approx -2.77 on the two pairs for B, and the vectors above give about −2.70-2.70, which is higher.

The math

Definition: Node embedding

Let G=(V,E)G = (V, E) be a graph. A node embedding of dimension dd is a function f ⁣:V→Rdf \colon V \to \mathbb{R}^d, written zv=f(v)z_v = f(v). The embedding is learned from the graph so that, for a node vv and a node uu close to it in the graph, the dot product zu⋅zvz_u \cdot z_v is large.

Step 1 and 2 in symbols: walks and pairs

Run rr random walks of length ℓ\ell from each node. Fix a window size kk. For each walk and each position, the nodes within kk steps of the current node, excluding itself, form its context, written N(v)N(v) for the walk's node vv. The training data is the multiset of pairs (v,u)(v, u) with uu in N(v)N(v). In the tiny example, k=1k = 1, so each node's context is its immediate neighbours on the walk.

Step 4 and 5 in symbols: the softmax over nodes

For a centre node vv, the model gives a probability to each node uu of the graph: P(u∣v)=exp⁡(zu⋅zv)∑w∈Vexp⁡(zw⋅zv).P(u \mid v) = \frac{\exp(z_u \cdot z_v)}{\sum_{w \in V} \exp(z_w \cdot z_v)}. The numerator rewards a large dot product between uu and vv. The denominator sums over all nodes, so the probabilities add to 1. For v=Bv = B in the tiny example, the four dot products are 0.8,1.0,0.6,0.00.8, 1.0, 0.6, 0.0, and the denominator is e0.8+e1.0+e0.6+e0.0≈7.766e^{0.8} + e^{1.0} + e^{0.6} + e^{0.0} \approx 7.766. So P(A∣B)=e0.8/7.766≈0.287P(A \mid B) = e^{0.8} / 7.766 \approx 0.287, and P(C∣B)=e0.6/7.766≈0.235P(C \mid B) = e^{0.6} / 7.766 \approx 0.235.

Step 6 in symbols: the objective

The embedding maximises the log-likelihood of the observed pairs: L=∑v∈V∑u∈N(v)ln⁡P(u∣v).\mathcal{L} = \sum_{v \in V} \sum_{u \in N(v)} \ln P(u \mid v). For the tiny example and centre node BB, the contexts are AA and CC, so the contribution is ln⁡P(A∣B)+ln⁡P(C∣B)≈−1.250−1.450=−2.70\ln P(A \mid B) + \ln P(C \mid B) \approx -1.250 - 1.450 = -2.70. A uniform embedding gives every probability 1/41/4, so the same contribution is 2ln⁡(1/4)≈−2.772 \ln(1/4) \approx -2.77. Larger is better, so the learned vectors win.

The sum over all of VV in the denominator is expensive for a large graph. Practical methods replace it with a small random sample of "negative" nodes, which keeps the same idea at much lower cost. Training uses stochastic gradient descent on the resulting objective.

Worked examples

Example
1. Tiny case: the four-node path

Use the vectors zA=(1,0)z_A = (1, 0), zB=(0.8,0.6)z_B = (0.8, 0.6), zC=(0,1)z_C = (0, 1), zD=(−0.6,0.8)z_D = (-0.6, 0.8).

  1. Compute the dot products of zCz_C with each node: zC⋅zA=0z_C \cdot z_A = 0, zC⋅zB=0.6z_C \cdot z_B = 0.6, zC⋅zC=1z_C \cdot z_C = 1, zC⋅zD=0.8z_C \cdot z_D = 0.8.
  2. Exponentiate: e0=1e^0 = 1, e0.6≈1.822e^{0.6} \approx 1.822, e1≈2.718e^1 \approx 2.718, e0.8≈2.226e^{0.8} \approx 2.226.
  3. Sum the four values: 1+1.822+2.718+2.226≈7.7661 + 1.822 + 2.718 + 2.226 \approx 7.766.
  4. Divide each by the sum to get P(⋅∣C)P(\cdot \mid C): about 0.129 for A, 0.235 for B, 0.350 for C and 0.287 for D.
  5. Interpretation: both B and D are direct neighbours of C on the path, but the model gives D 0.287 and B 0.235, because D's vector is closer to C's. A, which is two steps from C, gets the lowest probability, 0.129. The ordering follows the vectors, not just the edges, and training moves the vectors so that the walk pairs score higher.
Example
2. Standard case: two triangles joined by one edge

Take six nodes: a triangle on 1, 2, 3, a triangle on 4, 5, 6, and one edge from 3 to 4. Random walks mostly stay inside one triangle, so the good embedding puts 1, 2 and 3 on one side and 4, 5 and 6 on the other. Use the idealised one-dimensional vectors z=+1z = +1 for nodes 1, 2, 3 and z=−1z = -1 for nodes 4, 5, 6.

  1. The dot product zu⋅z1z_u \cdot z_1 is +1+1 for uu in {1,2,3}\{1, 2, 3\} and −1-1 for uu in {4,5,6}\{4, 5, 6\}.
  2. The softmax denominator is 3e1+3e−1≈8.155+1.104=9.2583e^{1} + 3e^{-1} \approx 8.155 + 1.104 = 9.258.
  3. The probability of node 2 near node 1 is e1/9.258≈0.294e^{1} / 9.258 \approx 0.294.
  4. The probability of node 4 near node 1 is e−1/9.258≈0.040e^{-1} / 9.258 \approx 0.040.
  5. The total probability on the triangle {1,2,3}\{1, 2, 3\} is 3⋅0.294≈0.8813 \cdot 0.294 \approx 0.881, and this includes node 1 itself. Each node in {4,5,6}\{4, 5, 6\} gets 0.040, so the total there is 3⋅0.040≈0.1193 \cdot 0.040 \approx 0.119. Check: 0.881+0.119=10.881 + 0.119 = 1.
  6. Interpretation: about nine in ten probability mass sits in node 1's own triangle, which matches the walks. The one edge between the triangles costs a little probability but does not break the cluster structure.
Example
3. Realistic case: recommending a book from co-purchases

A bookshop links two books when customers often buy them together. The learned two-dimensional vectors are illustrative: a science-fiction novel z1=(0.9,0.2)z_1 = (0.9, 0.2), a second science-fiction novel z2=(0.8,0.5)z_2 = (0.8, 0.5), a cookbook z3=(0.1,0.9)z_3 = (0.1, 0.9), and an atlas z4=(0.1,0.1)z_4 = (0.1, 0.1).

  1. A shopper views the first novel. Compute the dot product with the second novel: 0.9×0.8+0.2×0.5=0.72+0.10=0.820.9 \times 0.8 + 0.2 \times 0.5 = 0.72 + 0.10 = 0.82.
  2. Compute the dot product with the cookbook: 0.9×0.1+0.2×0.9=0.09+0.18=0.270.9 \times 0.1 + 0.2 \times 0.9 = 0.09 + 0.18 = 0.27.
  3. Compute the dot product with the atlas: 0.9×0.1+0.2×0.1=0.09+0.02=0.110.9 \times 0.1 + 0.2 \times 0.1 = 0.09 + 0.02 = 0.11.
  4. Rank the candidates by score: the second novel (0.82), the cookbook (0.27), then the atlas (0.11).
  5. Check the ranking against the graph: the first novel and second novel are bought together, so their vectors should be close, and the ranking agrees.
  6. Interpretation: the recommender only compares vectors, so it does not touch the full co-purchase graph at query time. A real system would use many more dimensions and learned values.

Try it: A shopper views the cookbook z3=(0.1,0.9)z_3 = (0.1, 0.9). Which of the other three items scores highest? Answer: with the second novel, 0.1×0.8+0.9×0.5=0.08+0.45=0.530.1 \times 0.8 + 0.9 \times 0.5 = 0.08 + 0.45 = 0.53. The first novel scores 0.09+0.18=0.270.09 + 0.18 = 0.27 and the atlas scores 0.100.10. The second novel wins.

Common mistakes and tradeoffs

It is tempting to think a node embedding stores the graph itself. In fact it stores only a summary of each node's neighbourhood in a fixed number of numbers. Two nodes can have similar vectors without being joined, because they share neighbours, and the summary loses the exact edges.

It is tempting to think the dimension of the vector must match the number of nodes. In fact the dimension is a chosen size, often much smaller than the number of nodes. The method compresses the graph into that size, and too small a dimension loses information.

It is tempting to think the embedding is found by reading the graph once. In fact it is learned from random walks, so two runs with different walks give different vectors. Only the relative geometry is stable, and the exact numbers are arbitrary, as the rotation of a set of vectors shows.

It is tempting to think a method that learns one vector per node can handle new nodes. In fact the classic methods, DeepWalk and node2vec among them, learn a vector for each node seen in training, so a new node needs retraining. The GraphSAGE paper addresses this by learning a function that builds the vector from a node's neighbourhood, which works for nodes not seen during training.

When to use it: when a graph is large, when a downstream task needs fast similarity between nodes, and when the graph is mostly fixed.

When not to use it: when the graph changes constantly and every new node must be embedded at once, or when node features such as text carry most of the signal. Then a model that combines features with neighbourhood information, such as a Graph Convolutional Network, is usually better.

Alternatives: spectral methods that factor the adjacency matrix give embeddings directly from linear algebra and are easier to analyse. Embeddings built from walks scale better to large sparse graphs.

History

Before learned embeddings, researchers reduced a graph to vectors with spectral methods. Laplacian eigenmaps, for example, used the eigenvectors of a matrix built from the graph. These methods were principled but costly on large graphs.

In 2014, DeepWalk by Perozzi, Al-Rfou and Skiena borrowed the training method of word embeddings. It treated random walks as sentences and nodes as words, so the same skip-gram style objective applied. This made node embeddings practical for large graphs.

In 2016, node2vec by Grover and Leskovec added biased random walks that control whether a walk explores locally or moves further out. This gave a tunable trade-off between capturing communities and capturing structural roles.

In 2017, GraphSAGE by Hamilton, Ying and Leskovec pointed out that the earlier methods were transductive: they could not embed nodes absent from training. Its inductive approach learns an aggregation function over a node's neighbourhood, so it generalises to unseen nodes. Its abstract names content recommendation and identifying protein functions as target tasks.

Recap

  • A node embedding gives each node a vector, so that nodes close in the graph have large dot products.
  • Training data come from random walks: each node is paired with the nodes near it on a walk.
  • A softmax over all nodes turns dot products into probabilities, and the objective is the log-likelihood of the observed pairs.
  • A uniform set of vectors is a baseline; a learned set scores higher on the observed pairs.
  • Classic methods learn one vector per node and need retraining for new nodes; inductive methods such as GraphSAGE learn a function instead.
  • Read next: Graph Convolutional Network shows how neighbourhood averaging is built into a neural network, and Word Embedding is the text analogue this idea borrows from.

Check yourself

QuestionIn one sentence, what does a node embedding give each node, and what property should nearby nodes have?Show answer

It gives each node a vector of numbers, and nodes that are close in the graph should have vectors with a large dot product, so they point in similar directions.

QuestionUsing zA=(1,0)z_A = (1, 0) and zC=(0,1)z_C = (0, 1), compute zA⋅zCz_A \cdot z_C and say what it means for A and C.Show answer

The dot product is 1×0+0×1=01 \times 0 + 0 \times 1 = 0. A zero dot product means the vectors are perpendicular, so the model gives A and C no similarity. Their nodes are two steps apart in the path, so they are only weakly related.

QuestionA graph has 4 nodes. If every node has the same vector, what is the probability P(u given v) for any pair under the softmax?Show answer

Every dot product is equal, so every term in the softmax is equal and the probability is 1/41/4 for each node. Each probability equals the uniform baseline.

QuestionWhy can a classic node embedding, learned from random walks on a training graph, fail on a node that was added later?Show answer

The classic method learns one vector for each node seen during training. A new node has no learned vector, so the model cannot embed it without retraining. Inductive methods learn a function from neighbourhood to vector, which can be applied to new nodes.

Sources