Hard system-design machine-learning

When do graph embeddings help, and should you pick node2vec or GraphSAGE?

The 30 second answer

A graph helps when links such as product to brand, category or promoter carry signal that carts miss. node2vec turns random walks into sentences and trains a word2vec-style objective, but learns one vector per node, so new nodes wait for retraining. GraphSAGE learns a function over a node's features and sampled neighbours, so a new product gets a vector immediately. Always justify the graph first.

The word2vec idea. Skip-gram slides a window over text and trains each word's vector to predict nearby words; negative sampling replaces the full softmax by contrasting true context words with sampled noise words. It does not aim at meaning directly, but words used in similar contexts end up close.

Walk-based graph embeddings. DeepWalk treats truncated random walks as sentences and trains skip-gram on them. node2vec keeps that objective but uses a biased walk whose return and in-out parameters, p and q, trade breadth-first local structure against depth-first exploration. Nodes inside a walk's window are positives and sampled nodes are negatives. These methods are simple and cheap on a stable graph, but they are transductive: one vector per training node, so tomorrow's product has none until retraining. Plain walks also ignore node types in a mixed graph of products, brands and shoppers.

Inductive graph models. GraphSAGE learns aggregator functions that sample and combine neighbour features with the node's own features, usually over a couple of hops. Because the input includes the node's features, a new product linked only to its brand and category gets an embedding on listing day without retraining. That vector can feed the two-tower product tower as one more feature.

When not to. Almost anything can be drawn as a graph. Opinion: distant neighbours often add less than expected, so compare against a simple baseline such as averaging neighbour embeddings, and state why the graph structure matters for this problem before choosing a graph model.

Alternate interview scenarios

"Explain how word2vec learns, and why similar words end up close." word2vec has two architectures: CBOW predicts a word from its surrounding words, and skip-gram predicts surrounding words from a word. Training the full softmax over the vocabulary is expensive, so the later paper introduced negative sampling: push up the score of a real word-context pair and push down scores for a few sampled words, plus subsampling of very frequent words. Nothing labels meaning. Words that appear in similar contexts get similar gradients and therefore similar vectors. The same trick transfers to anything with sequences, such as user sessions or random walks on a graph.

Sources (5)
  1. Mikolov et al., Efficient Estimation of Word Representations in Vector Space (CBOW, skip-gram)
  2. Mikolov et al., Distributed Representations of Words and Phrases (negative sampling)
  3. Perozzi et al., DeepWalk
  4. Grover and Leskovec, node2vec
  5. Hamilton et al., Inductive Representation Learning on Large Graphs (GraphSAGE)