UniformNegativeSampler

class dgl.graphbolt.UniformNegativeSampler(datapipe, graph, negative_ratio)[源代码]

基类:NegativeSampler

基于均匀分布为每个源节点采样负目标节点。

函数名:sample_uniform_negative

需要注意的是,术语“负样本”指的是假负样本,表明采样的对不保证不存在于图中。对于每条边 (u, v),它应该生成 negative_ratio 对负样本边 (u, v'),其中 v' 是从图中的所有节点中均匀选择的。

参数
  • datapipe (DataPipe) – 数据管道。

  • graph (FusedCSCSamplingGraph) – 执行负采样的图。

  • negative_ratio (int) – 负样本与正样本的比例。

示例

>>> from dgl import graphbolt as gb
>>> indptr = torch.LongTensor([0, 1, 2, 3, 4])
>>> indices = torch.LongTensor([1, 2, 3, 0])
>>> graph = gb.fused_csc_sampling_graph(indptr, indices)
>>> seeds = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0]])
>>> item_set = gb.ItemSet(seeds, names="seeds")
>>> item_sampler = gb.ItemSampler(
...     item_set, batch_size=4,)
>>> neg_sampler = gb.UniformNegativeSampler(
...     item_sampler, graph, 2)
>>> for minibatch in neg_sampler:
...       print(minibatch.seeds)
...       print(minibatch.labels)
...       print(minibatch.indexes)
tensor([[0, 1], [1, 2], [2, 3], [3, 0], [0, 1], [0, 3], [1, 1], [1, 2],
    [2, 1], [2, 0], [3, 0], [3, 2]])
tensor([1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.])
tensor([0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3])