dgl.DGLGraph.flatten

DGLGraph.flatten()[source]

Remove all batching information of the graph, and regard the current graph as an independent graph rather then a batched graph. Graph topology and attributes would not be affected.

User can change the structure of the flattened graph.

Examples

>>> import dgl
>>> import torch as th
>>> g_list = []
>>> for _ in range(3)            # Create three graphs, each with #nodes 4
>>>     g = dgl.DGLGraph()
>>>     g.add_nodes(4)
>>>     g.add_edges([0,1,2,3], [1,2,3,0])
>>>     g.ndata['h'] = th.rand(4, 3)
>>>     g_list.append(g)
>>> bg = dgl.batch(g_list)
>>> bg.ndata
{'h': tensor([[0.0463, 0.1251, 0.5967],
        [0.8633, 0.9812, 0.8601],
        [0.7828, 0.3624, 0.7845],
        [0.2169, 0.8761, 0.3237],
        [0.1752, 0.1478, 0.5611],
        [0.5279, 0.2556, 0.2304],
        [0.8950, 0.8203, 0.5604],
        [0.2999, 0.2946, 0.2676],
        [0.3419, 0.2935, 0.6618],
        [0.8137, 0.8927, 0.8953],
        [0.6229, 0.7153, 0.5041],
        [0.5659, 0.0612, 0.2351]])}
>>> bg.batch_size
3
>>> bg.batch_num_nodes
[4, 4, 4]
>>> bg.batch_num_edges
[4, 4, 4]
>>> bg.flatten()
>>> bg.batch_size
1
>>> bg.batch_num_nodes
[12]
>>> bg.batch_num_edges
[12]
>>> bg.remove_nodes([1,3,5,7,9,11])
>>> bg.ndata
{'h': tensor([[0.0463, 0.1251, 0.5967],
        [0.7828, 0.3624, 0.7845],
        [0.1752, 0.1478, 0.5611],
        [0.8950, 0.8203, 0.5604],
        [0.3419, 0.2935, 0.6618],
        [0.6229, 0.7153, 0.5041]])}