liveBook · Manning

Local neighborhood

As explained in Graph Neural Networks in Action

A node neighborhood is the set of nodes connected to a particular graph node, typically within a specified number of hops.

A local neighborhood gives a graph model a place to start when updating a node. For one-hop convolution, it consists of the starting node and the nodes connected to it by one edge. Unlike a patch of image pixels, it has no fixed shape or node order. section 3.4.1

A neighborhood-based update follows three practical steps:

  • Find the central node’s immediate neighbors through the graph’s edges. section 3.4.1
  • Gather their features and combine them with an aggregation such as a sum, mean, or maximum. The result must not depend on neighbor order. section 3.4.1 section 3.2.1
  • Use the combined information to update the node’s representation; successive layers repeat this process. section 3.2.1 section 4.3.2
Figure 3.12 A comparison of convolution over grid data (left; e.g., a 2D image) and over a graph (right).
figure

Compare the fixed grid patch with the differently shaped set of nodes reached by one graph edge. section 3.4.1

In PyTorch Geometric, `SAGEConv` accepts an `aggr` setting for the neighborhood operation; `GCNConv` uses a built-in weighted average. Choosing an operation changes how neighboring features contribute, not which nodes an edge connects. section 3.2.1

Listing 3.5 GraphSAGE class with aggregation parameter
class GraphSAGE(torch.nn.Module):
    def __init__(self, in_channels, \
hidden_channels, out_channels, agg_func='mean'):  #1
        super(GraphSAGE, self).__init__()
        self.conv1 = SAGEConv(in_channels, \
hidden_channels, aggr=agg_func)   #2
        self.conv2 = SAGEConv(hidden_channels, \
out_channels, aggr=agg_func)   #3

    def forward(self, x, edge_index):
         x = self.conv1(x, edge_index)
         x = F.relu(x)
         x = self.conv2(x, edge_index)

    return F.log_softmax(x, dim=1)

The GraphSAGE listing passes `agg_func` to each `SAGEConv` layer as its `aggr` parameter. section 3.2.1

A training mask selects which nodes contribute to the loss, but it does not remove their neighbors from message passing. A GCN still needs the graph structure during its forward pass to compute the local aggregations used for those predictions. section 4.3.2

How Graph Neural Networks in Action covers it

How deep it goes
Defines one-hop membership and explains order-independent aggregation, then shows configurable layers without deriving their mathematics.
Examples
Compares an image grid with a graph and configures aggregation in a two-layer GraphSAGE model.
What it assumes
Builds the neighborhood definition from nodes and edges while assuming familiarity with node features and model layers for implementation.
What you can do after
Apply it via PyTorch Geometric SAGEConv to choose an aggregation and preserve neighbor context during masked training.
How it connects
Connects local neighborhoods to graph convolution, neighborhood aggregation, message passing, and training masks.

Also explained in

Compare how 4 books explain local neighborhood →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.