mindspore_gl.graph.add_self_loop
- mindspore_gl.graph.add_self_loop(edge_index, edge_weight, node, fill_value, mode='dense')[source]
ADD the self loop from the input coo matrix. you can choose to operate on a dense matrix or a matrix in COO format.
- Parameters
edge_index (Tensor) – Edge index. The shape is \((2, N\_e)\) where \(N\_e\) is the number of edges.
edge_weight (Tensor) – Edge weights. The shape is \((N\_e)\) where \(N\_e\) is the number of edges.
node (int) – Number of nodes.
fill_value (Tensor) – self-loop value.
mode (str, optional) – type of operation matrix. Support type is
'dense'
and'coo'
. Default:'dense'
.
- Returns
if mode is
'dense'
,new_adj (Tensor) - dense matrix.
if mode is
'coo'
,edge_index (Tensor) - new edge_index.
edge_weight (Tensor) - new edge_weight.
- Raises
ValueError – if mode not is
'coo'
or'dense'
.ValueError – if fill_value length not equal to node.
TypeError – If node is not a positive int.
- Supported Platforms:
Ascend
GPU
Examples
>>> from mindspore import Tensor >>> from mindspore_gl.graph import add_self_loop >>> edge_index = [[1, 1, 2, 2], [0, 2, 0, 1]] >>> edge_index = ms.Tensor(edge_index, ms.int32) >>> edge_weight = Tensor([1, 1, 1, 1], ms.float32) >>> node = 3 >>> fill_value = Tensor([2, 2, 2], ms.float32) >>> new_adj = add_self_loop(edge_index, edge_weight, node, fill_value, mode='dense') >>> print(new_adj) [[2. 0. 0.] [1. 2. 1.] [1. 1. 2.]] >>> edge_index, edge_weight = add_self_loop(edge_index, edge_weight, node, fill_value, mode='coo') >>> print(edge_index) [[1 1 2 2 0 1 2] [0 2 0 1 0 1 2]] >>> print(edge_weight) [1. 1. 1. 1. 2. 2. 2.]