A multiclass text classification solution on the DBpedia-14 Ontology Classification Dataset using two advanced Graph Neural Network (GNN) architectures:
- GCN (Graph Convolutional Network) — Transductive setting
- GraphSAGE (Graph Sample and Aggregated Embeddings) — Inductive setting
- Dataset
- Data Shapes and Sizes
- Preprocessing
- Graph Construction
- Model Architectures
- Training
- Results
- Repository Structure
- Requirements
DBpedia-14 Ontology Classification Dataset
The DBpedia dataset is derived from Wikipedia articles and organized into 14 non-overlapping ontology classes. Each sample consists of a title and an abstract (content) extracted from a Wikipedia article.
| # | Class Label |
|---|---|
| 0 | Company |
| 1 | EducationalInstitution |
| 2 | Artist |
| 3 | Athlete |
| 4 | OfficeHolder |
| 5 | MeanOfTransportation |
| 6 | Building |
| 7 | NaturalPlace |
| 8 | Village |
| 9 | Animal |
| 10 | Plant |
| 11 | Album |
| 12 | Film |
| 13 | WrittenWork |
- Total Classes: 14
- Source: DBpedia / Hugging Face
datasetslibrary (dbpedia_14) - Text Fields Used:
title+content(concatenated)
| Split | Samples per Class | Total Samples |
|---|---|---|
| Train | 2,000 | 28,000 |
| Test | 1,000 | 14,000 |
| Total Nodes | — | 42,000 |
- TF-IDF Feature Matrix:
(42,000 nodes, 5,000 features) - Node Feature Dimension: 5,000
- Graph Edges (k-NN, K=10): ~438,000 edges
- Graph Type: Homogeneous k-NN cosine similarity graph
| Split | Samples per Class | Total Samples |
|---|---|---|
| Train | 2,000 | 28,000 |
| Test | 1,000 | 14,000 |
| Total Document Nodes | — | 42,000 |
- Vocabulary (Word Nodes): ~5,000 word nodes
- Total Graph Nodes: ~47,000 (42,000 docs + 5,000 words)
- Total Graph Edges: ~1,670,000
- Doc→Word edges: TF-IDF weighted
- Word→Word edges: PMI (Pointwise Mutual Information) weighted
- Node Feature Dimension: 5,000 (TF-IDF)
Both pipelines share the following preprocessing steps:
- Dataset loaded via Hugging Face
datasets(dbpedia_14) - Stratified sampling applied to ensure class balance:
- 2,000 samples/class for training
- 1,000 samples/class for testing
- Concatenate
titleandcontentfields - Lowercase all text
- Remove punctuation and special characters
- Tokenize using NLTK
- Remove English stopwords (NLTK stopwords corpus)
- Retain only alphabetic tokens
- Applied
TfidfVectorizerwithmax_features=5000 - Produces a sparse feature matrix of shape
(N_docs, 5000) - Used as node feature matrix for GNN input
- Algorithm: k-Nearest Neighbors (k-NN) on TF-IDF cosine similarity
- K: 10 neighbors per node
- Edges: Undirected cosine similarity edges
- Total Edges: ~438,000
- Node Count: 42,000 (train + test combined into one graph)
- All nodes are present during training (transductive)
- Type: Heterogeneous bipartite-style text graph
- Node Types:
- Document nodes: 42,000
- Word nodes: ~5,000 (top TF-IDF vocabulary terms)
- Edge Types:
- Doc → Word: weighted by TF-IDF score
- Word → Word: weighted by Pointwise Mutual Information (PMI) over a sliding window
- Total Edges: ~1,670,000
- Test nodes are unseen during training (inductive)
Input: Node features (N, 5000)
└─> GCNConv Layer 1: 5000 → 256, ReLU, Dropout(0.5)
└─> GCNConv Layer 2: 256 → 14 (num_classes)
└─> Log Softmax → Class Prediction
- Framework: PyTorch Geometric
- Optimizer: Adam
- Loss: Negative Log-Likelihood (NLL) / CrossEntropy
- Dropout: 0.5
Input: Node features (N, 5000)
└─> SAGEConv Layer 1: 5000 → 256, ReLU, Dropout(0.5)
└─> SAGEConv Layer 2: 256 → 14 (num_classes)
└─> Log Softmax → Class Prediction
- Framework: PyTorch Geometric
- Sampling:
NeighborLoaderfor mini-batch neighbor sampling - Aggregation: Mean aggregation (GraphSAGE default)
- Optimizer: Adam
- Loss: CrossEntropy
- Dropout: 0.5
- Mode: Full-batch (entire graph in memory)
- Train Mask: 28,000 nodes
- Val/Test Mask: 14,000 nodes
- Epochs: 30
- Hardware: GPU (CUDA if available, else CPU)
- Mode: Mini-batch with
NeighborLoader - Batch Size: Configurable per run
- Neighbor Sampling: 2-hop neighborhood
- Train Nodes: 28,000 document nodes
- Test Nodes: 14,000 unseen document nodes
- Epochs: 30
- Hardware: GPU (CUDA if available, else CPU)
| Metric | Value |
|---|---|
| Best Validation Accuracy | 94.93% |
| Best Epoch | 26 / 30 |
| Node Features | TF-IDF (5000-dim) |
| Graph Edges | ~438,000 (k-NN, K=10) |
| Metric | Value |
|---|---|
| Setting | Inductive (unseen test nodes) |
| Node Features | TF-IDF (5000-dim) |
| Graph Edges | ~1,670,000 (TF-IDF + PMI) |
| Architecture | 2-layer SAGEConv (256 hidden) |
Both models demonstrate strong classification performance on the 14-class DBpedia ontology dataset, showcasing the effectiveness of graph-based text representations over traditional flat classifiers.
DbPedia-Enitity-Classification-using-GCNs-and-GraphSage/
├── gcn/
│ └── transductive.ipynb # GCN transductive classification pipeline
├── GraphSage/
│ └── inductive.ipynb # GraphSAGE inductive classification pipeline
├── README.md
└── LICENSE
torch
torch-geometric
transformers
datasets
scikit-learn
nltk
numpy
pandas
matplotlib
Install PyTorch Geometric following the official guide based on your CUDA version.
MIT License — see LICENSE for details.