Negative graph in a batched graph

Hi,

when creating the negative graph from a batched graph for link predicition, nodes from each subgraph doesnt mix with eachother, right?

Thanks.

EdgeDataLoader treats a batched graph as a single large graph, so the nodes will mix with each other if you use the default builtin Uniform negative sampler.

That being said, you can write your own negative sampler so that each positive example only receive the negative examples from its own graph (which is possible with g.batch_num_nodes()).

1 Like

Thanks for your answer, once again. So how could I fix the following function? Fixing the source and destination nodes with g.batch_num_nodes() code?

def construct_negative_graph(graph, k, etype):

    utype, _, vtype = etype

    src, dst = graph.edges(etype=etype)

    neg_src = src.repeat_interleave(k)

    neg_dst = torch.randint(0, graph.num_nodes(vtype), (len(src) * k,))

    return dgl.heterograph(

        {etype: (neg_src, neg_dst)},

        num_nodes_dict={ntype: graph.num_nodes(ntype) for ntype in graph.ntypes})