GraphConvΒΆ
-
class
dgl.nn.tensorflow.conv.
GraphConv
(*args, **kwargs)[source]ΒΆ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Graph convolution from Semi-Supervised Classification with Graph Convolutional Networks
Mathematically it is defined as follows:
\[h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{1}{c_{ij}}h_j^{(l)}W^{(l)})\]where \(\mathcal{N}(i)\) is the set of neighbors of node \(i\), \(c_{ij}\) is the product of the square root of node degrees (i.e., \(c_{ij} = \sqrt{|\mathcal{N}(i)|}\sqrt{|\mathcal{N}(j)|}\)), and \(\sigma\) is an activation function.
- Parameters
in_feats (int) β Input feature size; i.e, the number of dimensions of \(h_j^{(l)}\).
out_feats (int) β Output feature size; i.e., the number of dimensions of \(h_i^{(l+1)}\).
norm (str, optional) β
How to apply the normalizer. Can be one of the following values:
right
, to divide the aggregated messages by each nodeβs in-degrees, which is equivalent to averaging the received messages.none
, where no normalization is applied.both
(default), where the messages are scaled with \(1/c_{ji}\) above, equivalent to symmetric normalization.left
, to divide the messages sent out from each node by its out-degrees, equivalent to random walk normalization.
weight (bool, optional) β If True, apply a linear layer. Otherwise, aggregating the messages without a weight matrix.
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
.
-
weight
ΒΆ The learnable weight tensor.
- Type
torch.Tensor
-
bias
ΒΆ The learnable bias tensor.
- Type
torch.Tensor
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 tensorflow as tf >>> from dgl.nn import GraphConv
>>> # Case 1: Homogeneous graph >>> with tf.device("CPU:0"): ... g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3])) ... g = dgl.add_self_loop(g) ... feat = tf.ones((6, 10)) ... conv = GraphConv(10, 2, norm='both', weight=True, bias=True) ... res = conv(g, feat) >>> print(res) <tf.Tensor: shape=(6, 2), dtype=float32, numpy= array([[ 0.6208475 , -0.4896223 ], [ 0.68356586, -0.5390842 ], [ 0.6208475 , -0.4896223 ], [ 0.7859846 , -0.61985517], [ 0.8251371 , -0.65073216], [ 0.48335412, -0.38119012]], dtype=float32)> >>> # allow_zero_in_degree example >>> with tf.device("CPU:0"): ... g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3])) ... conv = GraphConv(10, 2, norm='both', weight=True, bias=True, allow_zero_in_degree=True) ... res = conv(g, feat) >>> print(res) <tf.Tensor: shape=(6, 2), dtype=float32, numpy= array([[ 0.6208475 , -0.4896223 ], [ 0.68356586, -0.5390842 ], [ 0.6208475 , -0.4896223 ], [ 0.7859846 , -0.61985517], [ 0.8251371 , -0.65073216], [ 0., 0.]], dtype=float32)>
>>> # Case 2: Unidirectional bipartite graph >>> u = [0, 1, 0, 0, 1] >>> v = [0, 1, 2, 3, 2] >>> with tf.device("CPU:0"): ... g = dgl.heterograph({('_N', '_E', '_N'):(u, v)}) ... u_fea = tf.convert_to_tensor(np.random.rand(2, 5)) ... v_fea = tf.convert_to_tensor(np.random.rand(4, 5)) ... conv = GraphConv(5, 2, norm='both', weight=True, bias=True) ... res = conv(g, (u_fea, v_fea)) >>> res <tf.Tensor: shape=(4, 2), dtype=float32, numpy= array([[ 1.3607183, -0.1636453], [ 1.6665325, -0.2004239], [ 2.1405895, -0.2574358], [ 1.3607183, -0.1636453]], dtype=float32)>