What is the most efficient way to create batch graphs from batched adjacent matrix/ or (edge lists)

Hi, everyone

I am facing one problem when using dgl with current reinforcement learning libraries. These libraries assume that the input graph is stored as a tensor, for example, the adjacent matrix ( bs * N * N), or edge list (bs * max_num_edge * 2).

Suppose I have the graphs stored as tensors with shape ( bs * N * N). When I need to use these graph for the downstream tasks, I have to manually turn them one by one to dgl.graph, and then use the dgl.batch to concat them. I think this way using for loop is a liitle ugly. So I am wondering that if there is an efficient way without using the for loop.

Thanks in advance!

I think the best way is to directly create a block-diagonal graph and set its batch size. For you example, you could either use a scipy sparse matrix or concatenate all the edges into two node tensors, and then create a DGLGraph from it. Please read the doc of dgl.batch for how to relabel the node IDs properly. Once that is done, use g.set_batch_num_nodes and g.set_batch_num_edges to set batch size.

1 Like

Thanks for your suggestions. Indeed, I concatenated all the edged into two node tensors, and relabeled them manually.