liveBook · Manning

Graph attention network

As explained in Knowledge Graphs and LLMs in Action

A graph attention network is a graph neural network that uses learned attention weights to aggregate information from neighboring nodes.

A graph attention network (GAT) is a graph neural network that learns how strongly each neighboring node should influence a target node. Instead of treating normalized neighbors equally, it assigns learnable attention weights and aggregates a weighted neighborhood sum. This is useful when relationships differ in relevance, graphs contain noisy connections, or node relationships are nonlinear. section 11.6.2

A practical GAT pipeline connects attention-based encoding to a downstream prediction task:

  • Represent domain objects and their interactions as a graph, such as transactions connected by Bitcoin flows or users connected to movies through ratings. section 12.0
  • Encode node features by aggregating neighboring representations with learned attention coefficients rather than uniform neighborhood contributions. section 11.6.2
  • Pass the encoder output to a task-specific decoder; node classification uses log_softmax with CrossEntropyLoss, while link prediction evaluates predicted interactions. section 12.1.4 section 12.0

PyTorch Geometric encoder

A two-layer GAT implementation uses GATConv layers. The first layer produces hidden representations with eight attention heads and concatenates them; the second uses one head and disables concatenation to produce the output dimension. Self-loops can be enabled or disabled through add_self_loops. section 12.1.4

Listing 12.13 GAT model implemented by extending the base class
from torch_geometric.nn import GATConv

class GAT(BaseGraphModel):
    def __init__(self, input_dim,
                       hidden_dim,
                       out_dim,
                       num_heads=8,
                       add_self_loops=True):   #1
        concat_hidden_dim = hidden_dim * num_heads   #2

        conv1 = GATConv(   #3
            in_channels=input_dim,
            out_channels=hidden_dim,
            heads=num_heads,
            add_self_loops=add_self_loops
        )

        conv2 = GATConv(   #4
            in_channels=concat_hidden_dim,
            out_channels=out_dim,
            heads=1,  #5
            add_self_loops=add_self_loops,
            concat=False   #6
        )

        super(BaseGraphModel, self).__init__()   #7
        self.conv1 = conv1   #8
        self.conv2 = conv2   #9

Inspect the complete GAT model definition, including multi-head dimensions, self-loop configuration, and the two convolutional layers. section 12.1.4

Attention adds learnable coefficients and therefore increases parameter count and training cost. In the reported comparisons, GAT was the least efficient encoder for both node classification and link prediction; precision, recall, F1-score, and confusion matrices support evaluation across classes and recommendations. section 12.1.5 section 12.2.5

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Explains weighted aggregation and PyG multi-head configuration, then stops short of deriving the attention equations fully.
Examples
Uses Bitcoin transaction classification, AML detection, and user–movie recommendation link prediction as concrete applications.
What it assumes
Assumes familiarity with graph neural network encoders, node features, edges, and downstream prediction tasks.
What you can do after
Apply it via PyTorch Geometric to build an encoder and evaluate predictions with precision, recall, F1-score, and confusion matrices.
How it connects
Connects graph attention networks with graph neural networks, GraphSAGE, graph convolutional networks, node classification, and link prediction.

Also explained in

Compare how 2 books explain graph attention network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Knowledge Graphs and LLMs in Action. The section links point to the parts of the book it draws on.