Implementing a batch of batches

Hi everyone,

I have a big training set of about hundreds of independent graphs, with around 406660 nodes each. I’m trying to use dgl.batch to batch the different independent graphs, and then use dgl.contrib.sampling.NeighborSampler to randomly sample some of the nodes and their neighbourhood, since my independent graphs are pretty large. However, I get this error message:

Traceback (most recent call last):
  File "", line 169, in <module>
  File "/home/cc91/.local/lib/python3.7/site-packages/absl/", line 299, in run
    _run_main(main, args)
  File "/home/cc91/.local/lib/python3.7/site-packages/absl/", line 250, in _run_main
  File "", line 123, in train
    expand_factor = expand_factor):
  File "/home/cc91/.local/lib/python3.7/site-packages/dgl/contrib/sampling/", line 320, in __init__
  File "/home/cc91/.local/lib/python3.7/site-packages/dgl/contrib/sampling/", line 154, in __init__
    raise NotImplementedError("This loader only support read-only graphs.")
NotImplementedError: This loader only support read-only graphs.

However, as I understand the batched graphs are already readonly. Anyone has an idea of what is going on?

This is the part of the code concerned,

     optimizer = torch.optim.Adam(model.parameters(),
      criterion = nn.MSELoss()
      print("Start training...")
      start = time.time()
      for epoch in range(FLAGS.n_epochs):
          loss_list = []
          for batch, data in enumerate(train_dataloader):
              graph, labels = data
              for nf in dgl.contrib.sampling.NeighborSampler(graph,
                                                         expand_factor = expand_factor):
                  nf.ndata['features'] = nf.ndata['features'].to(device)

                  labels =
                  logits = model.forward(nf, nf.ndata['features'])
                  loss = criterion(logits, labels)

The output of print(type(graph)) is <class ‘dgl.batched_graph.BatchedDGLGraph’>, which seems to be correct.

This looks like a bug to me and I’ve reported it in issue #1148. Could you please try graph._graph.readonly(True) after graph, labels = data and see if the issue gets resolved?

Yes, thank you. I think the documentation is outdated.

Also, I’m still a bit confused about how to use NeighborSampler. How can I access the graph ndata? I need it to do the forward pass (model.forward(graph, graph.ndata[‘features’])). Also, do I need to modify the update_all function when using NeighborSampler?

NeighborSampler returns instances of a different data structure called NodeFlow. You may find this tutorial to be helpful.

Thanks! Do you know whether there is any pytorch example using NeighborSampler? It’d be helpful.

This one might be helpful:

1 Like