~/writing
Graph ML

Graph Attention Networks

An explanation of Graph Attention Networks (GATs), self-attention mechanics, and how multi-head attention addresses limitations in traditional GCNs.

Overview

Graph Attention Networks (GATs) are one of the most popular types of graph neural networks. With other types such as graph convolutional networks (GCN), every neighboring node has the same importance, which should not be the case as some nodes would be more essential than others.

Some cases would be in fraud detection, where fraud rings try to hide fraudulent activities within a majority of normal transactions, which would be able to fool GCNs since they label each node with the same importance.

GATs solve this by considering the importance of each neighbor, through an attention mechanism which assigns a weighting factor to every connection.


Self-Attention Mechanism

Self-attention refers to comparing inputs to one another. This mechanism attaches a weighting factor (attention score αij) to each connection.

Self Attention Mechanism

Node Embedding Calculation

The formula for calculating the embedding of node 1 (h1) is computed as:

h1 = α11Wx1 + α12Wx2 + α13Wx3 + α14Wx4

where W is a shared linear transformation weight matrix.


Computing Attention Scores

The attention score is calculated through a 3-step process:

  1. Linear transformation
  2. Activation function
  3. Softmax function

1. Linear Transformation

Given an input node i with feature vector hi, a linear transformation is applied through a weight matrix W:

h’i = Whi

2. Unnormalized Attention Coefficients

To calculate how much attention node i should attend to neighbor node j, these hidden representations are concatenated (||) and a parameterized attention vector aT is applied, followed by an activation function like LeakyReLU:

eij = LeakyReLU( aT [ Whi || Whj ] )

eij measures the unnormalized importance (attention coefficient) of node j’s feature representation to node i.

3. Softmax Normalization

Lastly, to make these coefficients comparable across nodes with varying degrees, the scores are normalized using a Softmax function:

αij = Softmaxj( eij ) = exp( LeakyReLU( aT [ Whi || Whj ] ) ) / ∑k ∈ Ni exp( LeakyReLU( aT [ Whi || Whk ] ) )

This represents the final attention weight assigned by node i to node j.


Multi-Head Attention

Instead of just doing the 3 steps above once, they are applied multiple times. Multi-head attention helps to reduce noise by evaluating one particular neighbor.

  • Variance Reduction: A single attention head might mistakenly put 99% of its focus on a noisy/irrelevant neighboring node. By using multiple attention heads and aggregating them, it helps to create smooth, reliable gradients during training and average variance.
  • Multi-Relational Capacity: Multi-head attention enables the neural network to learn different types of relationships, as nodes in a graph can connect for different reasons.