Code Architecture — Semi-Supervised Classification with Graph Convolutional Networks
Overview
The notebook builds a two-layer GCN in NumPy with a hand-written backward pass and Adam, and trains it on Zachary’s karate club graph with 3 labelled nodes per class. It runs on CPU in seconds. The first version of this notebook failed (every node got the same class); Section 5 explains the bug and checks the fixed gradient numerically.
Section-by-Section Breakdown
1. Setup & Imports (cell 2)
- Imports
numpy,networkx,matplotliband scikit-learn’sTSNE. - Saves figures as JPEG at dpi 80, registers the
atexithook that saves the executed notebook on Kaggle, and seeds NumPy withSEED = 42.
2. Graph Construction (cells 4–5)
nx.karate_club_graph(): 34 nodes, 78 edges in networkx.A = nx.to_numpy_array(G, weight=None)gives an unweighted 0/1 adjacency matrix, as in the paper’s karate club example (Appendix A.1).- Labels: 0 for the
Mr. Hifaction and 1 forOfficer(17 nodes each). The graph is drawn coloured by faction.
3. Normalised Adjacency (cell 7)
normalized_adjacency(A)returns  = D̃^(−1/2) (A + I) D̃^(−1/2) (Section 2.2, Eq. 8) and prints its first diagonal entries, row sums and a symmetry check.
4. GCN Layer (cell 9)
gcn_forward(X, W0, W1, A_hat): H1 = ReLU(Â X W0), Z = softmax(Â H1 W1) (Section 3.1, Eq. 9).init_weightsuses Glorot uniform initialisation (Section 5.2).
5. Semi-Supervised Training (cells 10–13)
- Inputs (cell 11): X = I (34 × 34 one-hot features), 16 hidden units, 2 classes. W0 is 34 × 16 and W1 is 16 × 2. The first 3 nodes of each class are labelled: [0, 1, 2, 9, 14, 15].
- Loss and gradients (cell 12):
cross_entropy_lossaverages over labelled nodes only.gcn_backwardmasks the output gradient,dZ2 = (Z − Y) * mask / n_labelled, and adds the L2 gradient5e-4 * W0on the first layer. AnAdamclass (learning rate 0.01) updates W0 and W1. A finite-difference check compares the analytic and numerical gradients; the saved run prints a maximum difference of 9.96e-11. - Training (cell 13): 200 epochs of full-batch Adam. Prints loss and accuracy every 50 epochs, then accuracy on labelled, all and unlabelled nodes and the misclassified node ids. Saved run: 1.0 / 0.8529 / 0.8214, misclassified [8, 24, 25, 28, 31].
6. Evaluation & Visualisation (cells 15–16)
- Training loss and accuracy curves.
- The graph drawn twice: true factions, and predictions with wrong nodes in orange (29 correct, 5 wrong).
7. Learned Embeddings (cell 18)
- t-SNE of the 16-dimensional hidden layer H1, coloured by true faction and by prediction.
8. Varying the Number of Labels (cell 20)
- For 1, 2, 3, 5, 8 and 10 labels per class (the first n nodes of each class), retrains from the same initial weights for 200 epochs and prints accuracy on all and unlabelled nodes. Saved unlabelled accuracy: 0.9375, 0.9333, 0.8214, 0.8333, 0.9444, 0.9286.
9. Ablation: Propagation Matrix (cell 22)
train_gcn(A_prop)trains the same model (same seed, 3 labels per class) with a different matrix in both layers and returns accuracy on the 28 unlabelled nodes.- Saved run: symmetric normalisation with self-loops 0.8214, no self-loops 0.8214, row normalisation 0.8214, no normalisation (A + I) 0.9286, no graph (identity) 0.5000.
10. Summary (cell 23)
- States what the saved run shows: the earlier failure and its cause, the fixed results, that removing the graph drops accuracy to 0.50, that this run does not show the paper’s normalisation is better, and how the setup differs from the paper.
Differences from the Paper
| Item | Paper | Notebook |
|---|---|---|
| Data | Citeseer, Cora, Pubmed, NELL (Section 5.1); karate club with 4 modularity classes (Appendix A.1) | Karate club with 2 faction labels |
| Labels | 20 per class (citation data); 1 per class (karate club, Appendix A.2) | 3 per class |
| Optimiser | Adam, lr 0.01 (Section 5.2) | Adam, lr 0.01 |
| Epochs | up to 200 with early stopping, window 10 (Section 5.2); 300 iterations for the karate club (Appendix A.2) | 200, no early stopping |
| Regularisation | dropout 0.5, L2 5 · 10⁻⁴ on the first layer (Section 5.2) | L2 5 · 10⁻⁴ on the first layer, no dropout |
| Hidden units | 16 (Section 5.2) | 16 |
| Implementation | TensorFlow, sparse matrices (Section 3.2) | NumPy, dense matrices, manual gradients |