Supervised Attention Using Homophily in Graph Neural Networks
Graph neural networks have become the standard approach for dealing with
learning problems on graphs. Among the different variants of graph neural
networks, graph attention networks (GATs) have been applied with great success
to different tasks. In the GAT model, each node assigns an importance score to
its neighbors using an attention mechanism. However, similar to other graph
neural networks, GATs aggregate messages from nodes that belong to different
classes, and therefore produce node representations that are not well separated
with respect to the different classes, which might hurt their performance. In
this work, to alleviate this problem, we propose a new technique that can be
incorporated into any graph attention model to encourage higher attention
scores between nodes that share the same class label. We evaluate the proposed
method on several node classification datasets demonstrating increased
performance over standard baseline models.
Michail Chatzianastasis, Giannis Nikolentzos, Michalis Vazirgiannis
# Supervised Attention Using Homophily in Graph Neural Networks

###### Abstract

Graph neural networks have become the standard approach for dealing with learning problems on graphs. Among the different variants of graph neural networks, graph attention networks (GATs) have been applied with great success to different tasks. In the GAT model, each node assigns an importance score to its neighbors using an attention mechanism. However, similar to other graph neural networks, GATs aggregate messages from nodes that belong to different classes, and therefore produce node representations that are not well separated with respect to the different classes, which might hurt their performance. In this work, to alleviate this problem, we propose a new technique that can be incorporated into any graph attention model to encourage higher attention scores between nodes that share the same class label. We evaluate the proposed method on several node classification datasets demonstrating increased performance over standard baseline models.

Keywords:Graph Neural Networks Graph Attention Networks Supervised Attention

## 1 Introduction

Graph neural networks (GNNs) have recently emerged as a general framework for learning graph representations and have been applied with great success in different domains such as in bioinformatics [22], in physics [9] and in natural language processing [29], just to name a few. Among others, GNNs have been used to generate molecules with specific chemical characteristics [24], to predict compound-protein interactions for drug discovery [34] and to detect misinformation in social media [16].

While different types of GNNs have been proposed, most of these models follow an iterative message passing scheme, where each node aggregates information from its neighbors [12]. One of the most popular classes of this kind of models are the graph attention networks (GATs) [35, 3, 6, 19]. GATs employ an attention mechanism which can capture the importance of each neighbor and are thus considered state-of-the-art models in various graph learning tasks. These models are also highly interpretable since the learned attention scores can provide information about the relevance of the neighboring nodes.

Unfortunately, real-world graphs often contain noise, as there usually exist edges between unrelated nodes. In such a setting, once multiple message passing
steps are performed, nodes will end up receiving too much noisy information from nodes that belong to different classes, thus leading to indistinguishable representations. This problem is known as oversmoothing in the graph representation learning literature [7, 4], and can dramatically harm the performance of GNNs in the node classification task. Several approaches have been proposed to address the issue of oversmoothing such as normalization layers [39, 11], generalized band-pass filtering operations [25], and approaches that change the graph structure [7]. However, most of them are computationally expensive or require extensive architectural modifications.

In this work, we focus on removing the noisy information from the graph using an attention mechanism. Specifically, we propose a new loss function that encourages nodes to mainly attend to nodes that belong to the same class, and to a lesser extent to nodes that belong to different classes by supervising the attention scores. To motivate our approach, we first experimentally verify that GNNs perform better as the edge homophily in the graph increases, i. e., as we remove inter-class edges. Therefore, it is important to learn attention scores close to 0 for the inter-class edges. Furthermore, we demonstrate that the proposed method outperforms various baselines in real-world node classification tasks. Our approach is computationally efficient, and it can be applied to any graph attention model with minimal modifications in the architecture. Finally, we visualize the distribution of the learned attention scores of our proposed model and of vanilla graph attention networks, for the intra- and the inter-class edges. We verify that our proposed model learns higher attention scores for the intra-class edges, leading to high quality node representations.

Our contributions can be summarized as follows:

* We show experimentally that GNNs perform better as the edge homophily in the graph increases, and that it is important to learn attention scores close to 0 for the inter-class edges.
* We propose a novel loss function for attentional GNNs that encourages nodes to attend mainly to nodes that belong to the same class, and to a lesser extent to nodes that belong to different classes.
* We show that our approach outperforms various baselines in real-world node classification tasks.

The rest of the paper is organized as follows. Section 2 presents the related work. Section 3 introduces the proposed loss function. Finally, Sections 4 and 5 present the experimental results and conclusions, respectively.

## 2 Related Work

Graph Neural Networks (GNNs) have received significant attention in the past years, with a growing number of research works proposing novel methods and applications. The first GNN models were proposed several years ago [33, 30], however with the rise of deep learning, GNNs have gained renewed interest in the research community [21, 15]. The majority of GNN models can be reformulated
into a single common framework known as Message Passing Neural Networks (MPNNs) [12]. These models iteratively update a given node's representation by aggregating the feature vectors of its neighbors. Graph attention networks (GATs) correspond to one of the major subclasses of MPNNs [35, 3, 6]. GATs employ an attention mechanism which allows them to incorporate explicit weights for each neighbor. One of the main advantages of these models is that they are highly interpretable due to the learned attention scores. Numerous studies have proposed several enhancements and expansions to the message passing mechanism of MPNNs. These include among others, works that use more expressive or learnable aggregation functions [26, 32, 10, 6], schemes that operate on high-order neighborhoods of nodes [1, 17, 28], and approaches that operate in the hyperbolic space [23, 5, 27]. However, a common issue that affects the performance of various MPNNs is oversmoothing. Several studies have investigated the causes and effects of oversmoothing, as well as potential solutions to mitigate this problem, including normalization techniques [38, 11] and graph rewiring methods [7, 18].

## 3 Methodology

### Preliminaries

Let \(G=(V,E)\) be an undirected graph where \(V\) is a set of nodes and \(E\) is a set of edges. We will denote by \(N\) the number of vertices and by \(M\) the number of edges, i. e., \(N=|V|\) and \(M=|E|\). Then, we have that \(V=\{v_{1},v_{2},\ldots,v_{N}\}\). Let \(\mathbf{A}\in\mathbb{R}^{N\times N}\) denote the adjacency matrix of \(G\), \(\mathbf{X}=[\mathbf{x}_{1},\mathbf{x}_{2},\ldots,\mathbf{x}_{N}]^{\top}\in\mathbb{R}^{N\times d}\) be the matrix that stores the node features, and \(\mathbf{Y}=[y_{1},y_{2},\ldots,y_{N}]^{\top}\in\{1,\ldots,C\}^{N}\) the vector that stores the nodes' class labels where \(C\) is the number of classes. Let \(\mathcal{N}(i)\) denote the indices of the neighbors of node \(v_{i}\), i. e., the set \(\{j\colon\{v_{i},v_{j}\}\in E\}\). We denote the features of the neighbors of a node \(v_{i}\) by the multiset \(\mathbf{X}_{\mathcal{N}(i)}=\{\mathbf{x}_{j}\colon j\in\mathcal{N}(i)\}\). We also define the neighborhood of \(v_{i}\) including \(v_{i}\) as \(\overline{\mathcal{N}}(i)=\mathcal{N}(i)\cup\{i\}\) and the corresponding features as \(\mathbf{X}_{\overline{\mathcal{N}}(i)}\). Given a training set of nodes, the goal of supervised node classification is to learn a mapping from the node set to the set of labels, \(f:V\rightarrow\{0,1,\ldots,C\}\).

### Graph Neural Networks

GNNs typically use the graph structure \(\mathbf{A}\) along with the node features \(\mathbf{X}\) to learn a representation \(\mathbf{h}_{i}\) for each node \(v_{i}\in V\)[14]. As already discussed, most GNNs employ a message-passing scheme [12] where every node updates its representation by aggregating the representations of its neighbors and combining them with its own representation. Since there is no natural ordering of the neighbors of a node, the aggregation function needs to be permutation invariant, and usually has a significant impact on the performance and the expressiveness of the GNN model [37]. Common aggregation functions include the sum, mean, max, and min operators, but also attention-based pooling aggregators [21, 15].
In this work, we mainly focus on attention-based aggregators, where the representation of each node \(v_{i}\) is updated using a weighted sum of the representations of its neighbors:

\[\mathbf{h}_{i}=\sigma\left(\sum_{j\in\overline{\mathcal{N}}(i)}\alpha_{ij}\mathbf{W}\mathbf{h }_{j}\right) \tag{1}\]

where \(\mathbf{h}_{i}\in\mathbb{R}^{d}\) denotes the hidden representation of node \(v_{i}\), \(\mathbf{W}\in\mathbb{R}^{d_{o}\times d}\) is a weight matrix and \(\alpha_{ij}\) is the learned attention score (e. g., how much node \(v_{i}\) attends to node \(v_{j}\)). Equation (1) is applied iteratively, however, for ease of notation, we have dropped the superscript (that denotes the iteration number). Among the different attention models that have been proposed in the past years, the Graph Attention Network (GAT) [35] computes the attention scores by applying a single-layer feedforward neural network in the concatenated node features of the two nodes, while GATv2 [3], an improved version of GAT, computes more expressive and dynamic attention. In our experiments, we use GATv2 as the backbone network, but our approach can be easily applied to any graph attention model. Specifically, we compute the un-normalized attention score between two nodes \(v_{i},v_{j}\) using the following equation:

\[e_{ij}=\mathbf{a}^{\top}\text{LeakyReLU}\left(\mathbf{W}_{2}\left[\mathbf{h}_{i}\|\mathbf{h}_{ j}\right]\right) \tag{2}\]

where \(\mathbf{a}\in\mathbb{R}^{d_{o}}\) is a weight vector and \(\mathbf{W}_{2}\in\mathbb{R}^{d_{o}\times 2d}\) a weight matrix. Then, we apply the softmax function to normalize the attention scores across all neighbors of \(v_{i}\):

\[\alpha_{ij}=\frac{\exp\left(e_{ij}\right)}{\sum_{k\in\mathcal{N}(i)}\exp\left( e_{ik}\right)} \tag{3}\]

### Problem of Information Mixing

Definition 1 (Edge Homophily): Given a graph \(G=(V,E)\) with a vector of node class labels \(\mathbf{Y}\), the edge homophily ratio is the fraction of intra-class edges in the graph, i. e., the fraction of edges that connect nodes with the same labels.

\[h(G,\mathbf{Y})=\frac{\left|\left\{\left(v_{i},v_{j}\right)\colon\left(v_{i},v_{j} \right)\in E\wedge y_{i}=y_{j}\right\}\right|}{\left|E\right|} \tag{4}\]

The problem of information mixing or oversmoothing [7; 36] occurs mainly in cases where there are edges between nodes that belong to different classes (i. e., inter-class edges). In each message passing iteration, information will be exchanged through these "noisy" edges, leading nodes that belong to different classes into obtaining highly non-separable representations. Therefore, the node classification task is becoming extremely challenging. Ideally, we would like to identify and eliminate those "noisy" edges, such that nodes will only aggregate information from intra-class edges.
In this paper, we leverage graph attention networks in order to alleviate this issue. Specifically, we encourage the network to learn attention scores that minimize information mixing in the graph. Note that a node \(v_{i}\) receives noisy information as follows:

\[\text{noise}(i)=\sum_{j\in\mathcal{N}_{\text{inter}}(i)}\alpha_{ij}\mathbf{W}\mathbf{h}_ {j} \tag{5}\]

where \(\mathcal{N}_{\text{inter}}(i)\) is the set of indices of the inter-class neighbors of node \(v_{i}\). Therefore, we would like our attention scores to satisfy the following equation:

\[\left\{\alpha_{ij}^{*}\colon j\in\mathcal{N}_{\text{inter}}(i)\right\}=\operatorname* {arg\,min}_{\left\{\alpha_{ij}\colon j\in\mathcal{N}_{\text{inter}}(i)\right\} }\sum_{j\in\mathcal{N}_{\text{inter}}(i)}||\alpha_{ij}\mathbf{W}\mathbf{h}_{j}|| \tag{6}\]

The solution of the above equation gives us \(\alpha_{ij}=0\) for all the inter-class edges \(\{v_{i},v_{j}\}\).

### Supervised Attention using Homophily (HS-GATv2)

Based on the previous analysis, we propose a new loss function for training graph attention networks that deals with the information mixing problem. Specifically, we propose to supervise the attention scores between the edges, by providing labels that indicate if the edge is an intra- or an inter-class edge. Let \(V_{\text{train}}\) denote a set that contains the indices of the nodes that belong to the training set. Let also \(E_{\text{train}}\) denote the training edge set which consists of all the edges where both source and target nodes belong to the training node set, i. e., \(E_{\text{train}}=\left\{\{v_{i},v_{j}\}\colon\{v_{i},v_{j}\}\in E\wedge i\in V _{\text{train}}\wedge j\in V_{\text{train}}\right\}\). Formally, the proposed loss function combines the following two terms: (1) the cross-entropy loss between model predictions and class labels of nodes (denoted by \(L_{V}\)); and (2)

Figure 1: An illustration of our proposed method (**HS-GATv2**). We combine the loss \(L_{V}\) from the node classification task and the loss \(L_{E}\) from the attention scores based on the training edges, to push neighbor nodes with the same class to have large attention scores and nodes from different classes to have small attention scores. Our approach is applicable to any graph attention model, by setting accordingly the attention function \(\mathbf{f}\).
the supervised attention losses for the edges between nodes of the training set (denoted by \(L_{E}\)) with mixing coefficient \(\lambda\):

\[\begin{split} L&=L_{V}+\lambda\,L_{E}\\ L_{V}&=-\frac{1}{|V_{\text{train}}|}\sum_{i\in V_{ \text{train}}}\sum_{c=1}^{C}y_{i,c}\log(\mathbf{p}_{i,c})\\ L_{E}&=-\frac{1}{T\,|E_{\text{train}}|}\sum_{t=1}^{T }\sum_{e\in E_{\text{train}}}\Big{(}y_{e}\log\big{(}\sigma(e_{e}^{(t)})\big{)} \\ &\qquad\qquad\qquad+(1-y_{e})\log\big{(}1-\sigma(e_{e}^{(t)}) \big{)}\Big{)}\end{split} \tag{7}\]

where \(y_{i,c}\) indicates if node \(v_{i}\) belongs to the class \(c\) (i. e., \(y_{i,c}=1\) if \(v_{i}\) belongs to class \(c\), and \(0\) otherwise), \(\mathbf{p}_{i,c}\) is the predicted probability of node \(v_{i}\) belonging to class \(c\), \(e_{e}^{(t)}\) is the un-normalized attention score of edge \(e\) in the \(t\)-th message passing layer, and \(y_{e}\) is the label of the edge (i. e., \(1\) if source and target nodes belong to the same class, and \(0\) otherwise). An illustration of the proposed method is given in Figure 1.

## 4 Experiments

In this section, we extensively evaluate our method on synthetic as well as standard node classification datasets, and compare it against state-of-the-art GNNs. As already mentioned, we apply the proposed method to the GATv2 model.

### Adjusting Homophily in Graphs

Our method is based on the assumption that node classification is easier in homophilic graphs, since nodes from the different classes will have separable representations. In this experiment, we try to verify this claim. Therefore, we test the performance of various GNNs by adjusting the edge homophily in various graph datasets. Specifically, we remove \(k|E_{\text{inter}}|\) inter-class edges from each dataset, where \(|E_{\text{inter}}|\) is the number of inter-class edges in a graph. Setting \(k=0\) corresponds to the original graph and \(k=1\) corresponds to a fully homophilic graph. We report the results for Cora, Citeseer and Disease datasets in Figure 2. We observe that the performance increases for all the models, as the homophily of the graph increases. Our approach is strongly motivated by this observation, since the proposed loss function encourages the attention scores of inter-class edges to be close to \(0\), thus generating a more homophilic-like setting.

### Node Classification Benchmarks

**Baselines.** We compare our approach (HS-GATv2) against the following state-of-the-art GNN models: Graph Convolutional Network (GCN) [21], GraphSAGE [15],
Figure 2: Test performance of GNNs by removing \(k|E_{\text{inter}}|\) inter-class edges. Setting \(k=0\) corresponds to the original graph and \(k=1\) corresponds to a fully homophilic graph. Performance improves as the ratio of homophilic edges increases.
Graph Attention Network (GAT) [35], GATv2 [3], and Principal Neighbourhood Aggregation (PNA) [8].

**Datasets.** We utilize four well-known node classification benchmark datasets to evaluate our approach in real-world scenarios. We use three citation network datasets: Cora, CiteSeer and Pubmed [31], where each node corresponds to a scientific publication, edges correspond to citations and the goal is to predict the category of each publication. We follow the experimental setup of [21] and use 140 nodes for training, 300 for validation and 1000 for testing. We further use one disease spreading model: Disease [5]. It simulates the SIR disease spreading model [2], where the label of a node indicates if it is infected or not. We follow the experimental setup of [5] and use 30/10/60% for training, validation and test sets and report the average results from 10 different random splits.

**Experimental Setup.** We use the Adam optimizer [20] with the Glorot initialization [13]. We search the layers from \(\{1,2\}\) and the attention heads from \(\{1,4,8\}\). We set the weight decay equal to \(5\mathrm{e}{-5}\). We fix the mixing coefficient \(\lambda\) to \(0.1\). We search the hidden dimensions from \(\{8,16,32,64,128\}\), the learning rate from \(\{0.001,0.005\}\) and the dropout rate from \(\{0.0,0.2,0.5\}\).

**Results** Table 1 illustrates the obtained test accuracies. We observe that the proposed HS-GATv2 method outperforms the baselines on all three datasets. This highlights the ability of the proposed approach to use the attention mechanism to reduce the noisy information that each node receives from its neighbors, thus producing high-quality node representations.

### Distribution of Attention Scores

In this experiment, we compute the distribution of the un-normalized attention scores produced by HS-GATv2 and GATv2 for edges whose endpoints are not in the training set. The results for the Cora dataset are illustrated in Figure 3. Attention scores obtained from GATv2 have the same distribution for the intra- and inter-class edges. On the other hand, we observe that HS-GATv2 produces higher attention values for the intra-class edges even though it has not seen them during training. This allows our model to reduce the noisy information in the message passing procedure, and to focus mainly on the homophilic edges.

\begin{table}
\begin{tabular}{l c c c c} \hline \hline
**Method** & **Cora** & **Citeseer** & **Disease** & **Pubmed** \\ \hline MLP & 43.8 & 52.9 & 79.1 \(\pm\) 1.0 & 74.2 \(\pm\) 0.2 \\ GCN & 81.4 & 67.5 & 89.0 \(\pm\) 2.2 & 77.8 \(\pm\) 0.3 \\ GraphSAGE & 77.2 & 65.3 & 88.8 \(\pm\) 2.0 & 77.9 \(\pm\) 0.6 \\ PNA & 76.4 & 58.9 & 86.8 \(\pm\) 1.9 & 75.8 \(\pm\) 0.6 \\ \hline GAT & 82.5 & 70.6 & 88.1 \(\pm\) 2.5 & 78.1 \(\pm\) 0.6 \\ GATv2 & 83.5 & 71.6 & 89.2 \(\pm\) 1.7 & 78.5 \(\pm\) 0.4 \\
**HS-GATv2 (ours)** & **85.3** & **73.5** & **89.3**\(\pm\) 3.3 & **79.1**\(\pm\) 0.3 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Test accuracy in the node classification benchmarks.
## 5 Conclusion

In this paper, we introduced a new type of graph attention model that uses supervision in the attention scores by exploiting the network homophily. Our proposed loss function contains a loss term that encourages attention scores to be high between nodes that share the same label and therefore alleviates the problem of information mixing in GNNs. Our extensive experiments demonstrate an increase in the performance of the proposed method over state-of-the-art GNNs such as GAT and GATv2 in node classification tasks.

## Acknowledgements

G.N. is supported by the French National research agency via the AML-HELAS (ANR-19-CHIA-0020) project.
