r"""Add new nodes of the same node type Parameters ---------- num : int Number of nodes to add. data : dict, optional Feature data of the added nodes. ntype : str, optional The type of the new nodes. Can be omitted if there
(self, num, data=None, ntype=None)
| 281 | ################################################################# |
| 282 | |
| 283 | def add_nodes(self, num, data=None, ntype=None): |
| 284 | r"""Add new nodes of the same node type |
| 285 | |
| 286 | Parameters |
| 287 | ---------- |
| 288 | num : int |
| 289 | Number of nodes to add. |
| 290 | data : dict, optional |
| 291 | Feature data of the added nodes. |
| 292 | ntype : str, optional |
| 293 | The type of the new nodes. Can be omitted if there is |
| 294 | only one node type in the graph. |
| 295 | |
| 296 | Notes |
| 297 | ----- |
| 298 | |
| 299 | * Inplace update is applied to the current graph. |
| 300 | * If the key of ``data`` does not contain some existing feature fields, |
| 301 | those features for the new nodes will be created by initializers |
| 302 | defined with :func:`set_n_initializer` (default initializer fills zeros). |
| 303 | * If the key of ``data`` contains new feature fields, those features for |
| 304 | the old nodes will be created by initializers defined with |
| 305 | :func:`set_n_initializer` (default initializer fills zeros). |
| 306 | * This function discards the batch information. Please use |
| 307 | :func:`dgl.DGLGraph.set_batch_num_nodes` |
| 308 | and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph |
| 309 | to maintain the information. |
| 310 | |
| 311 | Examples |
| 312 | -------- |
| 313 | |
| 314 | The following example uses PyTorch backend. |
| 315 | |
| 316 | >>> import dgl |
| 317 | >>> import torch |
| 318 | |
| 319 | **Homogeneous Graphs or Heterogeneous Graphs with A Single Node Type** |
| 320 | |
| 321 | >>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2]))) |
| 322 | >>> g.num_nodes() |
| 323 | 3 |
| 324 | >>> g.add_nodes(2) |
| 325 | >>> g.num_nodes() |
| 326 | 5 |
| 327 | |
| 328 | If the graph has some node features and new nodes are added without |
| 329 | features, their features will be created by initializers defined |
| 330 | with :func:`set_n_initializer`. |
| 331 | |
| 332 | >>> g.ndata['h'] = torch.ones(5, 1) |
| 333 | >>> g.add_nodes(1) |
| 334 | >>> g.ndata['h'] |
| 335 | tensor([[1.], [1.], [1.], [1.], [1.], [0.]]) |
| 336 | |
| 337 | We can also assign features for the new nodes in adding new nodes. |
| 338 | |
| 339 | >>> g.add_nodes(1, {'h': torch.ones(1, 1), 'w': torch.ones(1, 1)}) |
| 340 | >>> g.ndata['h'] |