Some questions about the function "construct_negative_graph"

When the task is ‘link-prediction’, there is a function named “construct_negative_graph” for negative sampling, in other words, this function aims to generate a new negative_sampling_graph with edges that do not exist in the original graph. The code details of the function are as follows:

def construct_negative_graph(graph, k):
    src, dst = graph.edges()
    neg_src = src.repeat_interleave(k)
    neg_dst = torch.randint(0, graph.num_nodes(), (len(src) * k,))
    return dgl.graph((neg_src, neg_dst), num_nodes=graph.num_nodes())

MY QUESTION IS:
It looks like in the new graph obtained by negative sampling (neg_src, neg_dst) may have existing edges in the original graph(src, dst)?

For example:
First, we have 3 nodes node0, node1, node2 and the original graph is node0 \rightarrow node1 \rightarrow node2.
Then, the src, dst is [[0,1], [1,2]], let k = 1, hence, neg_src = [0,1]. neg_dst = randint(0,2,2), when neg_dst = [1,2], we get a negative_sampling_graph that is exactly the same as the original graph.

We have a dgl.dataloading.negative_sampler.GlobalUniform (dgl.dataloading.negative_sampler.GlobalUniform — DGL 0.8.1 documentation) negative sampler which currently supports returning true negative examples, excluding the positive examples.

Thanks for your reply! :melting_face:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.