dgl.DGLGraph.add_nodes

DGLGraph.add_nodes(num, data=None)[source]

Add multiple new nodes.

Parameters:
  • num (int) – Number of nodes to be added.
  • data (dict, optional) – Feature data of the added nodes.

Notes

If new nodes are added with features, and any of the old nodes do not have some of the feature fields, those fields are filled by initializers defined with set_n_initializer (default filling with zeros).

Examples

>>> G = dgl.DGLGraph()
>>> g.add_nodes(2)
>>> g.number_of_nodes()
2
>>> g.add_nodes(3)
>>> g.number_of_nodes()
5

Adding new nodes with features:

Note

Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> g.add_nodes(2, {'x': th.ones(2, 4)})    # default zero initializer
>>> g.ndata['x']
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])