dgl.dataloading.negative_sampler.GlobalUniform

class dgl.dataloading.negative_sampler.GlobalUniform(k, exclude_self_loops=True, replace=False)[source]

Negative sampler that randomly chooses negative source-destination pairs according to a uniform distribution.

For each edge (u, v) of type (srctype, etype, dsttype), DGL generates at most k pairs of negative edges (u', v'), where u' is chosen uniformly from all the nodes of type srctype and v' is chosen uniformly from all the nodes of type dsttype. The resulting edges will also have type (srctype, etype, dsttype). DGL guarantees that the sampled pairs will not have edges in between.

Parameters
  • k (int) – The desired number of negative samples to generate per edge.

  • exclude_self_loops (bool, optional) – Whether to exclude self-loops from negative samples. (Default: True)

  • replace (bool, optional) – Whether to sample with replacement. Setting it to True will make things faster. (Default: False)

Notes

This negative sampler will try to generate as many negative samples as possible, but it may rarely return less than k negative samples per edge. This is more likely to happen if a graph is so small or dense that not many unique negative samples exist.

Examples

>>> g = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> neg_sampler = dgl.dataloading.negative_sampler.GlobalUniform(2, True)
>>> neg_sampler(g, torch.LongTensor([0, 1]))
(tensor([0, 1, 3, 2]), tensor([2, 0, 2, 1]))
__init__(k, exclude_self_loops=True, replace=False)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(k[, exclude_self_loops, replace])

Initialize self.