r"""Batch a collection of :class:`DGLGraph` s into one graph for more efficient graph computation. Each input graph becomes one disjoint component of the batched graph. The nodes and edges are relabeled to be disjoint segments: ================= ========= ================= ===
(graphs, ndata=ALL, edata=ALL)
| 11 | |
| 12 | |
| 13 | def batch(graphs, ndata=ALL, edata=ALL): |
| 14 | r"""Batch a collection of :class:`DGLGraph` s into one graph for more efficient |
| 15 | graph computation. |
| 16 | |
| 17 | Each input graph becomes one disjoint component of the batched graph. The nodes |
| 18 | and edges are relabeled to be disjoint segments: |
| 19 | |
| 20 | ================= ========= ================= === ========= |
| 21 | graphs[0] graphs[1] ... graphs[k] |
| 22 | ================= ========= ================= === ========= |
| 23 | Original node ID 0 ~ N_0 0 ~ N_1 ... 0 ~ N_k |
| 24 | New node ID 0 ~ N_0 N_0 ~ N_0+N_1 ... \sum_{i=0}^{k-1} N_i ~ |
| 25 | \sum_{i=0}^k N_i |
| 26 | ================= ========= ================= === ========= |
| 27 | |
| 28 | Because of this, many of the computations on a batched graph are the same as if |
| 29 | performed on each graph individually, but become much more efficient |
| 30 | since they can be parallelized easily. This makes ``dgl.batch`` very useful |
| 31 | for tasks dealing with many graph samples such as graph classification tasks. |
| 32 | |
| 33 | For heterograph inputs, they must share the same set of relations (i.e., node types |
| 34 | and edge types) and the function will perform batching on each relation one by one. |
| 35 | Thus, the result is also a heterograph and has the same set of relations as the inputs. |
| 36 | |
| 37 | The numbers of nodes and edges of the input graphs are accessible via the |
| 38 | :func:`DGLGraph.batch_num_nodes` and :func:`DGLGraph.batch_num_edges` attributes |
| 39 | of the resulting graph. For homogeneous graphs, they are 1D integer tensors, |
| 40 | with each element being the number of nodes/edges of the corresponding input graph. For |
| 41 | heterographs, they are dictionaries of 1D integer tensors, with node |
| 42 | type or edge type as the keys. |
| 43 | |
| 44 | The function supports batching batched graphs. The batch size of the result |
| 45 | graph is the sum of the batch sizes of all the input graphs. |
| 46 | |
| 47 | By default, node/edge features are batched by concatenating the feature tensors |
| 48 | of all input graphs. This thus requires features of the same name to have |
| 49 | the same data type and feature size. One can pass ``None`` to the ``ndata`` |
| 50 | or ``edata`` argument to prevent feature batching, or pass a list of strings |
| 51 | to specify which features to batch. |
| 52 | |
| 53 | To unbatch the graph back to a list, use the :func:`dgl.unbatch` function. |
| 54 | |
| 55 | Parameters |
| 56 | ---------- |
| 57 | graphs : list[DGLGraph] |
| 58 | Input graphs. |
| 59 | ndata : list[str], None, optional |
| 60 | Node features to batch. |
| 61 | edata : list[str], None, optional |
| 62 | Edge features to batch. |
| 63 | |
| 64 | Returns |
| 65 | ------- |
| 66 | DGLGraph |
| 67 | Batched graph. |
| 68 | |
| 69 | Examples |
| 70 | -------- |
no test coverage detected