dgl.save_graphs(filename, g_list, labels=None, formats=None)[source]

Save graphs and optionally their labels to file.

Besides saving to local files, DGL supports writing the graphs directly to S3 (by providing a "s3://..." path) or to HDFS (by providing "hdfs://..." a path).

The function saves both the graph structure and node/edge features to file in DGL’s own binary format. For graph-level features, pass them via the labels argument.

  • filename (str) – The file name to store the graphs and labels.

  • g_list (list) – The graphs to be saved.

  • labels (dict[str, Tensor]) – labels should be dict of tensors, with str as keys

  • formats (str or list[str]) – Save graph in specified formats. It could be any combination of coo, csc and csr. If not specified, save one format only according to what format is available. If multiple formats are available, selection priority from high to low is coo, csc, csr.


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

Create DGLGraph objects and initialize node and edge features.

>>> g1 = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> g2 = dgl.graph(([0, 2], [2, 3]))
>>> g2.edata["e"] = th.ones(2, 4)

Save Graphs into file

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

See also