0Pricing
Learn AI with Python · Lesson

Graph Convolutional Networks (GCN)

Message passing framework, GCNConv, node feature aggregation, PyTorch Geometric setup.

Graph Convolutional Networks (GCN) is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From CNNs to GCNs

CNNs exploit grid structure in images. A Graph Convolutional Network (GCN) generalizes convolution to irregular graphs, letting each node update its representation using information from its neighbors. This handles data with no fixed grid.

Message Passing

GCNs follow the message passing framework: at each layer every node (1) gathers messages from its neighbors, (2) aggregates them, and (3) updates its own embedding. Stacking layers lets information flow across the graph.

Neighbor Aggregation

The core operation is aggregating neighbor features. A simple GCN layer averages the feature vectors of a node neighbors (plus itself), then applies a learned linear transform and nonlinearity.

# For each node v:
#   h_v = activation( W * mean(features of v and its neighbors) )

Why Normalize the Adjacency

High-degree nodes would dominate if we summed neighbors naively. GCN uses the normalized adjacency to scale contributions, keeping the magnitude of node embeddings stable across nodes of different degree.

The Normalization Formula

The symmetric normalization adds self-loops then scales by node degrees, written D^(-1/2) (A + I) D^(-1/2). Adding the identity I lets a node keep its own features during aggregation.

# A_hat = A + I  (add self-loops)
# D_hat = degree matrix of A_hat
# A_norm = D_hat^(-1/2) * A_hat * D_hat^(-1/2)

PyTorch Geometric

PyTorch Geometric (PyG) is the standard library for GNNs in PyTorch. It provides ready-made layers like GCNConv that implement the normalized message-passing convolution for you.

import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv

A Single GCNConv Layer

GCNConv(in_channels, out_channels) creates one graph convolution. It takes the node feature matrix x and the edge_index (the graph connectivity in COO format) and returns updated node embeddings.

conv = GCNConv(in_channels=16, out_channels=32)
# x: [num_nodes, 16], edge_index: [2, num_edges]
h = conv(x, edge_index)  # -> [num_nodes, 32]

A 2-Layer GCN

A typical GCN stacks two layers: the first projects features and aggregates one hop of neighbors, the second aggregates a second hop, giving each node a 2-hop receptive field.

class GCN(torch.nn.Module):
    def __init__(self, in_dim, hid, out):
        super().__init__()
        self.conv1 = GCNConv(in_dim, hid)
        self.conv2 = GCNConv(hid, out)

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

Receptive Field Grows with Depth

Each GCN layer expands a node receptive field by one hop. Two layers see 2-hop neighborhoods, three layers see 3-hop, and so on. Depth lets distant information reach a node.

Over-Smoothing

But stacking too many layers causes over-smoothing: repeated neighbor averaging makes all node embeddings converge to nearly the same vector, destroying the distinctions needed for classification. This is why most GCNs use only 2 or 3 layers.

Mitigating Over-Smoothing

Techniques to fight over-smoothing include residual/skip connections, jumping-knowledge networks that combine layer outputs, and simply keeping the network shallow. Shallow GCNs often outperform deep ones on standard benchmarks.

Quick Check

Test your GCN knowledge.

Recap

You learned Graph Convolutional Networks:

  • GCNs follow the message passing framework: aggregate neighbor features then update
  • The normalized adjacency with self-loops keeps embeddings stable
  • GCNConv in PyTorch Geometric implements one convolution
  • A common architecture is a 2-layer GCN
  • Too much depth causes over-smoothing

Frequently asked questions

Is the “Graph Convolutional Networks (GCN)” lesson free?

Yes — the full text of “Graph Convolutional Networks (GCN)” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Graph Convolutional Networks (GCN)”?

Message passing framework, GCNConv, node feature aggregation, PyTorch Geometric setup. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Graph Convolutional Networks (GCN)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Graph Theory for Machine Learning
  2. Graph Convolutional Networks (GCN)
  3. Node Classification with GNN
  4. Link Prediction and Graph Classification
← Back to Learn AI with Python