DeepWalk: Scalable Representation Learning for Graphs
Graphs are ubiquitous data structures that underpin many real-world systems and phenomena, from social and information networks to biological interactions and chemical compounds. The recent explosion of graph-structured data has unleashed a wave of innovation in machine learning and AI, centered on the key challenge of learning useful feature representations of nodes and edges.
Consider the scale and richness of today‘s graph datasets:
- Facebook‘s social graph contains over 2.9 billion monthly active users and over 200 billion friend connections as of 2022 (Source)
- Microsoft‘s Bing web graph encompasses over 1 trillion web pages and 100 billion hyperlinks (Source)
- The Flickr social image graph contains over 3 billion images with over 40 billion associated tags, comments, and descriptions as of 2013 (Source)
As graph datasets grow in size and complexity, there is an urgent need for scalable, unsupervised techniques that can distill high-dimensional node features into compact, information-rich embeddings. These embeddings serve as the foundation for a wide range of machine learning tasks on graphs, including:
- Node classification: Predicting the label or category of a node based on its features and neighborhood structure
- Link prediction: Estimating the likelihood of a missing or future edge between two nodes
- Community detection: Discovering groups of nodes that are densely connected internally but sparsely connected externally
- Network visualization: Mapping nodes to 2D or 3D coordinates for interpretable plots of graph structure
- Anomaly detection: Identifying nodes or subgraphs that deviate significantly from patterns in the overall graph
- Graph-level classification: Categorizing entire graphs based on their topology and node/edge attributes
Traditional approaches to graph representation learning have relied on hand-engineered features, graph statistics, and kernel functions. However, these methods often struggle to capture complex, non-linear interactions between nodes and edges, and fail to adapt to the diverse characteristics of real-world graphs.
In 2014, a pioneering technique called DeepWalk was proposed by Perozzi et al. to automatically learn node embeddings from a graph using techniques from natural language processing. DeepWalk bridges the gap between graph analytics and word embeddings, drawing an analogy between nodes in a network and words in a text corpus.
The key insight of DeepWalk is that nodes that share similar neighborhoods and structural roles should have similar vector representations. To capture this idea, DeepWalk employs two main steps:
- Perform truncated random walks on the graph to extract sequences of nodes
- Train a skip-gram language model on the node sequences to learn a mapping from nodes to embeddings
In essence, DeepWalk treats nodes as "words" and generates "sentences" by randomly traversing the graph. The co-occurrence relationships between nodes in these sentences implicitly encode local graph structure, akin to how word co-occurrences in natural language encode semantic and syntactic relationships.
Formally, given an undirected graph $G = (V, E)$ with node set $V$ and edge set $E$, DeepWalk generates a corpus of random walks $\mathcal{S}$. Each random walk $S \in \mathcal{S}$ is a sequence of nodes $(v_1, v_2, \dots, v_n)$, where $v_i \in V$ and $(vi, v{i+1}) \in E$. The length of each walk and the number of walks per node are hyperparameters that control the tradeoff between capturing local and global graph structure.
After generating the corpus of random walks, DeepWalk employs the skip-gram architecture to learn node embeddings. Skip-gram is a shallow neural network with a single hidden layer that aims to predict the context nodes (i.e., neighbors) of a given target node. The objective function is:
$$\mathcal{L} = -\sum{S \in \mathcal{S}} \sum{vi \in S} \sum{-k \leq j \leq k, j \neq 0} \log p(v_{i+j} | v_i)$$
where $k$ is the context window size and $p(v_j | v_i)$ is the conditional probability of observing context node $v_j$ given target node $v_i$, parameterized by the dot product of their embeddings:
$$p(v_j | v_i) = \frac{\exp(\mathbf{v}_i^T \mathbf{v}j)}{\sum{u \in V} \exp(\mathbf{v}_i^T \mathbf{u})}$$
Here, $\mathbf{v}_i$ and $\mathbf{v}_j$ are the embedding vectors for nodes $v_i$ and $v_j$, respectively.
To efficiently approximate the expensive denominator term (which sums over all nodes in the graph), DeepWalk employs hierarchical softmax, a binary tree-based approximation that reduces the time complexity from $O(|V|)$ to $O(\log |V|)$ per training example.
The key steps of the DeepWalk algorithm are summarized in the pseudocode below:
function DeepWalk(G, walk_length, num_walks, embedding_dim, window_size):
walks = []
for v in G.nodes():
for i in range(num_walks):
walk = random_walk(G, v, walk_length)
walks.append(walk)
flatten_walks = [str(node) for walk in walks for node in walk]
model = Word2Vec(flatten_walks, vector_size=embedding_dim, window=window_size, min_count=0, sg=1, workers=4)
return model.wv
The learned embeddings capture both local community structure and global structural roles in the graph. Nodes that are closely connected in the graph and/or share similar "contexts" in the random walks will have similar vector representations in the embedding space.
Experimental results on benchmark datasets like BlogCatalog, Flickr, and YouTube have shown that DeepWalk embeddings can significantly outperform traditional graph features on tasks like node classification and link prediction. For example, on the BlogCatalog dataset with 10,312 nodes and 333,983 edges, DeepWalk achieves a micro-F1 score of 0.211 for multi-label node classification, compared to 0.176 for spectral clustering and 0.201 for edge clustering (Source).
The success of DeepWalk has inspired a line of related techniques for graph representation learning, including:
- node2vec (Grover & Leskovec, 2016): Extends DeepWalk with biased random walks that trade off between breadth-first and depth-first exploration, capturing both homophily and structural equivalence in the embeddings
- LINE (Tang et al., 2015): Learns node embeddings by directly modeling first-order and second-order proximity between nodes, without the need for random walks
- HARP (Chen et al., 2017): Proposes a hierarchical paradigm for graph representation learning that compresses the input graph into smaller graphs before applying techniques like DeepWalk or node2vec
- GraphSAGE (Hamilton et al., 2017): An inductive framework for learning node embeddings that leverages feature information to generate embeddings for previously unseen nodes
- Graph Attention Networks (Velickovic et al., 2018): Incorporates an attention mechanism into graph convolutional networks to learn node embeddings that weight the importance of different neighbors
The table below summarizes some of the key differences between these graph embedding techniques:
| Method | Random Walks? | Node Features? | Inductive? | Attention? |
|---|---|---|---|---|
| DeepWalk | Yes | No | No | No |
| node2vec | Yes | No | No | No |
| LINE | No | No | No | No |
| HARP | Yes | No | No | No |
| GraphSAGE | No | Yes | Yes | No |
| GAT | No | Yes | Yes | Yes |
While these newer techniques offer additional capabilities and flexibility, DeepWalk remains a simple yet powerful baseline for unsupervised graph representation learning. Its core idea of transforming graphs into linear sequences has been extended to other domains like point clouds (PointWalk), heterogeneous networks (MetaPath2Vec), and knowledge graphs (RDF2Vec).
In real-world applications, DeepWalk and its variants have achieved strong results on tasks such as:
- Social network analysis: Predicting user interests and demographics from social connections and interactions (e.g., Pinterest, Facebook)
- Recommender systems: Learning user and item embeddings for personalized recommendation (e.g., YouTube, Amazon)
- Biomedical networks: Discovering novel drug-target interactions and disease pathways from protein-protein interaction networks and electronic health records
- Urban computing: Modeling traffic flow and human mobility patterns from transportation networks and location-based social networks
As an example, researchers at Pinterest applied DeepWalk to learn user embeddings from the Pinterest pin-board graph, which were then used to recommend related pins and boards to users. The DeepWalk embeddings outperformed traditional content-based and collaborative filtering approaches, improving related pin recommendations by 8% (Source).
Looking ahead, there are many exciting directions for future research on graph representation learning, including:
- Scaling to billion-node graphs by leveraging distributed computing and GPUs
- Incorporating multiple types of node and edge features into the embedding process
- Learning dynamic embeddings for time-evolving graphs
- Generating human-interpretable explanations for node embeddings and downstream predictions
- Unifying the strengths of different graph embedding paradigms (e.g., combining GNNs with random walks)
- Developing standardized benchmarks and evaluation protocols for graph representation learning
As graph-structured data continues to grow in scale and importance, I believe that unsupervised representation learning techniques like DeepWalk will play an increasingly critical role in powering intelligent applications and discoveries. By democratizing access to high-quality node and edge features, DeepWalk and its successors can help unlock the full potential of graph machine learning for both researchers and practitioners.
To learn more about DeepWalk and graph representation learning, I recommend the following resources:
- "DeepWalk: Online Learning of Social Representations" by Perozzi et al. (https://arxiv.org/abs/1403.6652)
- "Representation Learning on Graphs: Methods and Applications" by Hamilton et al. (https://arxiv.org/abs/1709.05584)
- "Graph Representation Learning" by Liu et al. (https://www.cs.mcgill.ca/~wlh/grl_book/)
- "A Comprehensive Survey on Graph Neural Networks" by Wu et al. (https://arxiv.org/abs/1901.00596)
- "Deep Learning on Graphs" course by Jure Leskovec at Stanford (http://cs224w.stanford.edu/)
I hope this deep dive into DeepWalk has been informative and inspiring! Feel free to experiment with DeepWalk and other graph representation learning techniques on your own datasets, and share your findings with the community. The field of graph machine learning is still rapidly evolving, and there are many opportunities to make impactful contributions. Happy graph embedding!