均匀负采样器

class dgl.graphbolt.UniformNegativeSampler(datapipe, graph, negative_ratio)[source]

基础类:NegativeSampler

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

功能名称: sample_uniform_negative.

需要注意的是,术语“负”指的是假阴性,表示采样的对在图中的缺失性并未得到保证。对于每条边(u, v),应该生成negative_ratio对负边(u, v'),其中v'是从图中所有节点中均匀选择的。

Parameters:
  • datapipe (DataPipe) – The datapipe.

  • graph (FusedCSCSamplingGraph) – 要在其上执行负采样的图。

  • negative_ratio (int) – The proportion of negative samples to positive samples.

示例

>>> 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])