Algorithm Search⌘K

Proofs: DeepWalk

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

DeepWalk is stochastic gradient descent on the skip-gram loss of its walks full

This proof shows: correctness, termination.

Let GG be a graph with V={0,…,n−1}V = \{0, \ldots, n-1\}, given by the out-lists Adj[v]\mathrm{Adj}[v], and let TT be a binary tree whose leaves are the nn vertices and in which every inner node has two children, so TT has n−1n - 1 inner nodes. For a vertex uu, let P(u)=((i1,s1),…,(iL,sL))P(u) = ((i_1, s_1), \ldots, (i_L, s_L)) be the inner nodes on the path from the root to the leaf of uu, with sl=+1s_l = +1 for a step to the left child and sl=−1s_l = -1 for a step to the right child. For x∈Rdx \in \mathbb{R}^d and vectors Ψi\Psi_i, write

Pr⁡(u∣x)=∏(i,s)∈P(u)σ(s Ψi⋅x),ℓu(x,Ψ)=−log⁡Pr⁡(u∣x)=−∑(i,s)∈P(u)log⁡σ(s Ψi⋅x),\Pr(u \mid x) = \prod_{(i, s) \in P(u)} \sigma(s\, \Psi_i \cdot x), \qquad \ell_u(x, \Psi) = -\log \Pr(u \mid x) = -\sum_{(i, s) \in P(u)} \log \sigma(s\, \Psi_i \cdot x),

with σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}).

Termination. Line L2 runs γ\gamma times and line L4 runs nn times per pass. A walk has at most tt vertices, so lines L6 and L7 run at most t⋅2wt \cdot 2w times per walk, and each tree path has at most n−1n - 1 nodes. Every loop is over a finite range, so the algorithm stops.

Lemma 1 (walks). Given its start vv, the sequence WW of line L5 has the distribution of the simple random walk on GG from vv, stopped after tt vertices or at the first vertex with no out-neighbour. In particular, consecutive vertices of WW are joined by an arc.

Proof. The walk starts at vv. While it has fewer than tt vertices and its last vertex xx has an out-neighbour, it appends an element of Adj[x]\mathrm{Adj}[x] drawn uniformly and independently of the past, which is the transition rule of the simple random walk; otherwise it stops. □\square

Lemma 2 (pairs). For a walk WW of length LL, lines L6 and L7 visit each ordered pair of positions (j,k)(j, k) with 1≤j,k≤L1 \le j, k \le L and 0<∣k−j∣≤w0 < |k - j| \le w exactly once, and no other pair.

Proof. Line L6 takes each jj once. Line L7 takes each kk in the range max⁡(1,j−w)≤k≤min⁡(L,j+w)\max(1, j - w) \le k \le \min(L, j + w) once and skips k=jk = j. These are exactly the kk with 1≤k≤L1 \le k \le L and 0<∣k−j∣≤w0 < |k - j| \le w. □\square

Lemma 3 (the model is a distribution). For every xx and every Ψ\Psi, Pr⁡(u∣x)>0\Pr(u \mid x) > 0 for each uu, and ∑u∈VPr⁡(u∣x)=1\sum_{u \in V} \Pr(u \mid x) = 1.

Proof. Each factor σ(⋅)\sigma(\cdot) is positive. For a node aa of TT, let π(a)\pi(a) be the product of σ(s Ψi⋅x)\sigma(s\, \Psi_i \cdot x) over the inner nodes ii strictly above aa on the path from the root, with the sign ss of the step taken at ii, so π(root)=1\pi(\mathrm{root}) = 1 and π(leaf of u)=Pr⁡(u∣x)\pi(\text{leaf of } u) = \Pr(u \mid x). We show by induction on the height of aa that the sum of π\pi over the leaves below aa equals π(a)\pi(a). For a leaf it is clear. For an inner node aa with children bb (left) and cc (right), π(b)=π(a) σ(Ψa⋅x)\pi(b) = \pi(a)\, \sigma(\Psi_a \cdot x) and π(c)=π(a) σ(−Ψa⋅x)\pi(c) = \pi(a)\, \sigma(-\Psi_a \cdot x), so by induction the sum below aa is π(a)(σ(z)+σ(−z))\pi(a)(\sigma(z) + \sigma(-z)) with z=Ψa⋅xz = \Psi_a \cdot x. Since σ(z)+σ(−z)=11+e−z+e−ze−z+1=1\sigma(z) + \sigma(-z) = \frac{1}{1 + e^{-z}} + \frac{e^{-z}}{e^{-z} + 1} = 1, this is π(a)\pi(a). At the root, the sum over all leaves is 11. □\square

Lemma 4 (one update is one gradient step). Fix a pair of line L7, with x=ΦW[j]x = \Phi_{W[j]}, u=W[k]u = W[k] and the current Ψ\Psi. After lines L8 and L9,

Ψinew=Ψi−α ∂ℓu∂Ψi(x,Ψ) for each (i,s)∈P(u),xnew=x−α ∂ℓu∂x(x,Ψ),\Psi_i^{\text{new}} = \Psi_i - \alpha\, \frac{\partial \ell_u}{\partial \Psi_i}(x, \Psi) \text{ for each } (i, s) \in P(u), \qquad x^{\text{new}} = x - \alpha\, \frac{\partial \ell_u}{\partial x}(x, \Psi),

and no other vector changes.

Proof. Since σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)), ddz(−log⁡σ(z))=−(1−σ(z))\frac{d}{dz}\bigl(-\log \sigma(z)\bigr) = -(1 - \sigma(z)). The inner nodes on P(u)P(u) are distinct, so each Ψi\Psi_i with (i,s)∈P(u)(i, s) \in P(u) occurs in exactly one term of ℓu\ell_u, and by the chain rule

∂ℓu∂Ψi=−(1−σ(s Ψi⋅x)) s x,∂ℓu∂x=−∑(i,s)∈P(u)(1−σ(s Ψi⋅x)) s Ψi,\frac{\partial \ell_u}{\partial \Psi_i} = -(1 - \sigma(s\, \Psi_i \cdot x))\, s\, x, \qquad \frac{\partial \ell_u}{\partial x} = -\sum_{(i, s) \in P(u)} (1 - \sigma(s\, \Psi_i \cdot x))\, s\, \Psi_i,

while ℓu\ell_u does not depend on the other Ψi\Psi_i or on the other rows of Φ\Phi.

Line L9 handles the nodes of P(u)P(u) in turn. When it reaches (i,s)(i, s), the vector Ψi\Psi_i has not yet changed during this pair, because the nodes are distinct, and xx has not changed, because xx is written only after the loop; the vectors Ψi\Psi_i belong to inner nodes and are separate from the rows of Φ\Phi, so writing Ψi\Psi_i does not touch xx, even when u=W[j]u = W[j]. So g=α(1−σ(s Ψi⋅x)) sg = \alpha (1 - \sigma(s\, \Psi_i \cdot x))\, s is computed from the values before the pair, and Ψinew=Ψi+g x=Ψi−α ∂ℓu/∂Ψi\Psi_i^{\text{new}} = \Psi_i + g\, x = \Psi_i - \alpha\, \partial \ell_u / \partial \Psi_i. The accumulator receives g Ψig\, \Psi_i with the old Ψi\Psi_i, read before the update. After the loop it holds ∑(i,s)α(1−σ(s Ψi⋅x)) s Ψi=−α ∂ℓu/∂x\sum_{(i,s)} \alpha (1 - \sigma(s\, \Psi_i \cdot x))\, s\, \Psi_i = -\alpha\, \partial \ell_u / \partial x, and adding it to xx gives the claim. □\square

Theorem. DEEPWALK stops and returns Φ\Phi after the following process: it draws γn\gamma n random walks as in Lemma 1, γ\gamma from each vertex, and for each walk, in the order of Lemma 2, it takes for each pair of positions (j,k)(j, k) one gradient step of size α\alpha on the loss ℓW[k]\ell_{W[k]} of the probability distribution of Lemma 3, at the current values of ΦW[j]\Phi_{W[j]} and Ψ\Psi.

Proof. Termination was shown above. Lines L2 to L4 start one walk at each vertex in each of the γ\gamma passes. By Lemma 1 each walk is a stopped simple random walk, by Lemma 2 the inner loops visit exactly the pairs of positions at most ww apart, once each, by Lemma 3 the loss of each pair is the negative logarithm of a probability distribution on VV, and by Lemma 4 each visit performs exactly one gradient step on that loss and changes nothing else. Line L10 returns Φ\Phi. ■\blacksquare

So DeepWalk is stochastic gradient descent, with the samples generated online, on ∑(v,u)−log⁡Pr⁡(u∣Φv)\sum_{(v, u)} -\log \Pr(u \mid \Phi_v), the sum over all pairs of its walks. The loss is not convex in (Φ,Ψ)(\Phi, \Psi), so the theorem says nothing about reaching a global minimum.

Back to DeepWalk