Neural networks

Graph neural network

Imagine each person in a friendship network updating their notes after hearing from friends. A graph neural network updates a node’s representation using messages from connected nodes. Repeating this process lets information travel farther across the graph.

Neighbor Avalue = 2Neighbor Bvalue = 4Target nodeown value = 1Updatedtarget1 + (2 + 4)/2 =4
Messages follow the two edges; a toy update adds their mean to the target node’s own value.

How it works

  1. Initialize node features and include edge features if the task needs them.
  2. Compute messages along edges from neighboring node states and any relevant edge information.
  3. Aggregate incoming messages with an order-independent operation such as sum, mean or attention-weighted sum.
  4. Update node states and repeat for several layers, then predict node labels, links or a pooled graph output.

$$h_v^{(l+1)}=U\!\left(h_v^{(l)},\operatorname{AGG}_{u\in\mathcal{N}(v)}M(h_v^{(l)},h_u^{(l)},e_{uv})\right)$$

The knobs that matter

number of layers
more layers reach more hops but can make node states too similar
aggregation
sum, mean and attention preserve or weight different information
neighbor sampling
limits computation on high-degree nodes
hidden dimension
sets the size of node representations

What interviewers ask

  • What is message passing in a graph neural network?
  • Why should neighbor aggregation be invariant to neighbor order?
  • What is oversmoothing, and how would you reduce it?
  • How would you avoid data leakage in a link prediction task?

Common mistakes

  • Using held-out target edges during message passing can leak the answer in link prediction.
  • Adding more layers can blur node differences instead of improving predictions.
  • A mean aggregator loses neighbor-count information unless degree or related features are supplied.

When to use it

Use a GNN when relationships carry useful signal beyond individual features, such as molecular bonds or network connections. Compare against a feature-only baseline, especially when graph edges are noisy or unavailable at inference.

Implement it (4 problems)

Learn it

Interview questions about graph neural network