MCPcopy
hub / github.com/dmlc/dgl / add_nodes

Method add_nodes

python/dgl/heterograph.py:283–433  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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']

Callers 15

add_edgesMethod · 0.95
build_graph_trainFunction · 0.95
__getitem__Method · 0.95
build_graphMethod · 0.95
mergeFunction · 0.45
processMethod · 0.45
processMethod · 0.45
processMethod · 0.45

Calls 10

get_ntype_idMethod · 0.95
num_nodesMethod · 0.95
edgesMethod · 0.95
get_etype_idMethod · 0.95
_reset_cached_infoMethod · 0.95
DGLErrorClass · 0.85
number_of_ntypesMethod · 0.80
appendMethod · 0.80
get_relation_graphMethod · 0.80
add_rowsMethod · 0.80

Tested by 15

test_sequentialFunction · 0.36
test_prop_nodes_topoFunction · 0.36
test_heterograph_mergeFunction · 0.36
test_all_binary_builtinsFunction · 0.36
generate_graphFunction · 0.36
_test_map_to_subgraphFunction · 0.36
generate_graphFunction · 0.36
test_pull_multi_fallbackFunction · 0.36
test_empty_queryFunction · 0.36
test_cloneFunction · 0.36