liveBook · Manning

Attention

As explained in Knowledge Graphs and LLMs in Action

An attention mechanism is a technique that assigns learned importance weights to inputs so a model can prioritize the most relevant information.

Attention assigns learned importance weights to inputs, enabling a graph neural network to prioritize relevant neighbors during message aggregation instead of treating every connection equally. section 11.6 section 11.6.2

A neighborhood-attention pipeline typically:

  • Transforms node and neighbor features into representations used to compare relationships. section 11.6.3
  • Computes attention scores, normalizes them with softmax, and forms weighted neighbor values. section 11.6.3
  • Combines parallel heads so different components can learn different neighborhood patterns. section 11.6.3

Graph relationships

In a molecular graph, one attention head may emphasize chemical bond types while another captures spatial arrangements. Structural encodings can add degree-based topology information. section 11.6.3 section 11.6.3

Listing 11.3 Attention mechanism in transformer architecture
class TransformerStyleGraphAttention(nn.Module):
   def __init__(self, feature_dim):
       super().__init__()
       self.query_transform = nn.Linear(feature_dim, feature_dim)  
       self.key_transform = nn.Linear(feature_dim, feature_dim)
       self.value_transform = nn.Linear(feature_dim, feature_dim)  #1

   def forward(self, node_features, neighbor_features):
       Q = self.query_transform(node_features)
       K = self.key_transform(neighbor_features)
       V = self.value_transform(neighbor_features)   #2

       attention_scores = torch.matmul(Q, K.transpose(-2, -1)) #3
       attention_scores = attention_scores / math.sqrt(K.size(-1))  #3

       attention_weights = F.softmax(attention_scores, dim=-1) #4
       attended_values = torch.matmul(attention_weights, V)  #4

       return attended_values   #5

Inspect a PyTorch-style implementation that computes query, key, and value projections before scaled attention and weighted aggregation. section 11.6.3

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Explains weighted aggregation, learnable scoring, multihead variants, transformer connections, and structural encoding with implementation listings.
Examples
Uses social friendships, molecular bonds and spatial arrangements, PyTorch attention modules, and degree-based structural encodings.
What it assumes
Assumes graph neural network message passing and feature representations while motivating attention through unequal neighbor relevance.
What you can do after
Build it from scratch using weighted neighbor aggregation, multihead modules, transformer-style projections, and structural features.
How it connects
Connects graph neural networks with message passing, GraphSAGE, graph attention networks, transformers, and positional encoding.

Also explained in

Compare how 28 books explain attention →

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.