Code Architecture — DeepWalk: Online Learning of Social Representations
This document describes the notebook structure, key functions, data flow, and the simplifications compared with the paper.
Section-by-Section Breakdown
1. Setup & Imports
Imports NumPy, NetworkX, matplotlib and scikit-learn (K-Means, t-SNE, NMI, silhouette, logistic regression). Seeds Python and NumPy with 42, sets JPEG figure output at dpi 80, and registers the atexit hook that saves the executed notebook on Kaggle.
2. Graph Construction (build_synthetic_graph)
Three communities of 25 nodes; each node links to 6 random nodes of its own community; 6 fixed bridge edges join the communities. Result: 75 nodes and 399 edges. A spring-layout plot shows the graph coloured by community.
3. Uniform Random Walk Generation
deepwalk_random_walk(G, start, walk_length): moves to a uniformly random neighbour at each step (Section 3.1).generate_deepwalk_walks(G, num_walks, walk_length): for each ofNUM_WALKS = 10passes, shuffles the nodes and starts one walk ofWALK_LENGTH = 20at every node (Algorithm 1). Output: 750 walks, 15,000 tokens.
4. Skip-gram with Negative Sampling
build_vocab(walks): maps nodes to indices and builds the unigram^0.75 negative-sampling distribution.skipgram_train(walks, vocab_size, dim=16, window_size=3, neg_samples=5, epochs=5, lr=0.025): two matricesW_inandW_outof shape (75, 16). For every (center, context) pair within the window, it raises σ(W_in[center] · W_out[context]) and lowers it for 5 negatives drawn from a 100,000-entry table. The learning rate decays linearly per epoch. ReturnsW_inas the embeddings (about 81,000 pairs per epoch).
5. Biased Random Walks (node2vec, for comparison)
alias_setup/alias_draw: alias tables for O(1) sampling.compute_node2vec_transition_probs(G, p, q): for each (previous, current) node pair, weights 1/p to go back, 1 to a common neighbour, 1/q otherwise.biased_random_walkandgenerate_node2vec_walks: the same walk counts and lengths as DeepWalk, with (p, q) = (1, 2) (BFS-like) and (1, 0.5) (DFS-like). Each set of walks is trained with the sameskipgram_train.
6. Evaluation & Comparison
evaluate_embeddings: t-SNE to 2D for the plot, K-Means with 3 clusters, NMI against the true communities, and the silhouette score.classify: the paper’s protocol (Section 6.1) for one label per node: logistic regression trained on 10% of the nodes (every community present), accuracy on the other 90%, mean and standard deviation over 10 random splits.- Four embeddings are compared: DeepWalk, node2vec BFS-like, node2vec DFS-like, and a random Gaussian embedding of the same shape as the baseline.
7. Graph Visualization with Learned Clusters
graph_with_clusters draws the graph four times: true communities, then the K-Means clusters of each method.
8. Summary
States what the saved run shows: DeepWalk NMI 1.0000 and accuracy 0.979 ± 0.016 with 10% labels, the random baseline at NMI 0.0194 and accuracy 0.338, all three walk types tied, and that the tiny graph is far easier than the paper’s datasets.
Data Flow / Shapes
Graph G (75 nodes, 399 edges)
→ walks: 750 lists of 20 node ids
→ skipgram_train → embeddings (75, 16)
→ K-Means (75,) cluster ids → NMI, silhouette
→ 10 splits × logistic regression (7 train / 68 test nodes) → accuracy
Deliberate Simplifications vs Full Paper
| Aspect | Paper | This Notebook |
|---|---|---|
| Graphs | BlogCatalog, Flickr, YouTube (10,312 to 1,138,499 nodes) | synthetic, 75 nodes, 3 communities |
| Walks per node γ | 80 | 10 |
| Walk length t | 40 | 20 |
| Window w | 10 | 3 |
| Dimensions d | 128 | 16 |
| Output layer | Hierarchical Softmax | negative sampling (5 negatives) |
| Training | asynchronous SGD, several workers | single-threaded NumPy, 5 epochs |
| Labels | multi-label, Micro-F1 and Macro-F1 | one community per node, accuracy |