---
title: "Graph Classification with Transformers"
description: "Graph classification with Hugging Face Transformers uses Microsoft's Graphormer, fine-tuned on the ogbg-molhiv dataset from the Open Graph Benchmark."
url: "https://swe.quest/articles/graph-classification-with-transformers-f7f50e8dbcc7e952"
canonical: "https://huggingface.co/blog/graphml-classification"
source: "Hugging Face"
published: "2023-04-14"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["machine-learning","model-training","python"]
---

# Graph Classification with Transformers

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-04-14, 6 min read, How-to. Original: [https://huggingface.co/blog/graphml-classification](https://huggingface.co/blog/graphml-classification)

## Summary

Graph classification with Hugging Face Transformers uses Microsoft's Graphormer, fine-tuned on the ogbg-molhiv dataset from the Open Graph Benchmark. The walkthrough covers loading graph datasets from the Hub, the expected jsonl graph format (edge\_index, num\_nodes, y, node\_feat, edge\_attr), and Graphormer preprocessing that computes degree and shortest-path features, either upfront or on the fly in the data collator for large graphs. It then loads a pretrained checkpoint with a new classification head and trains it with Trainer and GraphormerDataCollator. Batch size and gradient accumulation need tuning to avoid OOMs, and 20 epochs take about a day on a CPU. A reader comment notes the module has since moved under transformers.models.deprecated.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Python ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- Hugging Face Transformers basics
- graph data structures ([easy posts on Data structures](https://swe.quest/tags/data-structures.md?difficulty=easy))

## Keywords

Graphormer, Hugging Face Transformers, Hugging Face Datasets, Open Graph Benchmark, ogbg-molhiv, NetworkX

## Related posts

- [Introduction to Graph Machine Learning](https://swe.quest/articles/introduction-to-graph-machine-learning-67c6459102a9edc7.md): Hugging Face · 2023-01-03 · Easy · 13 min read · Machine learning, Algorithms
  Graphs (nodes linked by edges) underpin tasks from molecule generation to recommendation, and learning on them requires permutation-invariant/equivariant models because shuffling an adjacency matrix or edge list yields the same graph.
- [How to train a Language Model with Megatron-LM](https://swe.quest/articles/how-to-train-a-language-model-with-megatron-lm-30e46ab3423562c4.md): Hugging Face · 2022-09-07 · Hard · 7 min read · Model training, LLMs, Python
  Megatron-LM, NVIDIA's GPU-optimized framework for pre-training transformers, can train a GPT2-style model end to end: environment setup in an NGC PyTorch container, preprocessing data into tokenized binary mmap files, launching distributed training, and converting checkpoints to Hugging Face Transformers.
- [Introducing TabFM: A zero-shot foundation model for tabular data](https://swe.quest/articles/introducing-tabfm-a-zero-shot-foundation-model-for-tabular-data-3b60f0bd6faee92a.md): Google Research · 2026-06-30 · Easy · 5 min read · Machine learning, Model training, Analytics & OLAP
  TabFM is a foundation model that treats tabular classification and regression as in-context learning, so it predicts on an unseen table in a single forward pass with no training, hyperparameter tuning or feature engineering.
- [Perceiver IO: a scalable, fully-attentional model that works on any modality](https://swe.quest/articles/perceiver-io-a-scalable-fully-attentional-model-that-works-on-any-modality-f8a43e7ae2c87797.md): Hugging Face · 2021-12-15 · Hard · 21 min read · Machine learning, LLMs, Computer vision, Python
  Perceiver IO avoids the quadratic cost of self-attention by running attention over a small fixed set of latent variables (e.g. 256×1280).
- [Introducing ModuleLens: Zooming into PyTorch Modules for Human and Agentic Engineering](https://swe.quest/articles/introducing-modulelens-zooming-into-pytorch-modules-for-human-and-agentic-8767bb31b4979135.md): Pinterest Engineering · 2026-10-19 · Hard · 5 min read · ML platforms, Python, Developer productivity, AI agents, Recommendations & ranking
  ModuleLens lets Pinterest ML engineers capture any PyTorch module from a sharded multi-GPU recommendation model and rebuild it locally within seconds, replacing full-model initialization with a "module-first" workflow.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
