Skip to content

Label Propagation on Cora

Author: K3-Node Team
Backend: Multi-Backend
Dataset: Cora (Planetoid)
Description: Classify papers in the Cora citation network by topic without training any model.

View in Colab   GitHub source


Label Propagation on Cora

Classify papers in the Cora citation network by topic without training any model. Label propagation (Zhu & Ghahramani, 2002) starts from the 140 known labels and repeatedly lets every paper take the average label of its neighbors, since linked papers tend to share a topic.

Same model as PyG's examples/label_prop.py; on Cora instead of ogbn-arxiv to keep the dataset small.

Install K3-Node, then choose a backend: "tensorflow", "torch" or "jax"

!pip install k3-node[examples]
import os
os.environ["KERAS_BACKEND"] = "tensorflow"

Load the data

data holds one graph: node features x, edges edge_index, labels y, and masks marking the training, validation and test nodes.

import keras
from keras import ops
from k3_node.datasets import Planetoid
from k3_node.loader import FullGraphDataset
from k3_node.models import LabelPropagation

dataset = Planetoid("data/Planetoid", name="Cora")
data = dataset[0]
print(data)

Propagate the labels

model = LabelPropagation(num_layers=3, alpha=0.9)
out = model(data.y, data.edge_index, mask=data.train_mask)  # scores for every paper and topic
print(f"Validation accuracy: {data.accuracy(out, mask='val_mask'):.4f}")
print(f"Test accuracy: {data.accuracy(out, mask='test_mask'):.4f}")