gammagl.utils.add_self_loops¶
- class add_self_loops(edge_index, edge_attr=None, n_loops=1, fill_value: float | str | None = None, num_nodes: int | None = None)[source]¶
Adds a self-loop \((i,i) \in \mathcal{E}\) to every node \(i \in \mathcal{V}\) in the graph given by
edge_index. In case the graph is weighted or has multi-dimensional edge features (edge_attr != None), edge features of self-loops will be added according tofill_value.>>> from gammagl.data import Graph >>> from gammagl.utils.loop import add_self_loops >>> import numpy >>> edge_index = tlx.constant([[0, 0, 0], [1, 2, 3]]) array([[0, 0, 0], [1, 2, 3]]) >>> edge_index, _ = add_self_loops(edge_index) array([[0, 0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 1, 2, 3]])
- Parameters:
edge_index (tensor) – The edge indices.
n_loops (int) – the number of loops
edge_attr (tensor, optional) – Edge weights or multi-dimensional edge features. (default:
None)fill_value (float, tensor, str, optional) – The way to generate edge features of self-loops (in case
edge_attr != None). If given asfloatortorch.Tensor, edge features of self-loops will be directly given byfill_value. If given asstr, edge features of self-loops are computed by aggregating all features of edges that point to the specific node, according to a reduce operation. ("add","mean","min","max","mul"). (default:1.)num_nodes (int, optional) – The number of nodes, i.e.
max_val + 1ofedge_index. (default:None)
- Return type:
LongTensor,Tensor