Edge Sampling on Heterographs

Hi,
Newbie of GNN and DGL here. I am trying to implement a link prediction model with an heterogeneous graph. To train the graph, I need positive and negative samples of edges. To create a ‘positive’ subgraph (pos_g) and a ‘negative’ subgraph (neg_g), I rely on the dgl.contrib.sampling.EdgeSampler class. However, this class cannot work with an HeteroGraph. When i run my sampler, i get the following error :
/opt/dgl/include/dgl/packed_func_ext.h:117: Check failed: ObjectTypeChecker<TObjectRef>::Check(sptr.get()): Expected type graph.Graph but get graph.HeteroGraph

Here is the code I used to generate the error :

def edge_sampler(g, neg_sample_size, proportion_of_edges, edges=None, return_false_neg=False):
    num_of_edges = 0
    for etypes in g.canonical_etypes:
        num_of_edges += g.number_of_edges(etypes)
    sampler = dgl.contrib.sampling.EdgeSampler(g, batch_size=int(num_of_edges * proportion_of_edges),
                                               seed_edges=edges,
                                               neg_sample_size=neg_sample_size,
                                               negative_mode='tail',
                                               shuffle=True,
                                               return_false_neg=return_false_neg)
    sampler = iter(sampler)
    return next(sampler)

pos_g, neg_g = edge_sampler(g, neg_sample_size, proportion_of_edges, valid_eids, return_false_neg=True)

Is there a work around to use the EdgeSampler class on a HeteroGraph? If not, do you have any recommendations on how to do edge sampling on a HeteroGraph?

Thanks in advance!

Hi,
My experience of doing edge sampling is use th.randperm to general seed edges and then use edge_subgraph(https://docs.dgl.ai/en/latest/generated/dgl.edge_subgraph.html#dgl.edge_subgraph) to create the dgl.Graph.

For heterogeneous graph, you can feed {edge_type: eids} into edge_subgraph.
For negative sampling, how do you sample negative edges?

Hi,
Thank you for your answer, it works perfectly for generating positive edges.

For negative sampling, I would like to either ‘corrupt’ the head or the tail of the edge, e.g. when corrupting the tail I would randomly choose another node in the graph (and make sure that this new negative edge is not a real edge in the original graph).

I found out the documentation for the new 0.5.0 version about constructing negative graphs on heterographs, it is very helpful.

Thanks for your help!