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.

\[\begin{split}H^{0} = X \\ H^{l+1} = (1-\alpha)\left(\tilde{D}^{-1/2} \tilde{A} \tilde{D}^{-1/2} H^{l}\right) + \alpha H^{0}\end{split}\]

Where \(\tilde{A}=A+I\)

Parameters
  • k (int) – Number of iters.

  • alpha (float) – Transmission probability.

  • edge_drop (float) – The drop rate on the edge of messages received by each node. Default: 1.0.

Inputs:
  • x (Tensor): The input node features. The shape is \((N,*)\) where \(N\) 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 \((N, )\) where \(N\) is the number of nodes.

  • out_deg (Tensor): Out degree for nodes. Out degree for nodes. The shape is \((N, )\) where \(N\) is the number of nodes.

  • g (Graph): The input graph.

Outputs:

Tensor, the output feature of shape \((N,*)\) 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.

\[h_i^{(l+1)} = \sum_{j\in \mathcal{N}(i)} \alpha_{i,j} W^{(l)} h_j^{(l)}\]

\(\alpha_{i, j}\) represents the attention score between node \(i\) and node \(j\).

\[\begin{split}\alpha_{ij}^{l} = \mathrm{softmax_i} (e_{ij}^{l}) \\ e_{ij}^{l} = \mathrm{LeakyReLU}\left(\vec{a}^T [W h_{i} \| W h_{j}]\right)\end{split}\]
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 \((N,D_{in})\) where \(N\) is the number of nodes and \(D_{in}\) could be of any shape.

  • g (Graph) - The input graph.

Outputs:

Tensor, the output feature of shape \((N,D_{out})\) where \(D_{out}\) should be equal to \(D_{in} * num\_attn\_head\).

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.

\[h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{1}{c_{ji}}h_j^{(l)}W^{(l)})\]

\(\mathcal{N}(i)\) represents the neighbour node of \(i\). \(c_{ji} = \sqrt{|\mathcal{N}(j)|}\sqrt{|\mathcal{N}(i)|}\).

\[h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{e_{ji}}{c_{ji}}h_j^{(l)}W^{(l)})\]
Parameters
  • in_feat_size (int) – Input node feature size.

  • out_size (int) – Output node feature size.

  • activation (Cell) – Activation function, default is None.

  • dropout (float) – The keep rate, greater than 0 and less equal than 1. E.g. dropout=0.9, dropping out 10% of input units. Default: 0.5.

Inputs:
  • x (Tensor) - The input node features. The shape is \((N, D_{in})\) where \(N\) is the number of nodes, and \(D_{in}\) should be equal to in_feat_size in Args.

  • in_deg (Tensor) - In degree for nodes. The shape is \((N, )\) where \(N\) is the number of nodes.

  • out_deg (Tensor) - Out degree for nodes. The shape is \((N, )\) where \(N\) is the number of nodes.

  • g (Graph) - The input graph.

Outputs:

Tensor, output node features with shape of \((N, D_{out})\), where \((D_{out})\) 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)
class mindspore_gl.nn.GNNCell[source]

GNN Cell class.

Construct function will be translated by default.

static disable_display()[source]

Disable display code comparison.

static enable_display(screen_width=200)[source]

Enable display code comparison.

Parameters

screen_width (int) – Determines the screen width on which the code is displayed.