mindspore_gl.nn
APIs for graph convolutions.
- class mindspore_gl.nn.APPNPConv(k: int, alpha: float, edge_drop=1.0)[source]
Approximate Personalization Propagation in Neural Prediction Layers. From the paper Predict then Propagate: Graph Neural Networks meet Personalized PageRank.
Where
- Parameters
- Inputs:
x (Tensor): The input node features. The shape is
where is the number of nodes, and could be of any shape.in_deg (Tensor): In degree for nodes. In degree for nodes. The shape is
where is the number of nodes.out_deg (Tensor): Out degree for nodes. Out degree for nodes. The shape is
where is the number of nodes.g (Graph): The input graph.
- Outputs:
Tensor, the output feature of shape
where should be the same as input shape.
- Raises
TypeError – If k is not an int.
TypeError – If alpha or edge_drop is not a float.
ValueError – If alpha is not in range [0.0, 1.0]
ValueError – If edge_drop is not in range (0.0, 1.0]
Examples
>>> import mindspore as ms >>> from mindspore_gl.nn.conv import APPNPConv >>> from mindspore_gl import GraphField >>> n_nodes = 4 >>> n_edges = 7 >>> feat_size = 4 >>> src_idx = ms.Tensor([0, 1, 1, 2, 2, 3, 3], ms.int32) >>> dst_idx = ms.Tensor([0, 0, 2, 1, 3, 0, 1], ms.int32) >>> ones = ms.ops.Ones() >>> feat = ones((n_nodes, feat_size), ms.float32) >>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges) >>> in_degree = ms.Tensor([3, 2, 1, 1], ms.int32) >>> out_degree = ms.Tensor([1, 2, 1, 2], ms.int32) >>> appnpconv = APPNPConv(k=3, alpha=0.5, edge_drop=1.0) >>> res = appnpconv(feat, in_degree, out_degree, *graph_field.get_graph()) >>> print(res.shape) (4, 4)
- class mindspore_gl.nn.GATConv(in_feat_size: int, out_size: int, num_attn_head: int, input_drop_out_rate: float = 1.0, attn_drop_out_rate: float = 1.0, leaky_relu_slope: float = 0.2, activation=None, add_norm=False)[source]
Graph Attention Network, from the paper Graph Attention Network.
represents the attention score between node and node .- Parameters
in_feat_size (int) – Input node feature size.
out_size (int) – Output node feature size.
num_attn_head (int) – Number of attention head used in GAT.
input_drop_out_rate (float) – Input drop out rate. Default: 1.0.
attn_drop_out_rate (float) – Attention drop out rate. Default: 1.0.
leaky_relu_slope (float) – Slope for leaky relu. Default: 0.2.
activation (Cell) – Activation function, default is None.
add_norm – Whether the edge information needs normalization or not. Default: False.
- Inputs:
x (Tensor) - The input node features. The shape is
where is the number of nodes and could be of any shape.g (Graph) - The input graph.
- Outputs:
Tensor, the output feature of shape
where should be equal to .
- Raises
TypeError – If in_feat_size, out_size, or num_attn_head is not an int.
TypeError – If input_drop_out_rate, attn_drop_out_rate, or leaky_relu_slope is not a float.
TypeError – If activation is not a Cell.
ValueError – If input_drop_out_rate or attn_drop_out_rate is not in range (0.0, 1.0]
Examples
>>> import mindspore as ms >>> from mindspore_gl.nn.conv import GATConv >>> from mindspore_gl import GraphField >>> n_nodes = 4 >>> n_edges = 7 >>> feat_size = 4 >>> src_idx = ms.Tensor([0, 1, 1, 2, 2, 3, 3], ms.int32) >>> dst_idx = ms.Tensor([0, 0, 2, 1, 3, 0, 1], ms.int32) >>> ones = ms.ops.Ones() >>> feat = ones((n_nodes, feat_size), ms.float32) >>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges) >>> gatconv = GATConv(in_feat_size=4, out_size=2, num_attn_head=3) >>> res = gatconv(feat, *graph_field.get_graph()) >>> print(res.shape) (4, 6)
- class mindspore_gl.nn.GCNConv(in_feat_size: int, out_size: int, activation=None, dropout=0.5)[source]
Graph Convolution Network Layer. from the paper Semi-Supervised Classification with Graph Convolutional Networks.
represents the neighbour node of . .- Parameters
- Inputs:
x (Tensor) - The input node features. The shape is
where is the number of nodes, and should be equal to in_feat_size in Args.in_deg (Tensor) - In degree for nodes. The shape is
where is the number of nodes.out_deg (Tensor) - Out degree for nodes. The shape is
where is the number of nodes.g (Graph) - The input graph.
- Outputs:
Tensor, output node features with shape of
, where should be the same as out_size in Args.
- Raises
TypeError – If in_feat_size or out_size is not an int.
TypeError – If dropout is not a float.
TypeError – If activation is not a Cell.
ValueError – If dropout is not in range (0.0, 1.0]
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> from mindspore_gl.nn.conv import GCNConv >>> from mindspore_gl import GraphField >>> n_nodes = 4 >>> n_edges = 7 >>> feat_size = 4 >>> src_idx = ms.Tensor([0, 1, 1, 2, 2, 3, 3], ms.int32) >>> dst_idx = ms.Tensor([0, 0, 2, 1, 3, 0, 1], ms.int32) >>> ones = ms.ops.Ones() >>> feat = ones((n_nodes, feat_size), ms.float32) >>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges) >>> in_degree = ms.Tensor([3, 2, 1, 1], ms.int32) >>> out_degree = ms.Tensor([1, 2, 1, 2], ms.int32) >>> gcnconv = GCNConv(in_feat_size=4, out_size=2, activation=None, dropout=1.0) >>> res = gcnconv(feat, in_degree, out_degree, *graph_field.get_graph()) >>> print(res.shape) (4, 2)