dgl.DGLGraph.add_edgesΒΆ
-
DGLGraph.
add_edges
(u, v, data=None, etype=None)[source]ΒΆ Add multiple new edges for the specified edge type
The i-th new edge will be from
u[i]
tov[i]
.- Parameters
u (int, tensor, numpy.ndarray, list) β Source node IDs,
u[i]
gives the source node for the i-th new edge.v (int, tensor, numpy.ndarray, list) β Destination node IDs,
v[i]
gives the destination node for the i-th new edge.data (dict, optional) β Feature data of the added edges. The i-th row of the feature data corresponds to the i-th new edge.
etype (str or tuple of str, optional) β The type of the new edges. Can be omitted if there is only one edge type in the graph.
Notes
Inplace update is applied to the current graph.
If end nodes of adding edges does not exists, add_nodes is invoked to add new nodes. The node features of the new nodes will be created by initializers defined with
set_n_initializer()
(default initializer fills zeros). In certain cases, it is recommanded to add_nodes first and then add_edges.If the key of
data
does not contain some existing feature fields, those features for the new edges will be created by initializers defined withset_n_initializer()
(default initializer fills zeros).If the key of
data
contains new feature fields, those features for the old edges will be created by initializers defined withset_n_initializer()
(default initializer fills zeros).This function discards the batch information. Please use
dgl.DGLGraph.set_batch_num_nodes()
anddgl.DGLGraph.set_batch_num_edges()
on the transformed graph to maintain the information.
Examples
The following example uses PyTorch backend.
>>> import dgl >>> import torch
Homogeneous Graphs or Heterogeneous Graphs with A Single Edge Type
>>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2]))) >>> g.num_edges() 2 >>> g.add_edges(torch.tensor([1, 3]), torch.tensor([0, 1])) >>> g.num_edges() 4
Since
u
orv
contains a non-existing node ID, the nodes are added implicitly. >>> g.num_nodes() 4If the graph has some edge features and new edges are added without features, their features will be created by initializers defined with
set_n_initializer()
.>>> g.edata['h'] = torch.ones(4, 1) >>> g.add_edges(torch.tensor([1]), torch.tensor([1])) >>> g.edata['h'] tensor([[1.], [1.], [1.], [1.], [0.]])
We can also assign features for the new edges in adding new edges.
>>> g.add_edges(torch.tensor([0, 0]), torch.tensor([2, 2]), ... {'h': torch.tensor([[1.], [2.]]), 'w': torch.ones(2, 1)}) >>> g.edata['h'] tensor([[1.], [1.], [1.], [1.], [0.], [1.], [2.]])
Since
data
contains new feature fields, the features for old edges will be created by initializers defined withset_n_initializer()
.>>> g.edata['w'] tensor([[0.], [0.], [0.], [0.], [0.], [1.], [1.]])
Heterogeneous Graphs with Multiple Edge Types
>>> g = dgl.heterograph({ ... ('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]), ... torch.tensor([0, 0, 1, 1])), ... ('developer', 'develops', 'game'): (torch.tensor([0, 1]), ... torch.tensor([0, 1])) ... }) >>> g.add_edges(torch.tensor([3]), torch.tensor([3])) DGLError: Edge type name must be specified if there are more than one edge types. >>> g.num_edges('plays') 4 >>> g.add_edges(torch.tensor([3]), torch.tensor([3]), etype='plays') >>> g.num_edges('plays') 5
See also