Quant Memo
Advanced

DeepWalk and Skip-Gram on Graphs

Turning a graph into embeddings by pretending random walks over nodes are sentences of words, then reusing the word2vec skip-gram model to learn which nodes tend to appear near each other.

Prerequisites: Word Embeddings and Word2Vec, Graph Neural Networks

DeepWalk's trick is to notice that a random walk over a graph looks a lot like a sentence: instead of words in a line, you get a sequence of nodes visited one after another. If two nodes keep showing up near each other across many random walks — the way "coffee" and "cup" keep showing up near each other across many sentences — they probably belong together, even if they're not directly connected. DeepWalk exploits this by generating thousands of random walks starting from every node, treating each walk as a "sentence," and feeding those sentences straight into skip-gram, the same neural model behind word2vec.

Skip-gram's job is simple: given a node in the middle of a walk, predict which nodes are likely to appear near it within a small window. Training this prediction task forces the model to assign each node a vector — an embedding — such that nodes with similar neighborhoods end up with similar vectors. A node representing a stock frequently co-traded near other momentum names, for instance, ends up embedded close to those names, purely from walk co-occurrence, with no hand-built features required.

The appeal for graphs of securities, counterparties, or supply chains is that DeepWalk needs only the graph's edges, no extra attributes, and it captures multi-hop structure that a simple "who is directly connected to whom" table misses — two stocks that never trade together directly but both cluster near the same intermediary names get pulled close together in embedding space, because random walks pass through that shared intermediary repeatedly.

DeepWalk generates random walks over a graph and feeds them to skip-gram exactly as word2vec feeds sentences to it, treating "nodes visited near each other in a walk" as the graph analogue of "words appearing near each other in text." The resulting embeddings place structurally similar nodes — even ones with no direct edge — close together, at the cost of ignoring any features on the nodes themselves.

Related concepts

Practice in interviews

Further reading

  • Perozzi, Al-Rfou & Skiena, DeepWalk (2014)
ShareTwitterLinkedIn