GATConv

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

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

Graph Attention Layer from Graph Attention Network

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

where \(\alpha_{ij}\) is the attention score bewteen node \(i\) and node \(j\):

\[ \begin{align}\begin{aligned}\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{aligned}\end{align} \]
Parameters
  • in_feats (int, or pair of ints) – Input feature size; i.e, the number of dimensions of \(h_i^{(l)}\). ATConv can be applied on homogeneous graph and unidirectional bipartite graph. If the layer is to be applied to a unidirectional bipartite graph, in_feats specifies the input feature size on both the source and destination nodes. If a scalar is given, the source and destination node feature size would take the same value.

  • out_feats (int) – Output feature size; i.e, the number of dimensions of \(h_i^{(l+1)}\).

  • num_heads (int) – Number of heads in Multi-Head Attention.

  • feat_drop (float, optional) – Dropout rate on feature. Defaults: 0.

  • attn_drop (float, optional) – Dropout rate on attention weight. Defaults: 0.

  • negative_slope (float, optional) – LeakyReLU angle of negative slope. Defaults: 0.2.

  • residual (bool, optional) – If True, use residual connection. Defaults: False.

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

Examples

>>> import dgl
>>> import numpy as np
>>> import tensorflow as tf
>>> from dgl.nn import GATConv
>>>
>>> # 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))
>>>     gatconv = GATConv(10, 2, num_heads=3)
>>>     res = gatconv(g, feat)
>>>     res
<tf.Tensor: shape=(6, 3, 2), dtype=float32, numpy=
array([[[ 0.75311995, -1.8093625 ],
        [-0.12128812, -0.78072834],
        [-0.49870574, -0.15074375]],
    [[ 0.75311995, -1.8093625 ],
        [-0.12128812, -0.78072834],
        [-0.49870574, -0.15074375]],
    [[ 0.75311995, -1.8093625 ],
        [-0.12128812, -0.78072834],
        [-0.49870574, -0.15074375]],
    [[ 0.75311995, -1.8093626 ],
        [-0.12128813, -0.78072834],
        [-0.49870574, -0.15074375]],
    [[ 0.75311995, -1.8093625 ],
        [-0.12128812, -0.78072834],
        [-0.49870574, -0.15074375]],
    [[ 0.75311995, -1.8093625 ],
        [-0.12128812, -0.78072834],
        [-0.49870574, -0.15074375]]], dtype=float32)>
>>> # Case 2: Unidirectional bipartite graph
>>> u = [0, 1, 0, 0, 1]
>>> v = [0, 1, 2, 3, 2]
>>> g = dgl.heterograph({('A', 'r', 'B'): (u, v)})
>>> with tf.device("CPU:0"):
>>>     u_feat = tf.convert_to_tensor(np.random.rand(2, 5))
>>>     v_feat = tf.convert_to_tensor(np.random.rand(4, 10))
>>>     gatconv = GATConv((5,10), 2, 3)
>>>     res = gatconv(g, (u_feat, v_feat))
>>>     res
<tf.Tensor: shape=(4, 3, 2), dtype=float32, numpy=
array([[[-0.89649093, -0.74841046],
        [ 0.5088224 ,  0.10908248],
        [ 0.55670375, -0.6811229 ]],
    [[-0.7905004 , -0.1457274 ],
        [ 0.2248168 ,  0.93014705],
        [ 0.12816726, -0.4093595 ]],
    [[-0.85875374, -0.53382933],
        [ 0.36841977,  0.51498866],
        [ 0.31893706, -0.5303393 ]],
    [[-0.89649093, -0.74841046],
        [ 0.5088224 ,  0.10908248],
        [ 0.55670375, -0.6811229 ]]], dtype=float32)>