Algorithm Search⌘K

Proofs: node2vec

These are the human proofs of node2vec, at full length.

node2vec samples the second-order walk exactly and takes gradient steps on the skip-gram loss full

This proof shows: correctness, termination.

We use the notation of the page. G=(V,E)G = (V, E) is an undirected graph with positive weights ww, N(v)N(v) is the set of neighbours of vv, and p,q>0p, q > 0. For an edge {t,v}\{t, v\} and x∈N(v)x \in N(v) let α(t,x)=1/p\alpha(t, x) = 1/p if x=tx = t, 11 if x∈N(t)x \in N(t), and 1/q1/q otherwise, and π(t,v,x)=α(t,x) w(v,x)\pi(t, v, x) = \alpha(t, x)\, w(v, x).

Lemma 1 (the step distribution is defined). For every edge {t,v}\{t, v\}, the weights π(t,v,x)\pi(t, v, x) for x∈N(v)x \in N(v) are positive and their sum Z(t,v)Z(t, v) is positive, so P(x∣t,v)=π(t,v,x)/Z(t,v)P(x \mid t, v) = \pi(t, v, x) / Z(t, v) is a probability distribution on N(v)N(v).

Proof. Each α(t,x)\alpha(t, x) is one of 1/p1/p, 11 and 1/q1/q, which are positive, and each w(v,x)>0w(v, x) > 0, so every π(t,v,x)>0\pi(t, v, x) > 0. Since t∈N(v)t \in N(v), the set N(v)N(v) is not empty and Z(t,v)≥π(t,v,t)>0Z(t, v) \ge \pi(t, v, t) > 0. □\square

Lemma 2 (exact sampling). Let x1,…,xjx_1, \dots, x_j be the neighbours of vv with weights π1,…,πj\pi_1, \dots, \pi_j and Z=∑iπiZ = \sum_i \pi_i. Drawing UU uniform in [0,1)[0, 1) and returning the first xix_i with UZ<π1+⋯+πiU Z < \pi_1 + \dots + \pi_i returns xix_i with probability πi/Z\pi_i / Z.

Proof. The event that xix_i is returned is π1+⋯+πi−1≤UZ<π1+⋯+πi\pi_1 + \dots + \pi_{i-1} \le U Z < \pi_1 + \dots + \pi_i, an interval of length πi/Z\pi_i / Z for UU. These intervals partition [0,1)[0, 1). □\square

Lemma 3 (walks). A walk produced by lines L5 to L7 from a vertex uu is a sequence of vertices in which consecutive vertices are adjacent. If N(u)=∅N(u) = \emptyset or ℓ=1\ell = 1 it is [u][u]; otherwise it has exactly ℓ\ell vertices. Given its first i≥2i \ge 2 vertices ending in t,vt, v, the next vertex is xx with probability π(t,v,x)/Z(t,v)\pi(t, v, x) / Z(t, v), independently of the earlier vertices.

Proof. Line L5 appends a neighbour of uu when one exists and ℓ>1\ell > 1; by Lemma 2 with the weights w(u,⋅)w(u, \cdot) it is chosen in proportion to the weight. If no vertex was appended, the walk has one vertex and the condition 1<∣walk∣1 < |walk| of line L6 fails, so the walk is [u][u]. Otherwise the last two vertices t,vt, v are adjacent, and by Lemma 1 and Lemma 2 line L7 appends a neighbour xx of vv with the stated probability, which depends only on tt and vv. Then v,xv, x are adjacent, so the invariant "the last two vertices are adjacent" holds again, and each round adds one vertex, so the loop stops when the walk has ℓ\ell vertices. □\square

Lemma 4 (reduction to the weighted walk). If p=q=1p = q = 1, then P(x∣t,v)=w(v,x)/∑y∈N(v)w(v,y)P(x \mid t, v) = w(v, x) / \sum_{y \in N(v)} w(v, y), the first-order weighted random walk.

Proof. Every α(t,x)\alpha(t, x) equals 1. □\square

Lemma 5 (the update is a gradient step). For a pair (u,c)(u, c) with negative samples x1,…,xsx_1, \dots, x_s, let λ(zu)=−log⁡σ(zu⋅yc)−∑ilog⁡σ(−zu⋅yxi)\lambda(z_u) = -\log \sigma(z_u \cdot y_c) - \sum_{i} \log \sigma(-z_u \cdot y_{x_i}), with σ(a)=1/(1+e−a)\sigma(a) = 1/(1 + e^{-a}). Then

∇zuλ=−(1−σ(zu⋅yc)) yc+∑iσ(zu⋅yxi) yxi,\nabla_{z_u} \lambda = -(1 - \sigma(z_u \cdot y_c))\, y_c + \sum_i \sigma(z_u \cdot y_{x_i})\, y_{x_i},

and the analogous formulas hold for each context vector. The implementation adds η(ℓj−σ(zu⋅yj)) yj\eta (\ell_j - \sigma(z_u \cdot y_j))\, y_j to zuz_u for each target jj with label ℓj=1\ell_j = 1 for cc and 00 for the samples, which is −η∇zuλ-\eta \nabla_{z_u} \lambda.

Proof. ddalog⁡σ(a)=1−σ(a)\frac{d}{da} \log \sigma(a) = 1 - \sigma(a) and ddalog⁡σ(−a)=−σ(a)\frac{d}{da} \log \sigma(-a) = -\sigma(a); apply the chain rule with a=zu⋅yja = z_u \cdot y_j, whose gradient in zuz_u is yjy_j. For the label 1 term, −(1−σ) yc-(1 - \sigma)\, y_c; for a label 0 term, σ yxi\sigma\, y_{x_i}. Negating and scaling by η\eta gives η(1−σ) yc\eta(1 - \sigma)\, y_c and η(0−σ) yxi\eta(0 - \sigma)\, y_{x_i}. □\square

Theorem. NODE2VEC stops, and its walks are independent samples of the second-order walk of Grover and Leskovec, rr from every vertex, each with ℓ\ell vertices (or one vertex from an isolated vertex). Its vectors are the result of stochastic gradient steps on the skip-gram loss with negative sampling over the pairs of these walks.

Proof. Lines L3 and L4 run rnr n times, each walk has at most ℓ\ell vertices by Lemma 3, and line L9 visits each of the at most rnℓr n \ell positions of the walks, with at most 2k2k context vertices and ss samples each, a finite number of updates. So the algorithm stops. The walks have the stated distribution by Lemma 3 and use fresh random numbers, so they are independent given the graph. The learning phase consists of the updates of Lemma 5. ■\blacksquare

Back to node2vec