H OW A TTENTIVE ARE G RAPH A TTENTION N ETWORKS ?
A BSTRACT
Graph Attention Networks (GATs) are one of the most popular GNN architectures and are considered as the state-of-the-art architecture for representation learning with graphs.
In GAT, every node attends to its neighbors given its own representation as the query.
However, in this paper we show that GAT computes a very limited kind of attention: the ranking of the attention scores is unconditioned on the query node .
We formally deﬁne this restricted kind of attention as static attention and distinguish it from a strictly more expressive dynamic attention.
Because GATs use a static attention mechanism, there are simple graph problems that GAT cannot express: in a controlled problem, we show that static attention hinders GAT from even ﬁtting the training data.
To remove this limitation, we introduce a simple ﬁx by modifying the order of operations and propose GATv2: a dynamic graph attention variant that is strictly more expressive than GAT.
We perform an extensive evaluation and show that GATv2 outperforms GAT across OGB and other benchmarks while we match their parametric costs.
Our code is available at https://github.com/tech-srl/how_attentive_are_ gats .
1 GATv2 is available as part of the PyTorch Geometric library, 2 the Deep
Graph Library, 3 and the TensorFlow GNN library.
I NTRODUCTION
Graph neural networks (GNNs; Gori et al., 2005; Scarselli et al., 2008) have seen increasing popularity over the past few years (Duvenaud et al., 2015; Atwood and Towsley, 2016;
Bronstein et al., 2017;
Monti et al., 2017).
GNNs provide a general and efﬁcient framework to learn from graph-structured data.
Thus, GNNs are easily applicable in domains where the data can be represented as a set of nodes and the prediction depends on the relationships (edges) between the nodes.
Such domains include molecules, social networks, product recommendation, computer programs and more.
In a GNN, each node iteratively updates its state by interacting with its neighbors.
GNN variants (Wu et al., 2019; Xu et al., 2019; Li et al., 2016) mostly differ in how each node aggregates and combines the representations of its neighbors with its own.
Veliˇckovi´c et al.
(2018) pioneered the use of attention-based neighborhood aggregation, in one of the most common GNN variants – Graph
Attention Network (GAT).
In GAT, every node updates its representation by attending to its neighbors using its own representation as the query.
This generalizes the standard averaging or max-pooling of neighbors (Kipf and Welling, 2017; Hamilton et al., 2017), by allowing every node to compute a weighted average of its neighbors, and (softly) select its most relevant neighbors.
The work of
[Table]
[Table]
[Table]
[Figure]
) can actually compute dynamic attention, where every query has a different ranking of attention coefﬁcients of the keys.
Veli ˇ ckovi ´ c et al.
also generalizes the Transformer’s (Vaswani et al., 2017) self-attention mechanism, from sequences to graphs (Joshi, 2020).
Nowadays, GAT is one of the most popular GNN architectures (Bronstein et al., 2021) and is considered as the state-of-the-art neural architecture for learning with graphs (Wang et al., 2019a).
Nevertheless, in this paper we show that GAT does not actually compute the expressive, well known, type of attention (Bahdanau et al., 2014), which we call dynamic attention.
Instead, we show that
GAT computes only a restricted “static” form of attention: for any query node, the attention function is monotonic with respect to the neighbor (key) scores.
That is, the ranking (the argsort ) of attention coefﬁcients is identical for all nodes in the graph, and is unconditioned on the query node.
This fact severely hurts the expressiveness of GAT, and is demonstrated in Figure 1a.
Supposedly, the conceptual idea of attention as the form of interaction between GNN nodes is orthogonal to the speciﬁc choice of attention function.
However, Veliˇckovi´c et al.
’s original design of
GAT has spread to a variety of domains (Yang et al., 2020; Wang et al., 2019c; Huang and Carley, 2019; Ma et al., 2020;
Kosaraju et al., 2019; Nathani et al., 2019; Wu et al., 2020; Zhang et al., 2020) and has become the default implementation of “graph attention network” in all popular GNN libraries such as PyTorch Geometric (Fey and Lenssen, 2019), DGL (Wang et al., 2019b), and others (Dwivedi et al., 2020; Gordi´c, 2020; Brockschmidt, 2020).
Many other works employed GNNs with attention mechanisms other than the standard GAT’s
(Yun et al.
(2019), see also Appendix A).
However, none of these works identiﬁed the monotonicity of GAT’s attention mechanism, the theoretical differences between attention types, nor empirically compared their performance.
To overcome the limitation we identiﬁed in GAT, we introduce a simple ﬁx to its attention function by only modifying the order of internal operations.
The result is GATv2 – a graph attention variant that has a universal approximator attention function, and is thus strictly more expressive than GAT .
The effect of ﬁxing the attention function in GATv2 is demonstrated in Figure 1b.
In summary, our main contribution is identifying that one of the most popular GNN types, the graph attention network, does not compute dynamic attention, the kind of attention that it seems to compute.
We introduce formal deﬁnitions for analyzing the expressive power of graph attention mechanisms (Deﬁnitions 3.1 and 3.2), and derive our claims theoretically (Theorem 1) from the equations of
Veliˇckovi´c et al.
Empirically, we use a synthetic problem to show that standard GAT cannot express problems that require dynamic attention (Section 4.1).
We introduce a simple ﬁx by switching the order of internal operations in GAT, and propose GATv2, which does compute dynamic attention (Theorem 2).
We further conduct a thorough empirical comparison of GAT and GATv2 and ﬁnd that GATv2 outperforms GAT across 12 benchmarks of node-, link-, and graph-prediction.
For example, GATv2 outperforms extensively tuned GNNs by over 1.4% in the difﬁcult “UnseenProj
Test” set of the VarMisuse task (Allamanis et al., 2018), without any hyperparameter tuning; and
GATv2 improves over an extensively-tuned GAT by 11.5% in 13 prediction objectives in QM9.
In node-prediction benchmarks from OGB (Hu et al., 2020), not only that GATv2 outperforms GAT with respect to accuracy – we ﬁnd that dynamic attention provided a much better robustness to noise.
P RELIMINARIES
A directed graph G =
( V , E ) contains nodes V = { 1 , ..., n } and edges E ⊆ V × V , where ( j, i
) ∈
E denotes an edge from a node j to a node i .
We assume that every node i ∈ V has an initial representation h (0) i ∈ R d 0 .
An undirected graph can be represented with bidirectional edges.
2. G RAPH N EURAL N ETWORKS
A graph neural network (GNN) layer updates every node representation by aggregating its neighbors’ representations.
A layer’s input is a set of node representations { h
i ∈ R
d | i ∈ V} and the set of edges E .
A layer outputs a new set of node representations { h (cid:48)
i ∈ R d (cid:48)
| i ∈ V} , where the same parametric function is applied to every node given its neighbors N i = { j ∈ V | ( j, i ) ∈ E} :
[Equation]
The design of f and AGGREGATE is what mostly distinguishes one type of GNN from the other.
For example, a common variant of GraphSAGE (Hamilton et al., 2017) performs an element-wise mean as AGGREGATE , followed by concatenation with h i , a linear layer and a ReLU as f .
2. G RAPH A TTENTION N ETWORKS
GraphSAGE and many other popular GNN architectures (Xu et al., 2019; Duvenaud et al., 2015) weigh all neighbors j ∈ N i with equal importance (e.g., mean or max-pooling as AGGREGATE ).
To address this limitation, GAT (Veliˇckovi´c et al., 2018) instantiates Equation (1) by computing a learned weighted average of the representations of N i .
A scoring function e : R d × R d
→ R computes a score for every edge ( j, i ) , which indicates the importance of the features of the neighbor j to the node i :
[Equation]
where a ∈ R 2 d (cid:48) , W ∈ R d (cid:48)
× d are learned, and (cid:107) denotes vector concatenation.
These attention scores are normalized across all neighbors j ∈ N i using softmax, and the attention function is deﬁned as:
[Equation]
Then, GAT computes a weighted average of the transformed features of the neighbor nodes (followed by a nonlinearity σ ) as the new representation of i , using the normalized attention coefﬁcients:
[Equation]
From now on, we will refer to Equations (2) to (4) as the deﬁnition of GAT.
T HE E XPRESSIVE P OWER OF G RAPH A TTENTION M ECHANISMS
In this section, we explain why attention is limited when it is not dynamic (Section 3.1).
We then show that GAT is severely constrained, because it can only compute static attention (Section 3.2).
Next, we show how GAT can be ﬁxed (Section 3.3), by simply modifying the order of operations.
We refer to a neural architecture (e.g., the scoring or the attention function of GAT) as a family of functions , parameterized by the learned parameters.
An element in the family is a concrete function with speciﬁc trained weights.
In the following, we use [ n ] to denote the set [ n ] = { 1 , 2 , ..., n } ⊂ N .
3. T HE I MPORTANCE OF D YNAMIC W EIGHTING
Attention is a mechanism for computing a distribution over a set of input key vectors, given an additional query vector.
If the attention function always weighs one key at least as much as any other key, unconditioned on the query , we say that this attention function is static :
Deﬁnition 3.1 (Static attention) .
A (possibly inﬁnite) family of scoring functions F ⊆ (cid:0)
R
d
× R d → R (cid:1) computes static scoring for a given set of key vectors
K = { k 1 , ..., k n } ⊂ R d and query vectors Q = { q 1 , ..., q m } ⊂ R d , if for every f ∈ F there exists a “highest scoring” key j f ∈ [ n ] such that for every query i ∈ [ m ] and key j ∈ [ n ] it holds that f (cid:0) q i , k j f (cid:1) ≥ f ( q i , k j ) .
We say that a family of attention functions computes static attention given K and Q , if its scoring function computes static scoring, possibly followed by monotonic normalization such as softmax.
Static attention is very limited because every function f ∈ F has a key that is always selected , regardless of the query.
Such functions cannot model situations where different keys have different relevance to different queries.
Static attention is demonstrated in Figure 1a.
The general and powerful form of attention is dynamic attention :
Deﬁnition 3.2 (Dynamic attention) .
A (possibly inﬁnite) family of scoring functions F ⊆ (cid:0)
R
d
× R d → R (cid:1) computes dynamic scoring for a given set of key vectors K
= { k 1 , ..., k n } ⊂ R d and query vectors Q = { q 1 , ..., q m } ⊂ R d , if for any mapping ϕ :
[ m ] → [ n ] there exists f ∈ F such that for any query i ∈ [ m ] and any key j (cid:54) =
ϕ ( i ) ∈
[ n ] : f (cid:0) q i , k ϕ ( i ) (cid:1) >
f ( q i , k j ) .
We say that a family of attention functions computes dynamic attention for K and Q , if its scoring function computes dynamic scoring, possibly followed by monotonic normalization such as softmax.
That is, dynamic attention can select every key ϕ ( i ) using the query i , by making f (cid:0) q i , k ϕ ( i ) (cid:1) the maximal in { f ( q i , k j ) | j ∈ [ n ] } .
Note that dynamic and static attention are exclusive properties, but they are not complementary.
Further, every dynamic attention family has strict subsets of static attention families with respect to the same K and Q .
Dynamic attention is demonstrated in Figure 1b.
Attending by decaying Another way to think about attention is the ability to “focus” on the most relevant inputs, given a query.
Focusing is only possible by decaying other inputs, i.e., giving these decayed inputs lower scores than others.
If one key is always given an equal or greater attention score than other keys (as in static attention), no query can ignore this key or decay this key’s score.
3. T HE L IMITED E XPRESSIVITY OF GAT
Although the scoring function e can be deﬁned in various ways, the original deﬁnition of Veliˇckovi´c et al.
(Equation (2)) has become the de facto practice: it has spread to a variety of domains and is now the standard implementation of “graph attention network” in all popular GNN libraries (Fey and Lenssen, 2019; Wang et al., 2019b; Dwivedi et al., 2020; Gordi´c, 2020; Brockschmidt, 2020).
The motivation of GAT is to compute a representation for every node as a weighted average of its neighbors.
Statedly, GAT is inspired by the attention mechanism of Bahdanau et al.
(2014) and the self-attention mechanism of the Transformer (Vaswani et al., 2017).
Nonetheless:
Theorem 1.
A GAT layer computes only static attention, for any set of node representations K = Q
= { h 1 , ..., h n } .
In particular, for n > 1 , a GAT layer does not compute dynamic attention.
Proof.
Let G =
( V , E ) be a graph modeled by a GAT layer with some a and W values (Equations (2) and (3)), and having node representations { h 1 , ..., h n } .
The learned parameter a can be written as a concatenation a =
[ a 1 (cid:107) a 2 ] ∈ R 2 d (cid:48)
such that a 1 , a 2 ∈ R d (cid:48) , and Equation (2) can be re-written as:
[Equation]
Since V is ﬁnite, there exists a node j max ∈ V such that a (cid:62) 2 W h
j max is maximal among all nodes j ∈ V
( j max is the j f required by Deﬁnition 3.1).
Due to the monotonicity of LeakyReLU and softmax , for every query node i ∈ V , the node j max also leads to the maximal value of its attention distribution { α ij | j ∈ V} .
Thus, from Deﬁnition 3.1 directly, α computes only static attention .
This also implies that α does not compute dynamic attention, because in GAT, Deﬁnition 3.2 holds only for constant mappings ϕ that map all inputs to the same output.
The consequence of Theorem 1 is that for any set of nodes V and a trained GAT layer, the attention function α deﬁnes a constant ranking ( argsort ) of the nodes, unconditioned on the query nodes i .
That is, we can denote s j = a (cid:62) 2 W h j and get that for any choice of h i , α is monotonic with respect to the per-node scores { s j | j ∈ V} .
This global ranking induces the local ranking of every neighborhood N i .
The only effect of h i is in the “sharpness” of the produced attention distribution.
This is demonstrated in Figure 1a (bottom), where different curves denote different queries ( h i ).
A discussion regarding the generalization to multi-head attention can be found in Appendix C.
3. B UILDING D YNAMIC G RAPH A TTENTION N ETWORKS
To create a dynamic graph attention network, we modify the order of internal operations in GAT and introduce GATv2 – a simple ﬁx of GAT that has a strictly more expressive attention mechanism.
GATv2 The main problem in the standard GAT scoring function (Equation (2)) is that the learned layers W and a are applied consecutively, and thus can be collapsed into a single linear layer.
To ﬁx this limitation, we simply apply the a layer after the nonlinearity ( LeakyReLU ), and the W layer after the concatenation, 5 effectively applying an MLP to compute the score for each query-key pair:
[Equation]
Veliˇckovi´c et al., 2018):
[Equation]
(our ﬁxed version)
[Equation]
The simple modiﬁcation makes a signiﬁcant difference in the expressiveness of the attention function:
Theorem 2.
A GATv2 layer computes dynamic attention for any set of node representations K = Q
= { h 1 , ..., h n } .
We prove Theorem 2 in Appendix B.
The main idea is that we can deﬁne an appropriate function that GATv2 will be a universal approximator (Cybenko, 1989; Hornik, 1991) of.
In contrast, GAT (Equation (52)) cannot approximate any such desired function (Theorem 1).
Complexity GATv2 has the same time-complexity as GAT’s declared complexity: O ( |V| dd (cid:48)
+ |E| d (cid:48) ) .
However, by merging its linear layers, GAT can be computed faster than stated by Veli ˇ ckovi ´ c et al.
For a detailed time- and parametric-complexity analysis, see Appendix I.
E VALUATION
First, we demonstrate the weakness of GAT using a simple synthetic problem that GAT cannot even ﬁt (cannot even achieve high training accuracy), but is easily solvable by GATv2 (Section 4.1).
Second, we show that GATv2 is much more robust to edge noise , because its dynamic attention mechanisms allow it to decay noisy (false) edges, while GAT’s performance severely decreases as noise increases (Section 4.2).
Finally, we compare GAT and GATv2 across 12 benchmarks overall.
(Sections 4.
to 4.6 and appendix F.4).
We ﬁnd that GAT is inferior to GATv2 across all examined benchmarks.
Setup When previous results exist, we take hyperparameters that were tuned for GAT and use them in GATv2, without any additional tuning.
Self-supervision (Kim and Oh, 2021;
Rong et al., 2020a), graph regularization (Zhao and Akoglu, 2020;
Rong et al., 2020b), and other tricks (Wang, 2021;
Huang et al., 2021) are orthogonal to the contribution of the GNN layer itself, and may further improve [Figure] A, ? B, ?
C, ?
[Figure]
[Figure] [Figure] all GNNs. In all experiments of GATv2, we constrain the learned matrix by setting W =
[ W (cid:48)
(cid:107) W (cid:48) ] , to rule out the increased number of parameters over GAT as the source of empirical difference (see
Appendix I.2).
Training details, statistics, and code are provided in Appendix D.
Our main goal is to compare dynamic and static graph attention mechanisms.
However, for reference, we also include non-attentive baselines such as GCN (Kipf and Welling, 2017),
GIN (Xu et al., 2019) and GraphSAGE (Hamilton et al., 2017).
These non-attentive GNNs can be thought of as a special case of attention, where every node gives all its neighbors the same attention score.
Additional comparison to a Transformer-style scaled dot-product attention (“DPGAT”), which is strictly weaker than our proposed GATv2 (see a proof in Appendix G.1), is shown in Appendix G.
4. S YNTHETIC B ENCHMARK : D ICTIONARY L OOKUP
The D ICTIONARY L OOKUP problem is a contrived problem that we designed to test the ability of a GNN architecture to perform dynamic attention.
Here, we demonstrate that GAT cannot learn this simple problem.
Figure 2 shows a complete bipartite graph of 2 k nodes.
Each “key node” in the bottom row has an attribute ( { A , B , C , ... } ) and a value ( { 1 , 2 , 3 , ... } ).
Each “query node” in the upper row has only an attribute ( { A , B , C , ... } ).
The goal is to predict the value of every query node (upper row), according to its attribute.
Each graph in the dataset has a different mapping from attributes to values.
We created a separate dataset for each k
= { 1 , 2 , 3 , ... } , for which we trained a different model, and measured per-node accuracy.
Although this is a contrived problem, it is relevant to any subgraph with keys that share more than one query, and each query needs to attend to the keys differently.
Such subgraphs are very common in a variety of real-world domains.
This problem tests the layer itself because it can be solved using a single GNN layer, without suffering from multi-layer side-effects such as over-smoothing (Li et al., 2018), over-squashing (Alon and Yahav, 2021), or vanishing gradients (Li et al., 2019).
Our code will be made publicly available, to serve as a testbed for future graph attention mechanisms.
Results Figure 3 shows the following surprising results: GAT with a single head (GAT 1 h ) failed to ﬁt the training set for any value of k , no matter for how many iterations it was trained, and after trying various training methods.
Thus, it expectedly fails to generalize (resulting in low test accuracy).
Using 8 heads, GAT 8 h successfully ﬁts the training set, but generalizes poorly to the test set.
In contrast, GATv2 easily achieves 100% training and 100% test accuracies for any value of k , and even for k =100 (not shown) and using a single head , thanks to its ability to perform dynamic attention.
These results clearly show the limitations of GAT, which are easily solved by GATv2.
An additional comparison to GIN, which could not ﬁt this dataset, is provided in Figure 5 in Appendix F.1.
Visualization Figure 1a (top) shows a heatmap of GAT’s attention scores in this D ICTIONARYL OOKUP problem.
As shown, all query nodes q 0 to q 9 attend mostly to the eighth key ( k 8 ), and have the same ranking of attention coefﬁcients (Figure 1a (bottom)).
In contrast, Figure 1b shows how
GATv2 can select a different key node for every query node, because it computes dynamic attention.
The role of multi-head attention Veliˇckovi´c et al.
(2018) found the role of multi-head attention to be stabilizing the learning process.
Nevertheless, Figure 3 shows that increasing the number of heads strictly increases training accuracy, and thus, the expressivity.
Thus, GAT depends on having multiple attention heads.
In contrast, even a single GATv2 head generalizes better than a multi-head GAT.
4. R OBUSTNESS TO N OISE
We examine the robustness of dynamic and static attention to noise.
In particular, we focus on structural noise: given an input graph G =
( V , E ) and a noise ratio 0 ≤ p ≤ 1 , we randomly sample |E|× p non-existing edges E (cid:48) from V×V\E .
We then train the GNN on the noisy graph G (cid:48)
=( V , E ∪E (cid:48) ) .
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure] p – noise ratio [Figure] [Figure]
work
[Figure]
Results Figure 8 shows the accuracy on two node-prediction datasets from the Open Graph Benchmark (OGB; Hu et al., 2020) as a function of the noise ratio p .
As p increases, all models show a natural decline in test accuracy in both datasets.
Yet, thanks to their ability to compute dynamic attention, GATv2 shows a milder degradation in accuracy compared to GAT, which shows a steeper descent.
We hypothesize that the ability to perform dynamic attention helps the models distinguishing between given data edges ( E ) and noise edges ( E (cid:48) ); in contrast, GAT cannot distinguish between edges, because it scores the source and target nodes separately.
These results clearly demonstrate the robustness of dynamic attention over static attention in noisy settings, which are common in reality.
4. P ROGRAMS : V AR M ISUSE
Setup V AR M ISUSE (Allamanis et al., 2018) is an inductive node-pointing problem that depends on 11 types of syntactic and semantic interactions between elements in computer programs.
[Table]
We used the framework of Brockschmidt (2020), who performed an extensive hyperparameter tuning by searching over 30 conﬁgurations for every GNN type.
We took their best GAT hyperparameters and used them to train GATv2, without further tuning.
Results As shown in Table 1, GATv2 is more accurate than GAT and other GNNs in the SeenProj test sets.
Furthermore, GATv2 achieves an even higher improvement in the Unseen Proj test set.
Overall, these results demonstrate the power of GATv2 in modeling complex relational problems, especially since it outperforms extensively tuned models, without any further tuning by us. [Table]
N ODE -P REDICTION
We further compare GATv2, GAT, and other GNNs on four node-prediction datasets from OGB.
[Table]
Results Results are shown in Table 2.
In all settings and all datasets, GATv2 is more accurate than
GAT and the non-attentive GNNs.
Interestingly, in the datasets of Table 2a, even a single head of
GATv2 outperforms GAT with 8 heads .
In Table 2b ( ogbn-proteins ), increasing the number of heads results in a major improvement for GAT (from 70.77 to 78.63), while GATv2 already gets most of the beneﬁt using a single attention head.
These results demonstrate the superiority of GATv2 over
GAT in node prediction (and even with a single head), thanks to GATv2’s dynamic attention.
4. G RAPH -P REDICTION : QM
Setup In the QM9 dataset (Ramakrishnan et al., 2014; Gilmer et al., 2017), each graph is a molecule and the goal is to regress each graph to 13 real-valued quantum chemical properties.
We used the implementation of Brockschmidt (2020) who performed an extensive hyperparameter search over 500 conﬁgurations; we took their best-found conﬁguration of GAT to implement GATv2.
[Table]
Results Table 3 shows the main results: GATv2 achieves a lower (better) average error than GAT, by 11.5% relatively.
GAT achieves the overall highest average error.
In some properties, the non-attentive
GNNs, GCN and GIN, perform best.
We hypothesize that attention is not needed in modeling these properties.
Generally, GATv2 achieves the lowest overall average relative error (rightmost column).
4. L INK -P REDICTION
We compare GATv2, GAT, and other GNNs in link-prediction datasets from OGB.
Results Table 8 shows that in all datasets, GATv2 achieves a higher MRR than GAT, which achieves the lowest MRR.
However, the non-attentive GraphSAGE performs better than all attentive GNNs.
We hypothesize that attention might not be needed in these datasets.
Another possibility is that dynamic attention is especially useful in graphs that have high node degrees : in ogbn-products and ogbn-proteins (Table 2) the average node degrees are 50.5 and 597, respectively (see Table 5 in
Appendix E).
ogbl-collab and ogbl-citation2 (Table 8), however, have much lower average node degrees – of 8.2 and 20.7.
We hypothesize that a dynamic attention mechanism is especially useful to select the most relevant neighbors when the total number of neighbors is high.
We leave the study of the effect of the datasets’s average node degrees on the optimal GNN architecture for future work.
4. D ISCUSSION
In all examined benchmarks, we found that GATv2 is more accurate than GAT .
Further, we found that GATv2 is signiﬁcantly more robust to noise than GAT.
In the synthetic D ICTIONARY L OOKUP benchmark (Section 4.1), GAT fails to express the data, and thus achieves even poor training accuracy.
In few of the benchmarks (Table 8 and some of the properties in Table 3) – a non-attentive model such as GCN or GIN achieved a higher accuracy than all GNNs that do use attention.
We hypothesize that attention is not needed in these datasets.
Which graph attention mechanism should I use?
It is usually impossible to determine in advance which architecture would perform best.
A theoretically weaker model may perform better in practice, because a stronger model might overﬁt the training data if the task is “too simple” and does not require such expressiveness.
Intuitively, we believe that the more complex the interactions between nodes are – the more beneﬁt a GNN can take from theoretically stronger graph attention mechanisms such as GATv2.
The main question is whether the problem has a global ranking of “inﬂuential” nodes (GAT is sufﬁcient), or do different nodes have different rankings of neighbors (use GATv2).
Veliˇckovi´c, the author of GAT, has conﬁrmed on Twitter 6 that GAT was designed to work in the “easy-to-overﬁt” datasets of the time (2017), such as Cora, Citeseer and Pubmed (Sen et al., 2008), where the data might had an underlying static ranking of “globally important” nodes.
Veli ˇ ckovi ´ c agreed that newer and more challenging benchmarks may demand stronger attention mechanisms such as GATv2.
that depends on the representation of the query node as well.
In this paper, we revisit the traditional assumptions and show that many modern graph benchmarks and datasets contain more complex interactions, and thus require dynamic attention .
C ONCLUSION
In this paper, we identify that the popular and widespread Graph Attention Network does not compute dynamic attention.
Instead, the attention mechanism in the standard deﬁnition and implementations of
GAT is only static : for any query, its neighbor-scoring is monotonic with respect to per-node scores. Further, the ranking (the argsort ) of attention coefﬁcients is identical for all nodes in the graph, and is unconditioned on the query node. As a result, GAT cannot even express simple alignment problems. To address this limitation, we introduce a simple ﬁx and propose GATv2: by modifying the order of operations in GAT, GATv2 achieves a universal approximator attention function and is thus strictly more powerful than GAT.
We demonstrate the empirical advantage of GATv2 over GAT in a synthetic problem that requires dynamic selection of nodes, and in 12 benchmarks from OGB and other public datasets.
Our experiments show that GATv2 outperforms GAT in all benchmarks while having the same parametric cost.
We encourage the community to use GATv2 instead or in addition to GAT whenever comparing new
GNN architectures to the common strong baselines.
In complex tasks and domains and in challenging datasets, a model that uses GAT as an internal component can replace it with GATv2 to beneﬁt from a strictly more powerful model.
{+To this end, we make our code publicly available at https: //github.com/tech-srl/how_attentive_are_gats , 7 and GATv2 is available as part of the PyTorch Geometric library, 8 the Deep Graph Library, 9 and the TensorFlow GNN library.6 https://twitter.com/PetarV_93/status/
+}
An annotated implementation of GATv2 is available at https://nn.labml.ai/graphs
A CKNOWLEDGMENTS
We thank Gail Weiss for the helpful discussions, thorough feedback, and inspirational paper (Weiss et al., 2018).
We also thank Petar Veliˇckovi´c for the useful initial discussion about the complexity and implementation of GAT, Lucio Dery, and the Graph Representation Learning Reading Group at
Mila 11 for their useful comments and questions.
A R ELATED W ORK
Attention in GNNs Modeling pairwise interactions between elements in graph-structured data goes back to interaction networks (Battaglia et al., 2016; Hoshen, 2017) and relational networks (Santoro et al., 2017).
The GAT formulation of Veliˇckovi´c et al.
(2018) rose as the most popular framework for attentional GNNs, thanks to its simplicity, generality, and applicability beyond reinforcement learning (Denil et al., 2017; Duan et al., 2017).
Nevertheless, in this work, we show that the popular and widespread deﬁnition of GAT is severely constrained to static attention only.
Other graph attention mechanisms Many works employed GNNs with attention mechanisms other than the standard GAT’s
(Zhang et al., 2018;
Thekumparampil et al., 2018; Gao and Ji, 2019;
Lukovnikov and Fischer, 2021; Shi et al., 2020; Dwivedi and Bresson, 2020; Busbridge et al., 2019;
Rong et al., 2020a; Veliˇckovi´c et al., 2020), and Lee et al.
(2018) conducted an extensive survey of attention types in GNNs.
However, none of these works identiﬁed the monotonicity of GAT’s attention mechanism, the theoretical differences between attention types, nor empirically compared their performance.
Kim and Oh (2021) compared two graph attention mechanisms empirically, but in a speciﬁc self-supervised scenario, without observing the theoretical difference in their expressiveness.
The static attention of GAT Qiu et al.
(2018) recognized the order-preserving property of GAT, but did not identify the severe theoretical constraint that this property implies: the inability to perform dynamic attention (Theorem 1).
Furthermore, they presented GAT’s monotonicity as a desired trait (!)
To the best of our knowledge, our work is the ﬁrst work to recognize the inability of GAT to perform dynamic attention and its practical harmful consequences.
B P ROOF FOR T HEOREM
For brevity, we repeat our deﬁnition of dynamic attention (Deﬁnition 3.2):
Deﬁnition 3.2 (Dynamic attention).
A (possibly inﬁnite) family of scoring functions F ⊆ (cid:0)
R
d
× R d → R (cid:1) computes dynamic scoring for a given set of key vectors K
= { k 1 , ..., k n } ⊂ R d and query vectors Q = { q 1 , ..., q m } ⊂ R d , if for any mapping ϕ :
[ m ] → [ n ] there exists f ∈ F such that for any query i ∈ [ m ] and any key j (cid:54) =
ϕ ( i ) ∈
[ n ] : f (cid:0) q i , k ϕ ( i ) (cid:1) >
f ( q i , k j ) .
We say that a family of attention functions computes dynamic attention for K and Q , if its scoring function computes dynamic scoring, possibly followed by monotonic normalization such as softmax.
Theorem 2.
A GATv2 layer computes dynamic attention for any set of node representations K = Q
= { h 1 , ..., h n } .
Proof.
Let G =
( V , E ) be a graph modeled by a GATv2 layer, having node representations { h 1 , ..., h n } , and let ϕ :
[ n ] → [ n ] be any node mapping [ n ] → [ n ] .
We deﬁne g : R 2 d → R as follows:
[Equation]
Next, we deﬁne a continues function (cid:101) g : R 2 d → R that equals to g in only speciﬁc n 2 inputs:
[Equation]
For all other inputs x ∈ R 2 d , (cid:101) g ( x ) realizes to any values that maintain the continuity of (cid:101)
g (this is possible because we ﬁxed the values of (cid:101) g for only a ﬁnite set of points).
Thus, for every node i ∈ V and j (cid:54) = ϕ ( i ) ∈ V :
[Equation]
If we concatenate the two input vectors, and deﬁne the scoring function e of GATv2 (Equation (7)) as a function of the concatenated vector [ h i (cid:107) h j ] , from the universal approximation theorem (Hornik et al., 1989; Cybenko, 1989; Funahashi, 1989; Hornik, 1991), e can approximate (cid:101) g for any compact subset of R 2 d .
Thus, for any sufﬁciently small (cid:15) (any 0 < (cid:15) < 1 / 2 ) there exist parameters W and a such that for every node i ∈ V and every j (cid:54) =
ϕ ( i ) :
[Equation]
, a (cid:0)
[Equation]
[Equation]
and due to the increasing monotonicity of softmax :
[Equation]
[Equation] The choice of nonlinearity In general, these results hold if GATv2 had used any common nonpolynomial activation function (such as ReLU, sigmoid, or the hyperbolic tangent function).
The
LeakyReLU activation function of GATv2 does not change its universal approximation ability (Leshno et al., 1993; Pinkus, 1999; Park et al., 2021), and it was chosen only for consistency with the original deﬁnition of GAT.
Veliˇckovi´c et al.
(2018) found it beneﬁcial to employ H separate attention heads and concatenate their outputs, similarly to Transformers. In this case, Theorem 1 holds for each head separately: every head h ∈
[ H ] has a (possibly different) node that maximizes { s ( h ) j | j ∈ V} , and the output is the concatenation of H static attention heads.
D T RAINING DETAILS
In this section we elaborate on the training details of all of our experiments.
All models use residual connections as in Veliˇckovi´c et al.
All used code and data are publicly available under the
MIT license.
D. N ODE - AND L INK -P REDICTION
We used the provided splits of OGB (Hu et al., 2020) and the Adam optimizer.
We tuned the following hyperparameters: number of layers ∈ { 2 , 3 , 6 } , hidden size ∈ { 64 , 128 , 256 } , learning rate ∈ { 0 . 0005 , 0 .
and sampling method – full batch, GraphSAINT (Zeng et al., 2019) and NeighborSampling (Hamilton et al., 2017).
We tuned hyperparameters according to validation score and early stopping.
The ﬁnal hyperparameters are detailed in Table 4.
For the rest of the input space, we don’t have any requirement on the value of (cid:101) g , except for maintaining the continuity of (cid:101) g .
There exist inﬁnitely many such possible (cid:101) g for every given set of keys, queries and a mapping ϕ , but the concrete functions are not needed for the proof.
[Table]
D. R OBUSTNESS TO N OISE
In these experiments, we used the same best-found hyperparameters in node-prediction, withattention heads in ogbn-arxiv and 1 head in ogbn-mag .
Each point is an average of 10 runs.
D. S YNTHETIC B ENCHMARK : D ICTIONARY L OOKUP
In all experiments, we used a learning rate decay of 0 . 5 , a hidden size of d = 128 , a batch size of 1024 , and the Adam optimizer.
We created a separate dataset for every graph size ( k ), and we split each such dataset to train and test with a ratio of 80:20.
Since this is a contrived problem, we did not use a validation set, and the reported test results can be thought of as validation results.
Every model was trained on a ﬁxed value of k .
Every key node (bottom row in Figure 2) was encoded as a sum of learned attribute embedding and a value embedding, followed by ReLU.
We experimented with layer normalization, batch normalization, dropout, various activation functions and various learning rates.
None of these changed the general trend, so the experiments in Figurewere conducted without any normalization, without dropout and a learning rate of 0 .
. D. P ROGRAMS : V AR M ISUSE
We used the code, splits, and the same best-found conﬁgurations as Brockschmidt (2020), who performed an extensive hyperparameter tuning by searching over 30 conﬁgurations for each GNN type.
We trained each model ﬁve times.
We took the best-found hyperparameters of Brockschmidt (2020) for GAT and used them to train
GATv2, without any further tuning.
D. G RAPH -P REDICTION : QM
We used the code and splits of Brockschmidt (2020) who performed an extensive hyperparameter search over 500 conﬁgurations.
We took the best-found hyperparameters of Brockschmidt (2020) for GAT and used them to train GATv2.
The only minor change from GAT is placing a residual connection after every layer, rather than after every other layer, which is within the experimented hyperparameter search that was reported by Brockschmidt (2020).
D. C OMPUTE AND R ESOURCES
Our experiments consumed approximately 100 days of GPU in total.
We used cloud GPUs of type
V100, and we used RTX 3080 and 3090 in local GPU machines.
E D ATA S TATISTICS E. N ODE - AND L INK -P REDICTION D ATASETS
Statistics of the OGB datasets we used for node- and link-prediction are shown in Table 5.
[Table]
E. QM
Statistics of the QM9 dataset, as used in Brockschmidt (2020) are shown in Table 6.
[Table]
E. V AR M ISUSE
Statistics of the V AR M ISUSE dataset, as used in Allamanis et al.
(2018) and Brockschmidt (2020),
are shown in Table 7.
[Table]
F A DDITIONAL R ESULTS F. D ICTIONARY L OOKUP
Figure 5 shows additional comparison between GATv2 and GIN (Xu et al., 2019) in the D ICTIONARY L OOKUP problem.
GATv2 easily achieves 100% train and test accuracy even for k =100 and using only a single head.
GIN, although considered as more expressive than other GNNs, cannot perfectly ﬁt the training data (with a model size of d = 128 ) starting from k =20 .
F. L INK -P REDICTION
Results for link-prediction are showen in Table 8. [Figure] [Figure] [Table]
[Figure]
[Table]
F. QM
Standard deviation for the QM9 results of Section 4.5 are presented in Table 9.
F. P UBMED C ITATION N ETWORK
We tuned the following parameters for both GAT and GATv2: number of layers ∈ { 0 , 1 , 2 } , hidden size ∈ { 8 , 16 , 32 } , number of heads ∈ { 1 , 4 , 8 } , dropout ∈ { 0 . 4 , 0 . 6 , 0 . 8 } , bias ∈ { True, False } , share weights ∈
{ True, False } , use residual ∈ { True, False } .
Table 10 shows the test accuracy (100 runs ± stdev) using the best hyperparameters found for each model.
It is important to note that PubMed has only 60 training nodes , which hinders expressive models such as GATv2 from exploiting their approximation and generalization advantages.
Still, GATv is more accurate than GAT even in this small dataset.
In Table 15, we show that this difference is statistically signiﬁcant (p-value < 0 . 0001 ).
G A DDITIONAL C OMPARISON WITH T RANSFORMER - STYLE A TTENTION (DPGAT)
The main goal of our paper is to highlight a severe theoretical limitation of the highly popular GAT architecture, and propose a minimal ﬁx. [Table] [Table]
We perform additional empirical comparison to DPGAT, which follows Luong et al.
and the dot-product attention of the Transformer (Vaswani et al., 2017).
We deﬁne DPGAT as:
DPGAT (Vaswani et al., 2017): [Equation]
[Equation]
Variants of DPGAT were used in prior work (Gao and Ji, 2019; Dwivedi and Bresson, 2020;
Rong et al., 2020a; Veliˇckovi´c et al., 2020; Kim and Oh, 2021), and we consider it here for the conceptual and empirical comparison with GAT.
Despite its popularity, DPGAT is strictly weaker than GATv2.
DPGAT provably performs dynamic attention for any set of node representations only if they are linearly independent (see Theoremand its proof in Appendix G.1).
Otherwise, there are examples of node representations that are linearly dependent and mappings ϕ , for which dynamic attention does not hold (Appendix G.2).
This constraint is not harmful when violated in practice, because every node has only a small set of neighbors, rather than all possible nodes in the graph; further, some nodes possibly never need to be “selected” in practice.
G. P ROOF THAT DPGAT P ERFORMS D YNAMIC A TTENTION FOR L INEARLY I NDEPENDENT N ODE R EPRESENTATIONS
Theorem 3.
A DPGAT layer computes dynamic attention for any set of node representations K = Q
= { h 1 , ..., h n } that are linearly independent.
Proof.
Let G =
( V , E ) be a graph modeled by a DPGAT layer, having linearly independent node representations { h 1 , ..., h n } .
Let ϕ : [ n ] → [ n ] be any node mapping [ n ] → [ n ] .
We denote the i th row of a matrix M as M i .
We deﬁne a matrix P as:
[-P i,j = (cid:26) 1 j = ϕ ( i ) 
 otherwise (14)-][Equation]
Let X ∈ R n × R d be the matrix holding the graph’s node representations as its rows:
[Equation]
Since the rows of X are linearly independent, it necessarily holds that d ≥ n .
Next, we ﬁnd weight matrices Q ∈ R d × R d and K ∈ R d × R d such that:
[Equation]
To satisfy Equation (16), we choose Q and K such that XQ
=
U and XK
= P (cid:62) U where U is an orthonormal matrix ( U · U (cid:62) =
U (cid:62) · U = I ).
We can obtain U using the singular value decomposition (SVD) of X :
[Equation]
Since Σ ∈ R n × R n and X has a full rank, Σ is invertible, and thus:
[Equation]
Now, we deﬁne Q as follows:
[Equation] [Equation]
Note that XQ =
U , as desired.
To ﬁnd K that satisﬁes XK = P (cid:62) U , we use Equation (17) and require:
[Equation]
20) and thus:
[Equation]
We deﬁne:
[Equation]
Where e is the attention score function of DPGAT (Equation (13)).
Now, for a query i and a key j , and the corresponding representations
h
i , h j :
[Equation]
[Equation]
Since X i Q = ( XQ ) i and X j K
= ( XK ) j , we get [Equation] [Equation]
i [Equation]
j (25)
Therefore:
[-(cid:26) 1 j = ϕ ( i ) 
-][Equation]
otherwise (26)
And thus: [Equation] otherwise [Equation]
To conclude, for every selected query i and any key j (cid:54)
=
ϕ ( i ) : [Equation]
[Equation]
and due to the increasing monotonicity of softmax : [Equation] [Equation]
Hence, a DPGAT layer computes dynamic attention.
In the case that d > n , we apply SVD to the full-rank matrix XX (cid:62) ∈ R n × n , and follow the same steps to construct Q and K .
In the case that Q ∈ R d × R d k and K ∈ R d × R d k and d k > d , we can use the same Q and K (Equations (19) and (21)) padded with zeros.
We deﬁne the Q (cid:48)
∈ R d × R d key and K (cid:
) ∈ R d × R d key as follows:
[Equation]
[Equation]
[Equation]
G. DPGAT IS STRICTLY WEAKER THAN GAT V
There are examples of node representations that are linearly dependent and mappings ϕ , for which dynamic attention does not hold.
First, we show a simple 2-dimensional example, and then we show the general case of such examples.
[Equation]
Consider the following linearly dependent set of vectors K = Q (Figure 6):
[Equation]
[Equation]
[Equation]
where ˆ x and ˆ y are the cartesian unit vectors.
We deﬁne β ∈ { 0 , 1 , 2 } to express { h 0 , h 1 , h 2 } using the same expression:
[Equation]
Let q ∈ Q be any query vector.
For brevity, we deﬁne the unscaled dot-product attention score as s :
[Equation]
Where e is the attention score function of DPGAT (Equation (13)).
The (unscaled) attention score between q and { h 0 , h 1 , h 2 } is:
[Equation]
The ﬁrst term (cid:0) q (cid:62) Q (cid:1) (cid:0) ˆ x (cid:62) K (cid:1) (cid:62) is unconditioned on β , and thus shared for every h β .
Let us focus on the second term β (cid:0) q (cid:62) Q (cid:1) (cid:0) ˆ y (cid:62) K (cid:1) (cid:62) .
If (cid:0) q (cid:62) Q (cid:1) (cid:0) ˆ y (cid:62) K (cid:1) (cid:62) > 0 , then:
[Equation] [Equation]
Otherwise, if
[Equation]
Thus, for any query q , the key h 1 can never get the highest score, and thus cannot be “selected”.
That is, the key h 1 cannot satisfy that e ( q , h 1 ) is strictly greater than any other key.
In the general case, let h 0 ,
h 1 ∈ R
d be some non-zero vectors , and λ is some scalar such that 0 < λ < 1 .
Consider the following linearly dependent set of vectors:
[Equation]
For any query q ∈ Q and β ∈ { 0 , λ, 1 } we deﬁne:
[Equation]
Where e is the attention score function of DPGAT (Equation (13)).
Therefore:
[Equation] [Equation] [Equation] [Equation]
[Equation]
If (cid:0) q (cid:62) Q (cid:1) (cid:0) h (cid:62) 1 K − h (cid:62)
K (cid:1) (cid:62) > 0 : e ( q , h 1 ) > e ( q
[Equation] h λ ) (50)
Otherwise, if (cid:0) q (cid:62) Q (cid:1) (cid:0) h (cid:62)
K − h (cid:62)
K (cid:1) (cid:62) ≤ 0 : e ( q , h 0 ) ≥ e ( q , h λ ) (51)
Thus, for any query q , the key h λ cannot be selected.
That is, the key h λ cannot satisfy that e ( q , h λ ) is strictly greater than any other key.
Therefore, there are mappings ϕ , for which dynamic attention does not hold.
While we prove that GATv2 computes dynamic attention (Appendix B) for any set of node representations K = Q , there are sets of node representations and mappings ϕ for which dynamic attention does not hold for DPGAT.
Thus, DPGAT is strictly weaker than GATv2.
G. E MPIRICAL E VALUATION
Here we repeat the experiments of Section 4 with DPGAT.
We remind that DPGAT is strictly weaker than our proposed GATv2 (see a proof in Appendix G.1).
H S TATISTICAL S IGNIFICANCE
Here we report the statistical signiﬁcance of the strongest GATv2 and GAT models of the experiments reported in Section 4.
[Figure]
[Figure]
DPGATGATv2GAT
[Figure]
DPGATGATv2GAT
[Figure]
[Table] [Table]
[Figure]
[Table]
[Table]
[Table]
% GATv 8 h
[Table]
< 0 . < 0 .0 .
< 0 .
< 0 .p - v a l u e < 0 .
0001 <
0 .noise ratio
A cc u r ac y
[Figure]
[Figure]
[Figure]
.0 .p - v a l u e < 0 .
< 0 .
< 0 .
0001 < 0 .
< 0 .
<
.noise ratio
[Figure]
Model
[Table]
[Table] [Table] [Table]
[Table]
I C OMPLEXITY A NALYSIS
We repeat the deﬁnitions of GAT, GATv2 and DPGAT:
[Equation]
Veliˇckovi´c et al.
[Equation]
our ﬁxed version
[Equation]
I. T IME C OMPLEXITY
GAT As noted by Veliˇckovi´c et al.
(2018), the time complexity of a single GAT head may be expressed as O ( |V| dd (cid:48)
+ |E| d (cid:48) ) .
Because of GAT’s static attention, this computation can be further optimized, by merging the linear layer a 1 with W , merging a 2 with W , and only then compute a (cid:62){ 1 , 2 } W h i for every i ∈ V .
GATv2 require the same computational cost as GAT’s declared complexity: O ( |V| dd (cid:48)
+ |E| d (cid:48) )
: we denote W =
[ W 1 (cid:107) W 2 ] , where W 1 ∈ R d (cid:48)
×
d and W d (cid:48)
× dcontain the left half and right half of the columns of W , respectively.
We can ﬁrst compute W 1 h i and W
2 h j for every i, j ∈ V .
This takes
O ( |V| dd (cid:48) ) .
Then, for every edge ( j, i ) , we compute LeakyReLU
( W · [ h i (cid:107) h j ]) using the precomputed W 1 h i and W 2 h j , since W · [ h i (cid:107) h j ]
= W 1 h
i + W 2 h j .
This takes O ( |E| d (cid:48) ) .
Finally, computing the results of the linear layer a takes additional O ( |E| d (cid:48) ) time, and overall
O ( |V| dd (cid:48)
+ |E| d (cid:48) ) .
DPGAT also takes the same time.
We can ﬁrst compute h (cid:62) i Q and h (cid:62) j K for every i, j ∈ V .
This takes O ( |V| dd (cid:48) ) .
Computing the dot-product (cid:0) h (cid:62) i Q (cid:1) (cid:0) h (cid:62) j K (cid:1) (cid:62) for every edge ( j, i ) takes additional
O ( |E| d (cid:48) ) time, and overall O ( |V| dd (cid:48)
+ |E| d (cid:48) ) .
I. P ARAMETRIC C OMPLEXITY
GAT
GATv
DPGAT
Ofﬁcial 2 d (cid:48)
+ dd (cid:48) d (cid:48)
+ 2 dd (cid:48) 2 dd k + dd (cid:48)
In our experiments 2 d (cid:48) + dd (cid:48) d (cid:48)
+ dd (cid:48) 2 dd (cid:48)
All parametric costs are summarized in Table 19.
All following calculations refer to a single layer having a single attention head, omitting bias vectors.
GAT has learned vector and a matrix: a ∈ R 2 d (cid:48) and W ∈ R d (cid:48)
× d , thus overall 2 d (cid:48) + dd (cid:48) learned parameters.
GATv2 has a matrix that is twice larger: W ∈
R d (cid:48) × 2 d , because it is applied on the concatenation [ h i (cid:107) h j ] .
Thus, the overall number of learned parameters is d (cid:48)
+ 2 dd (cid:48) .
However in our experiments, to rule out the increased number of parameters over GAT as the source of empirical difference, we constrained W =
[ W (cid:48)
(cid:107) W (cid:48) ] , and thus the number of parameters were d (cid:48)
+ dd (cid:48) .
DPGAT has Q and K matrices of sizes dd k each, and additional dd (cid:48) parameters in the value matrix
V , thus 2 dd k + dd (cid:48) parameters overall.
However in our experiments, we constrained Q = K and set d k = d (cid:48) , and thus the number of parameters is only 2 dd (cid:48) .