GCN2Convο
- class dgl.nn.pytorch.conv.GCN2Conv(in_feats, layer, alpha=0.1, lambda_=1, project_initial_features=True, allow_zero_in_degree=False, bias=True, activation=None)[source]ο
Bases:
Module
Graph Convolutional Network via Initial residual and Identity mapping (GCNII) from Simple and Deep Graph Convolutional Networks
It is mathematically is defined as follows:
\[\mathbf{h}^{(l+1)} =\left( (1 - \alpha)(\mathbf{D}^{-1/2} \mathbf{\hat{A}} \mathbf{D}^{-1/2})\mathbf{h}^{(l)} + \alpha {\mathbf{h}^{(0)}} \right) \left( (1 - \beta_l) \mathbf{I} + \beta_l \mathbf{W} \right)\]where \(\mathbf{\hat{A}}\) is the adjacency matrix with self-loops, \(\mathbf{D}_{ii} = \sum_{j=0} \mathbf{A}_{ij}\) is its diagonal degree matrix, \(\mathbf{h}^{(0)}\) is the initial node features, \(\mathbf{h}^{(l)}\) is the feature of layer \(l\), \(\alpha\) is the fraction of initial node features, and \(\beta_l\) is the hyperparameter to tune the strength of identity mapping. It is defined by \(\beta_l = \log(\frac{\lambda}{l}+1)\approx\frac{\lambda}{l}\), where \(\lambda\) is a hyperparameter. \(\beta\) ensures that the decay of the weight matrix adaptively increases as we stack more layers.
- Parameters:
in_feats (int) β Input feature size; i.e, the number of dimensions of \(h_j^{(l)}\).
layer (int) β the index of current layer.
alpha (float) β The fraction of the initial input features. Default:
0.1
lambda (float) β The hyperparameter to ensure the decay of the weight matrix adaptively increases. Default:
1
project_initial_features (bool) β Whether to share a weight matrix between initial features and smoothed features. Default:
True
bias (bool, optional) β If True, adds a learnable bias to the output. Default:
True
.activation (callable activation function/layer or None, optional) β If not None, applies an activation function to the updated node features. Default:
None
.allow_zero_in_degree (bool, optional) β If there are 0-in-degree nodes in the graph, output for those nodes will be invalid since no message will be passed to those nodes. This is harmful for some applications causing silent performance regression. This module will raise a DGLError if it detects 0-in-degree nodes in input graph. By setting
True
, it will suppress the check and let the users handle it by themselves. Default:False
.
Note
Zero in-degree nodes will lead to invalid output value. This is because no message will be passed to those nodes, the aggregation function will be appied on empty input. A common practice to avoid this is to add a self-loop for each node in the graph if it is homogeneous, which can be achieved by:
>>> g = ... # a DGLGraph >>> g = dgl.add_self_loop(g)
Calling
add_self_loop
will not work for some graphs, for example, heterogeneous graph since the edge type can not be decided for self_loop edges. Setallow_zero_in_degree
toTrue
for those cases to unblock the code and handle zero-in-degree nodes manually. A common practise to handle this is to filter out the nodes with zero-in-degree when use after conv.Examples
>>> import dgl >>> import numpy as np >>> import torch as th >>> from dgl.nn import GCN2Conv
>>> # Homogeneous graph >>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3])) >>> feat = th.ones(6, 3) >>> g = dgl.add_self_loop(g) >>> conv1 = GCN2Conv(3, layer=1, alpha=0.5, \ ... project_initial_features=True, allow_zero_in_degree=True) >>> conv2 = GCN2Conv(3, layer=2, alpha=0.5, \ ... project_initial_features=True, allow_zero_in_degree=True) >>> res = feat >>> res = conv1(g, res, feat) >>> res = conv2(g, res, feat) >>> print(res) tensor([[1.3803, 3.3191, 2.9572], [1.3803, 3.3191, 2.9572], [1.3803, 3.3191, 2.9572], [1.4770, 3.8326, 3.2451], [1.3623, 3.2102, 2.8679], [1.3803, 3.3191, 2.9572]], grad_fn=<AddBackward0>)
- forward(graph, feat, feat_0, edge_weight=None)[source]ο
Descriptionο
Compute graph convolution.
- param graph:
The graph.
- type graph:
DGLGraph
- param feat:
The input feature of shape \((N, D_{in})\) where \(D_{in}\) is the size of input feature and \(N\) is the number of nodes.
- type feat:
torch.Tensor
- param feat_0:
The initial feature of shape \((N, D_{in})\)
- type feat_0:
torch.Tensor
- param edge_weight:
edge_weight to use in the message passing process. This is equivalent to using weighted adjacency matrix in the equation above, and \(\tilde{D}^{-1/2}\tilde{A} \tilde{D}^{-1/2}\) is based on
dgl.nn.pytorch.conv.graphconv.EdgeWeightNorm
.- type edge_weight:
torch.Tensor, optional
- returns:
The output feature
- rtype:
torch.Tensor
- raises DGLError:
If there are 0-in-degree nodes in the input graph, it will raise DGLError since no message will be passed to those nodes. This will cause invalid output. The error can be ignored by setting
allow_zero_in_degree
parameter toTrue
.
Note
Input shape: \((N, *, \text{in_feats})\) where * means any number of additional dimensions, \(N\) is the number of nodes.
Output shape: \((N, *, \text{out_feats})\) where all but the last dimension are the same shape as the input.
Weight shape: \((\text{in_feats}, \text{out_feats})\).