dgl.data.utils.save_graphs

dgl.data.utils.save_graphs(filename, g_list, labels=None)[source]

Save DGLGraphs and graph labels to file

Parameters:
  • filename (str) – File name to store DGLGraphs.
  • g_list (list) – DGLGraph or list of DGLGraph
  • labels (dict (Default: None)) – labels should be dict of tensors/ndarray, with str as keys

Examples

>>> import dgl
>>> import torch as th

Create DGLGraph objects and initialize node and edge features.

>>> g1 = dgl.DGLGraph()
>>> g1.add_nodes(3)
>>> g1.add_edges([0, 0, 0, 1, 1, 2], [0, 1, 2, 1, 2, 2])
>>> g1.ndata["e"] = th.ones(3, 5)
>>> g2 = dgl.DGLGraph()
>>> g2.add_nodes(3)
>>> g2.add_edges([0, 1, 2], [1, 2, 1])
>>> g1.edata["e"] = th.ones(3, 4)

Save Graphs into file

>>> from dgl.data.utils import save_graphs
>>> graph_labels = {"glabel": th.tensor([0, 1])}
>>> save_graphs([g1, g2], "./data.bin", graph_labels)