Graph samplers¶

dgl.contrib.sampling.sampler.
NeighborSampler
(g, batch_size, expand_factor=None, num_hops=1, neighbor_type='in', transition_prob=None, seed_nodes=None, shuffle=False, num_workers=1, prefetch=False, add_self_loop=False)[source]¶ Create a sampler that samples neighborhood.
It returns a generator of
NodeFlow
. This can be viewed as an analogy of minibatch training on graph data – the given graph represents the whole dataset and the returned generator produces minibatches (in the form ofNodeFlow
objects).A NodeFlow grows from sampled nodes. It first samples a set of nodes from the given
seed_nodes
(or all the nodes if not given), then samples their neighbors and extracts the subgraph. If the number of hops is \(k(>1)\), the process is repeated recursively, with the neighbor nodes just sampled become the new seed nodes. The result is a graph we defined asNodeFlow
that contains \(k+1\) layers. The last layer is the initial seed nodes. The sampled neighbor nodes in layer \(i+1\) are in layer \(i\). All the edges are from nodes in layer \(i\) to layer \(i+1\).As an analogy to minibatch training, the
batch_size
here is equal to the number of the initial seed nodes (number of nodes in the last layer). The number of nodeflow objects (the number of batches) is calculated bylen(seed_nodes) // batch_size
(ifseed_nodes
is None, then it is equal to the set of all nodes in the graph).Note: NeighborSampler currently only supprts immutable graphs.
Parameters:  g (DGLGraph) – The DGLGraph where we sample NodeFlows.
 batch_size (int) – The batch size (i.e, the number of nodes in the last layer)
 expand_factor (int, float, str) –
The number of neighbors sampled from the neighbor list of a vertex. The value of this parameter can be:
 int: indicates the number of neighbors sampled from a neighbor list.
 float: indicates the ratio of the sampled neighbors in a neighbor list.
 str: indicates some common ways of calculating the number of sampled neighbors,
e.g.,
sqrt(deg)
.
Note that no matter how large the expand_factor, the max number of sampled neighbors is the neighborhood size.
 num_hops (int, optional) – The number of hops to sample (i.e, the number of layers in the NodeFlow). Default: 1
 neighbor_type (str, optional) –
Indicates the neighbors on different types of edges.
 ”in”: the neighbors on the inedges.
 ”out”: the neighbors on the outedges.
Default: “in”
 transition_prob (str, optional) –
A 1D tensor containing the (unnormalized) transition probability.
The probability of a node v being sampled from a neighbor u is proportional to the edge weight, normalized by the sum over edge weights grouping by the destination node.
In other words, given a node v, the probability of node u and edge (u, v) included in the NodeFlow layer preceding that of v is given by:
\[p(u, v) = \frac{w_{u, v}}{\sum_{u', (u', v) \in E} w_{u', v}}\]If neighbor type is “out”, then the probability is instead normalized by the sum grouping by source node:
\[p(v, u) = \frac{w_{v, u}}{\sum_{u', (v, u') \in E} w_{v, u'}}\]If a str is given, the edge weight will be loaded from the edge feature column with the same name. The feature column must be a scalar column in this case.
Default: None
 seed_nodes (Tensor, optional) – A 1D tensor list of nodes where we sample NodeFlows from. If None, the seed vertices are all the vertices in the graph. Default: None
 shuffle (bool, optional) – Indicates the sampled NodeFlows are shuffled. Default: False
 num_workers (int, optional) – The number of worker threads that sample NodeFlows in parallel. Default: 1
 prefetch (bool, optional) – If true, prefetch the samples in the next batch. Default: False
 add_self_loop (bool, optional) – If true, add self loop to the sampled NodeFlow. The edge IDs of the self loop edges are 1. Default: False

dgl.contrib.sampling.sampler.
LayerSampler
(g, batch_size, layer_sizes, neighbor_type='in', node_prob=None, seed_nodes=None, shuffle=False, num_workers=1, prefetch=False)[source]¶ Create a sampler that samples neighborhood.
This creates a NodeFlow loader that samples subgraphs from the input graph with layerwise sampling. This sampling method is implemented in C and can perform sampling very efficiently.
The NodeFlow loader returns a list of NodeFlows. The size of the NodeFlow list is the number of workers.
Note: LayerSampler currently only supprts immutable graphs.
Parameters:  g (DGLGraph) – The DGLGraph where we sample NodeFlows.
 batch_size (int) – The batch size (i.e, the number of nodes in the last layer)
 layer_size (int) – A list of layer sizes.
 neighbor_type (str, optional) –
Indicates the neighbors on different types of edges.
 ”in”: the neighbors on the inedges.
 ”out”: the neighbors on the outedges.
Default: “in”
 node_prob (Tensor, optional) – A 1D tensor for the probability that a neighbor node is sampled. None means uniform sampling. Otherwise, the number of elements should be equal to the number of vertices in the graph. It’s not implemented. Default: None
 seed_nodes (Tensor, optional) – A 1D tensor list of nodes where we sample NodeFlows from. If None, the seed vertices are all the vertices in the graph. Default: None
 shuffle (bool, optional) – Indicates the sampled NodeFlows are shuffled. Default: False
 num_workers (int, optional) – The number of worker threads that sample NodeFlows in parallel. Default: 1
 prefetch (bool, optional) – If true, prefetch the samples in the next batch. Default: False
Distributed sampler¶

class
dgl.contrib.sampling.dis_sampler.
SamplerPool
[source]¶ SamplerPool is an abstract class, in which the worker() method should be implemented by users. SamplerPool will fork() N (N = num_worker) child processes, and each process will perform worker() method independently. Note that, the fork() API uses shared memory for N processes and the OS will perfrom copyonwrite on that only when developers write that piece of memory. So fork N processes and load N copies of graph will not increase the memory overhead.
For example, users can use this class like this:
class MySamplerPool(SamplerPool):
 def worker(self):
 # Do anything here #
 if __name__ == ‘__main__’:
 … args = parser.parse_args() pool = MySamplerPool() pool.start(args.num_sender, args)
SamplerPool.start (num_worker, args) 
Start sampler pool 
SamplerPool.worker (args) 
Userdefined function for worker 

class
dgl.contrib.sampling.dis_sampler.
SamplerSender
(namebook, net_type='socket')[source]¶ SamplerSender for DGL distributed training.
Users use SamplerSender to send sampled subgraphs (NodeFlow) to remote SamplerReceiver. Note that, a SamplerSender can connect to multiple SamplerReceiver currently. The underlying implementation will send different subgraphs to different SamplerReceiver in parallel via multithreading.
Parameters:
SamplerSender.send (nodeflow, recv_id) 
Send sampled subgraph (NodeFlow) to remote trainer. 
SamplerSender.signal (recv_id) 
When the samplling of each epoch is finished, users can invoke this API to tell SamplerReceiver that sampler has finished its job. 

class
dgl.contrib.sampling.dis_sampler.
SamplerReceiver
(graph, addr, num_sender, net_type='socket')[source]¶ SamplerReceiver for DGL distributed training.
Users use SamplerReceiver to receive sampled subgraphs (NodeFlow) from remote SamplerSender. Note that SamplerReceiver can receive messages from multiple SamplerSenders concurrently by given the num_sender parameter. Only when all SamplerSenders connected to SamplerReceiver successfully, SamplerReceiver can start its job.
Parameters:
SamplerReceiver.__iter__ () 
Sampler iterator 
SamplerReceiver.__next__ () 
Return sampled NodeFlow object 