ShaDowKHopSampler

class dgl.dataloading.ShaDowKHopSampler(fanouts, replace=False, prob=None, prefetch_node_feats=None, prefetch_edge_feats=None, output_device=None)[source]

Bases: Sampler

K跳子图采样器来自具有浅层子图采样器的深度图神经网络

它执行节点级别的邻居采样,并返回由所有采样节点诱导的子图。从中采样邻居的种子节点将首先出现在子图的诱导节点中。

Parameters:
  • fanouts (list[int] or list[dict[etype, int]]) –

    List of neighbors to sample per edge type for each GNN layer, with the i-th element being the fanout for the i-th GNN layer.

    If only a single integer is provided, DGL assumes that every edge type will have the same fanout.

    If -1 is provided for one edge type on one layer, then all inbound edges of that edge type will be included.

  • replace (bool, 默认值 True) – 是否进行有放回的抽样

  • prob (str, optional) – 如果给定,每个邻居被采样的概率与g.edata中给定名称的边特征值成比例。该特征必须在每条边上是一个标量。

示例

节点分类

To train a 3-layer GNN for node classification on a set of nodes train_nid on a homogeneous graph where each node takes messages from 5, 10, 15 neighbors for the first, second, and third layer respectively (assuming the backend is PyTorch):

>>> g = dgl.data.CoraFullDataset()[0]
>>> sampler = dgl.dataloading.ShaDowKHopSampler([5, 10, 15])
>>> dataloader = dgl.dataloading.DataLoader(
...     g, torch.arange(g.num_nodes()), sampler,
...     batch_size=5, shuffle=True, drop_last=False, num_workers=4)
>>> for input_nodes, output_nodes, subgraph in dataloader:
...     print(subgraph)
...     assert torch.equal(input_nodes, subgraph.ndata[dgl.NID])
...     assert torch.equal(input_nodes[:output_nodes.shape[0]], output_nodes)
...     break
Graph(num_nodes=529, num_edges=3796,
      ndata_schemes={'label': Scheme(shape=(), dtype=torch.int64),
                     'feat': Scheme(shape=(8710,), dtype=torch.float32),
                     '_ID': Scheme(shape=(), dtype=torch.int64)}
      edata_schemes={'_ID': Scheme(shape=(), dtype=torch.int64)})

如果在异质图上进行训练,并且希望每种边类型有不同的邻居数量,则应提供一个字典列表。每个字典将指定每种边类型要选择的邻居数量。

>>> sampler = dgl.dataloading.ShaDowKHopSampler([
...     {('user', 'follows', 'user'): 5,
...      ('user', 'plays', 'game'): 4,
...      ('game', 'played-by', 'user'): 3}] * 3)

如果您希望进行非均匀邻居采样:

>>> g.edata['p'] = torch.rand(g.num_edges())   # any non-negative 1D vector works
>>> sampler = dgl.dataloading.ShaDowKHopSampler([5, 10, 15], prob='p')