MCPcopy Index your code
hub / github.com/dmlc/dgl / node2vec_random_walk

Function node2vec_random_walk

python/dgl/sampling/node2vec_randomwalk.py:11–100  ·  view source on GitHub ↗

Generate random walk traces from an array of starting nodes based on the node2vec model. Paper: `node2vec: Scalable Feature Learning for Networks `__. The returned traces all have length ``walk_length + 1``, where the first node is the starting

(
    g, nodes, p, q, walk_length, prob=None, return_eids=False
)

Source from the content-addressed store, hash-verified

9
10
11def node2vec_random_walk(
12 g, nodes, p, q, walk_length, prob=None, return_eids=False
13):
14 """
15 Generate random walk traces from an array of starting nodes based on the node2vec model.
16 Paper: `node2vec: Scalable Feature Learning for Networks
17 <https://arxiv.org/abs/1607.00653>`__.
18
19 The returned traces all have length ``walk_length + 1``, where the first node
20 is the starting node itself.
21
22 Note that if a random walk stops in advance, DGL pads the trace with -1 to have the same
23 length.
24
25 Parameters
26 ----------
27 g : DGLGraph
28 The graph. Must be on CPU.
29
30 Note that node2vec only support homogeneous graph.
31 nodes : Tensor
32 Node ID tensor from which the random walk traces starts.
33
34 The tensor must be on CPU, and must have the same dtype as the ID type
35 of the graph.
36 p: float
37 Likelihood of immediately revisiting a node in the walk.
38 q: float
39 Control parameter to interpolate between breadth-first strategy and depth-first strategy.
40 walk_length: int
41 Length of random walks.
42 prob : str, optional
43 The name of the edge feature tensor on the graph storing the (unnormalized)
44 probabilities associated with each edge for choosing the next node.
45
46 The feature tensor must be non-negative and the sum of the probabilities
47 must be positive for the outbound edges of all nodes (although they don&#x27;t have
48 to sum up to one). The result will be undefined otherwise.
49
50 If omitted, DGL assumes that the neighbors are picked uniformly.
51 return_eids : bool, optional
52 If True, additionally return the edge IDs traversed.
53
54 Default: False.
55
56 Returns
57 -------
58 traces : Tensor
59 A 2-dimensional node ID tensor with shape ``(num_seeds, walk_length + 1)``.
60 eids : Tensor, optional
61 A 2-dimensional edge ID tensor with shape ``(num_seeds, length)``.
62 Only returned if :attr:`return_eids` is True.
63
64 Examples
65 --------
66 >>> g1 = dgl.graph(([0, 1, 1, 2, 3], [1, 2, 3, 0, 0]))
67 >>> dgl.sampling.node2vec_random_walk(g1, [0, 1, 2, 0], 1, 1, walk_length=4)
68 tensor([[0, 1, 3, 0, 1],

Callers 2

time_randomwalkFunction · 0.90
sampleMethod · 0.90

Calls 1

cpuMethod · 0.45

Tested by

no test coverage detected