class dgl.nn.tensorflow.conv.SGConv(*args, **kwargs)[source]

Bases: tensorflow.python.keras.engine.base_layer.Layer

SGC layer from Simplifying Graph Convolutional Networks

\[H^{K} = (\tilde{D}^{-1/2} \tilde{A} \tilde{D}^{-1/2})^K X \Theta\]

where \(\tilde{A}\) is \(A\) + \(I\). Thus the graph input is expected to have self-loop edges added.

  • in_feats (int) – Number of input features; i.e, the number of dimensions of \(X\).

  • out_feats (int) – Number of output features; i.e, the number of dimensions of \(H^{K}\).

  • k (int) – Number of hops \(K\). Defaults:1.

  • cached (bool) –

    If True, the module would cache

    \[(\tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}})^K X\Theta\]

    at the first forward call. This parameter should only be set to True in Transductive Learning setting.

  • bias (bool) – If True, adds a learnable bias to the output. Default: True.

  • norm (callable activation function/layer or None, optional) – If not None, applies normalization to the updated node features. Default: False.

  • 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.


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. Set allow_zero_in_degree to True 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.


>>> import dgl
>>> import numpy as np
>>> import tensorflow as tf
>>> from dgl.nn import SGConv
>>> 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 = SGConv(10, 2, k=2, cached=True)
>>>     res = conv(g, feat)
>>>     res
<tf.Tensor: shape=(6, 2), dtype=float32, numpy=
array([[0.61023676, 0.5246612 ],
    [0.61023676, 0.5246612 ],
    [0.61023676, 0.5246612 ],
    [0.8697353 , 0.7477695 ],
    [0.60570633, 0.520766  ],
    [0.6102368 , 0.52466124]], dtype=float32)>