Heterograph with weighted edges


I am trying to build a heterograph with multiple node types and multiple edge types and with weighted edges.

This answer How to build a weighted graph in dgl? helps me to convert from a networkx weighted graph to a dgl graph for a single edge type, but I don’t know how to then assign it to a heterograph, specifying the node types and edge type.

Any help would be greatly appreciated!

Best regards,

Is it possible to first create DGLHeteroGraph with single edge types from networkx using dgl.graph and then merge them with dgl.hetero_from_relations?


Thank you very much for your help! That is what I was trying to do, but using the “from_networkx” functionality as suggested in the other answer, I wasn’t able to figure out how to set the node types and edge type for each of the individual graphs as required to turn them into a heterograph.

Best regards,


You may build the dgl heterograph from raw data directly. For example, you have two types of nodes location and user. The weight vector of edges (User, Location) is UvsL. The weight vector of edges (Location, User) is LvsU. Then you can build a heterograph with weighted edges like this.

#Build the graph without weight features
G = dgl.heterograph({
(‘user’, ‘is-at’, ‘location’): sp.coo_matrix(UvsL),
(‘location’, ‘has’, ‘user’): sp.coo_matrix(LvsU),})

#Adding weight features
G.edges[‘is-at’].data[‘weight’] = torch.Tensor(sp.coo_matrix(UvsL).data)
G.edges[‘has’].data[‘weight’] = torch.Tensor(sp.coo_matrix(LvsU).data)