Search

Search papers and lessons

DeepWalk: Online Learning of Social Representations

Bryan Perozzi, Rami Al-Rfou, Steven Skiena · 2014

DeepWalk gives every node of a graph a short vector by treating random walks on the graph as sentences and training a word-embedding model on them.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Representation learning
  • Linear algebra
  • Stochastic processes
  • ReadReady to readNot yet

TL;DR

DeepWalk turns each node of a network into a short list of numbers, so that nodes in the same community get similar lists. It does this by sampling many short random walks over the graph and treating each walk as a sentence. A word-embedding model trained on those sentences then places each node in a space of 128 dimensions. The learned vectors work with a simple linear classifier, and they stay useful when most nodes have no labels.

Builds on: Graph, Random Walk, Skip-Gram Model · Leads to: Node Embedding, and the many later methods that replace the walk generator or the training objective

Why it mattered

Earlier network features were hand-built, such as degree counts, or came from spectral methods (see History below). DeepWalk showed that a generic language model could learn node features from walks alone. The features were cheap to compute, could be updated as the graph changed, and were good enough to beat stronger baselines when labels were scarce.

The idea spread quickly. Later methods such as node2vec keep the walk-then-train pipeline and change how the walks are biased. Graph embedding became a standard first step for tasks like recommendation on social platforms and classification of users in large networks. The paper also made the link explicit that structure in a graph and structure in language share a power-law frequency pattern, which the rest of the field has used since.

History

Before 2014, learning features for networks had mostly meant either building them by hand or using spectral methods that factor the adjacency matrix. Both were expensive or inflexible on large, changing graphs. Language modelling had seen a sequence of advances, notably word vectors learned by skip-gram models, which had shown that a simple objective could give useful representations at scale.

Perozzi, Al-Rfou and Skiena noticed that the frequency of vertices in short random walks follows a power law, as word frequencies do. They used that observation to carry the word-vector method over to graphs. The paper appeared in 2014. It caught on because it was simple to implement, fast, and strong on the multi-label classification benchmarks the community already used.

The problem

Start with a graph G=(V,E)G = (V, E), where VV is the set of members and EE the set of links between them. Some members carry labels, such as the interest groups a user belongs to. The usual approach to predicting labels from a graph is relational classification: each node's label depends on its neighbours' labels, and inference runs iteratively over the whole network. That approach is slow, and errors can cascade from one step to the next.

The paper asks for something different. It wants a matrix XEX_E with one row per node and only a few columns dd, learned from the graph structure alone, without looking at the labels. Once learned, the same matrix can be used for many different labelling tasks. The authors list four properties they want: the representation should adapt when the network changes, it should reflect community structure so that nearby vectors mean similar people, it should be low-dimensional so that it generalises when labels are scarce, and it should be continuous, so that decision boundaries are smooth.

How it works

We trace a tiny graph by hand. The graph is a path of four nodes, a−b−c−da - b - c - d. Use a window size w=1w = 1 and walks of length t=4t = 4.

  1. Start a walk at a random node. Pick aa as the root. A walk moves to a uniformly random neighbour at each step. Check: aa has one neighbour, bb, so the first step must go to bb.
  2. Continue the walk. From bb the neighbours are aa and cc, and say the walk picks cc. Then from cc the neighbours are bb and dd, and say it picks dd. The walk is [a,b,c,d][a, b, c, d], which has length 4 as required.
  3. Treat the walk as a sentence. The vertices are the vocabulary, and the walk is a sentence of four words. Nothing about the nodes' identities is used beyond which vertex each position holds.
  4. Slide a window over the walk. With w=1w = 1, each position is paired with the vertex one step to its left and one step to its right when they exist. This gives the pairs (centre, context): (a,b)(a, b), (b,a)(b, a), (b,c)(b, c), (c,b)(c, b), (c,d)(c, d), (d,c)(d, c). Check: the two interior nodes bb and cc give two pairs each, and the two ends aa and dd give one each, so 2+2+1+1=62 + 2 + 1 + 1 = 6.
  5. Update the vector of the centre node. For each pair, the algorithm adjusts the centre node's vector so that the context node becomes more likely. In the paper this is one stochastic gradient step on Φ\Phi.
  6. Repeat over many walks. The paper starts γ\gamma walks from every node, with γ=80\gamma = 80 in the main experiments. Each pass goes over the nodes in a random order, which the authors note speeds convergence.
  7. Read off the result. After training, row vv of Φ\Phi is the vector for node vv. Nodes bb and cc, which share many walks, tend to end up with similar vectors.

The model uses d=128d = 128 dimensions, window w=10w = 10, and walks of length t=40t = 40 in the main setup. Those are the paper's values, and the path example uses far smaller numbers only so you can check every step.

flowchart LR
    G[Graph G] --> W[Sample gamma random walks per node, length t]
    W --> S[Slide window of size w over each walk]
    S --> P[Pairs of centre and context vertices]
    P --> SG[SkipGram update of Phi, hierarchical softmax]
    SG --> PHI[Matrix Phi: one d-dimensional vector per node]

The math

Definition: DeepWalk objective

Let Φ∈R∣V∣×d\Phi \in \mathbb{R}^{|V| \times d} hold one vector Φ(v)\Phi(v) per node. For a walk W\mathcal{W} and window size ww, DeepWalk minimises the sum, over all centre positions viv_i in the walk, of the negative log probability of the context vertices within ww steps of viv_i: −log⁡Pr⁡(uk∣Φ(vi))-\log \Pr(u_k \mid \Phi(v_i)) for each context vertex uku_k.

Step 2 in symbols: the walk and the window

The walk Wvi\mathcal{W}_{v_i} is a sequence of vertices v1,v2,…,vtv_1, v_2, \ldots, v_t, where each vj+1v_{j+1} is a uniformly chosen neighbour of vjv_j. In symbols, if N(v)N(v) is the set of neighbours of vv, then Pr⁡(vj+1=u∣vj=v)=1/∣N(v)∣\Pr(v_{j+1} = u \mid v_j = v) = 1 / |N(v)| for u∈N(v)u \in N(v). The window around position jj is the set of positions j−wj - w to j+wj + w, other than jj itself, clipped to the walk's ends.

The number of (centre, context) pairs a walk produces is the sum, over positions ii, of the number of positions inside the window. On the four-node example with w=1w = 1, that is 1+2+2+1=61 + 2 + 2 + 1 = 6, as in step 4.

Step 3 in symbols: the pair likelihood

The paper replaces the joint probability of a sequence by a product over a context set, using the relaxation from language modelling in which one word predicts its context without order. For a centre vertex vv and context vertex uu, the model needs Pr⁡(u∣Φ(v))\Pr(u \mid \Phi(v)). Computing this exactly needs a normalising sum over all ∣V∣|V| vertices, which is too costly for graphs with millions of nodes.

Step 4 in symbols: hierarchical softmax

Assign each vertex to a leaf of a binary tree. The path from the root to a leaf uu is a sequence of tree nodes b0,b1,…,bLb_0, b_1, \ldots, b_L, with b0b_0 the root and bL=ub_L = u, where L=⌈log⁡2∣V∣⌉L = \lceil \log_2 |V| \rceil. Then

Pr⁡(u∣Φ(v))=∏l=1LPr⁡(bl∣Φ(v)).\Pr(u \mid \Phi(v)) = \prod_{l=1}^{L} \Pr(b_l \mid \Phi(v)).

Each internal tree node bb has its own parameter vector ψb\psi_b. The factor for the step from a parent node to a child is a logistic classifier. If σ(x)=1/(1+e−x)\sigma(x) = 1 / (1 + e^{-x}) is the logistic function, the probability of going left at a node is σ(Φ(v)⊤ψb)\sigma(\Phi(v)^\top \psi_b), and of going right is 1−σ(Φ(v)⊤ψb)1 - \sigma(\Phi(v)^\top \psi_b). Each factor is a logistic model, so the cost of one pair falls from a sum over ∣V∣|V| vertices to L=O(log⁡∣V∣)L = O(\log |V|) factors.

Step 5 in symbols: the update

The paper trains with stochastic gradient descent. For one pair (v,u)(v, u) with loss J(Φ)=−log⁡Pr⁡(u∣Φ(v))J(\Phi) = -\log \Pr(u \mid \Phi(v)), the update is

Φ(v)←Φ(v)−α∂J∂Φ(v),\Phi(v) \leftarrow \Phi(v) - \alpha \frac{\partial J}{\partial \Phi(v)},

where α\alpha is the learning rate. Here ∂J/∂Φ(v)\partial J / \partial \Phi(v) is the gradient with respect to the centre vector only, and the tree parameters ψb\psi_b are updated in the same pass. Because each update touches only a few vectors, parallel workers rarely conflict, which the paper uses to train without locks.

Step 6 in symbols: the walk budget

Each vertex starts γ\gamma walks of length tt, and each walk with window ww produces the number of pairs given in step 2. For a walk of length tt and window w≤tw \le t, the count is

∑i=1t(min⁡(i−1,w)+min⁡(t−i,w)).\sum_{i=1}^{t} \bigl(\min(i - 1, w) + \min(t - i, w)\bigr).

For the main setting this gives the pair count used in the worked examples below.

Worked examples

Example
Example 1: the four-node path

Graph a−b−c−da - b - c - d, walk [a,b,c,d][a, b, c, d], window w=1w = 1.

  1. Position 1 (aa) has one neighbour inside the window, bb. That gives the pair (a,b)(a, b).
  2. Position 2 (bb) has two, aa and cc. That gives (b,a)(b, a) and (b,c)(b, c).
  3. Position 3 (cc) has two, bb and dd. That gives (c,b)(c, b) and (c,d)(c, d).
  4. Position 4 (dd) has one, cc. That gives (d,c)(d, c).
  5. Total: 1+2+2+1=61 + 2 + 2 + 1 = 6 pairs.
  6. Check with the formula of step 6: positions i=1..4i = 1..4 give min⁡(i−1,1)=0,1,1,1\min(i - 1, 1) = 0, 1, 1, 1 on the left, sum 33, and min⁡(4−i,1)=1,1,1,0\min(4 - i, 1) = 1, 1, 1, 0 on the right, sum 33. Total 66.

Interpretation: nodes bb and cc are each centres of two pairs and so receive the most updates. This is one reason they tend to end up closer to each other than aa is to dd.

Example
Example 2: the main setting, one node's walks

Use t=40t = 40, w=10w = 10, γ=80\gamma = 80.

  1. Count pairs in one walk with step 6. For each position ii from 1 to 40, the left count is min⁡(i−1,10)\min(i - 1, 10). The values of i−1i - 1 run from 00 to 3939. The values 00 to 99 sum to 4545, and the remaining 3030 positions each contribute 1010, which is 300300.
  2. So the left total is 45+300=34545 + 300 = 345.
  3. By symmetry the right total is also 345345, since min⁡(40−i,10)\min(40 - i, 10) runs over the same values.
  4. One walk therefore gives 345+345=690345 + 345 = 690 pairs.
  5. Each vertex starts γ=80\gamma = 80 walks, so each vertex is the root of 80×690=55,20080 \times 690 = 55{,}200 pairs. Those pairs are spread over the whole walk, so most of them update other vertices as well.
  6. Check: 690=2⋅345690 = 2 \cdot 345 and 80⋅690=55,20080 \cdot 690 = 55{,}200.

Interpretation: a single vertex's walks produce tens of thousands of training pairs, so even a sparse node sees many updates. The paper's own sensitivity experiments found that most of the benefit comes once about 30 walks per node are used.

Example
Example 3: the realistic scale, BlogCatalog

BlogCatalog has ∣V∣=10,312|V| = 10{,}312 vertices and 3939 labels. Use the same walk settings as example 2.

  1. Walks: γ⋅∣V∣=80⋅10,312=824,960\gamma \cdot |V| = 80 \cdot 10{,}312 = 824{,}960 walks in total.
  2. Pairs: each walk gives 690690 pairs, so 824,960⋅690824{,}960 \cdot 690 pairs in total.
  3. Compute: 824,960⋅690=569,222,400824{,}960 \cdot 690 = 569{,}222{,}400, which is about 5.7×1085.7 \times 10^8 updates to the centre vectors.
  4. Each update does O(log⁡∣V∣)O(\log |V|) work with hierarchical softmax, here ⌈log⁡210,312⌉=14\lceil \log_2 10{,}312 \rceil = 14 tree factors.
  5. Result: about 5.7×1085.7 \times 10^8 updates of size 1414, which is manageable on one machine with several threads.
  6. The paper's parallel experiments show that the speed-up on BlogCatalog stays consistent up to 8 workers, with no loss of predictive performance relative to serial training.

Interpretation: the cost grows with the number of walks and the window, not with a dense matrix over the graph. This is why the authors can run the method on YouTube, with over a million vertices, where some baselines cannot run.

Try it: a walk of length t=5t = 5 with window w=1w = 1. How many pairs does it give? Answer: min⁡(i−1,1)\min(i-1,1) for i=1..5i = 1..5 gives 0,1,1,1,10, 1, 1, 1, 1, sum 44; min⁡(5−i,1)\min(5-i,1) gives 1,1,1,1,01, 1, 1, 1, 0, sum 44; total 88.

Results

The experiments are multi-label classification on three social networks: BlogCatalog with 10,312 vertices and 39 labels, Flickr with 80,513 vertices and 195 labels, and YouTube with 1,138,499 vertices and 47 labels. The protocol follows earlier papers: a random portion of the labelled nodes is used for training, the rest for testing, the split is repeated 10 times, and a one-vs-rest logistic regression is trained on the vectors. Results are reported as Macro-F1 and Micro-F1.

  • Sparse labels. The abstract reports F1 scores up to 10% higher than competing methods when labelled data is sparse, and that DeepWalk can match or beat baselines while using 60% less training data in some experiments.
  • BlogCatalog. With 20% of nodes labelled, DeepWalk beats EdgeCluster, Modularity and wvRN even when they are given 90% of the data. SpectralClustering is more competitive, but DeepWalk still wins on Macro-F1 when TR≤20%T_R \le 20\% and on Micro-F1 when TR≤60%T_R \le 60\%.
  • Flickr. DeepWalk beats all baselines by at least 3% on Micro-F1. With only 3% of the graph labelled it beats the other methods trained on 10%.
  • YouTube. At 1% labelled nodes, Micro-F1 improves by 14% over EdgeCluster, the scalable baseline. The lead narrows as labels grow, and ends at 3% on Micro-F1 and 5% on Macro-F1.
  • Parallelism. The speed-up on BlogCatalog and Flickr stays consistent up to 8 workers, with no loss of predictive performance relative to the serial run.

The limits are worth stating. The experiments use one machine with several threads, not a cluster. The paper compares with baselines from the years before its publication, and the claim of superiority is about those baselines and these datasets. The best dimensionality depends on how many labelled examples are available, so d=128d = 128 is not a universal setting.

Common mistakes and tradeoffs

It is tempting to think that DeepWalk reads the labels to learn its features. In fact the representation is learned from the graph alone. The labels are used only later, by a classifier trained on the vectors, which is why the same vectors can serve many tasks.

It is tempting to think that the walks must be long to capture community structure. In fact the paper uses short walks, t=40t = 40, and a window of w=10w = 10. Short local walks are enough, and they keep the sampling cheap and easy to parallelise.

It is tempting to think that random walks give the same information as the adjacency matrix. In fact each walk is a sample: it sees the local neighbourhood of its root, and the vectors are built from many such samples rather than from the full matrix. Some global detail is therefore only captured indirectly.

It is tempting to think that the method is a neural network in the usual sense. In fact there is no hidden layer; it is a skip-gram model over vertices with a tree of logistic classifiers. The "deep" in the name refers to unsupervised feature learning in the broad sense.

When to use it: when the graph is large, when labels are sparse, and when the features must be reused for several tasks. When not to: when the graph is small enough for spectral methods and labels are dense, or when edge attributes and directions matter a great deal. Alternatives include spectral embeddings, which give exact eigenvectors at a higher cost, and biased-walk variants such as node2vec.

Reading guide

Check yourself

QuestionIn the four-node path a−b−c−da - b - c - d with window w=1w = 1, how many (centre, context) pairs does the walk [a,b,c,d][a, b, c, d] produce?Show answer

Six. Position aa gives one pair, bb and cc give two each, and dd gives one. The total is 1+2+2+1=61 + 2 + 2 + 1 = 6.

QuestionWith t=40t = 40 and w=10w = 10, how many pairs does one walk produce?Show answer

Each side of the window sums to 345345: the first ten positions give 0+1+⋯+9=450 + 1 + \cdots + 9 = 45, and the other thirty give 1010 each, for 300300. The total is 345+345=690345 + 345 = 690.

QuestionWhy does DeepWalk use hierarchical softmax instead of a full softmax over all vertices?Show answer

A full softmax needs a normalising sum over all ∣V∣|V| vertices for every pair, which is too expensive for graphs with millions of nodes. Hierarchical softmax assigns each vertex to a leaf of a binary tree, so one probability is a product of ⌈log⁡2∣V∣⌉\lceil \log_2 |V| \rceil logistic factors, which is much cheaper.

QuestionA researcher says that DeepWalk uses the labels to learn its vectors and so would not generalise to a new labelling task. What is wrong with this?Show answer

The vectors are learned from the walks on the graph alone, without labels. Labels enter only in the classifier trained afterwards. So the same vectors can be reused for a new labelling task by training a new classifier, which is one of the design goals the paper states.

QuestionIn the paper's experiments, what does the sparse-label result mean in practice?Show answer

It means that DeepWalk's vectors, with a simple classifier, can reach the accuracy of baselines that are given far more labelled nodes, such as 20% labelled for DeepWalk against 90% for the baselines on BlogCatalog. Useful structure is learned without labels, so fewer labels are needed for a good classifier.

Sources

Read the full paper

Bryan Perozzi, Rami Al-Rfou, Steven Skiena, 2014. arXiv:1403.6652