NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Betti numbers of attention graphs is all you really need

Laida Kushnareva, Dmitri Piontkovski, Irina PiontkovskayaarXiv 2022

Abstract

We apply methods of topological analysis to the attention graphs, calculated on the attention heads of the BERT model. Our research shows that the classifier built upon basic persistent topological features (namely, Betti numbers) of the trained neural network can achieve classification results on par with the conventional classification method. We show the relevance of such topological text representation on three text classification benchmarks. To the best of our knowledge, it is the first attempt to analyze the topology of an attention-based neural network, widely used for Natural Language Processing.

Introduction

Large Language Models like BERT are often treated as "black boxes" that produce high-dimensional embeddings. While effective, these embeddings are computationally expensive and difficult to interpret. Traditionally, to classify a sentence, a complex neural layer is attached to the model's output. This research investigates a fundamental question: Is the structural information hidden within the attention mechanism itself sufficient for classification? Specifically, we explore whether the topology of how tokens "pay attention" to one another—ignoring word identities and order—is enough to distinguish between linguistic categories, sentiment, or spam.

Methodology

Our approach bridges the gap between Natural Language Processing (NLP) and Topological Data Analysis (TDA) through a four-step pipeline:

  1. Graph Construction: For a given sentence, we extract attention weights from each of BERT’s 144 heads (12 layers × 12 heads). Each head is treated as a weighted directed graph where tokens are nodes and attention weights are edges.
  2. Filtration: To derive topological features, we apply a series of thresholds to the weighted graphs. An edge exists only if its weight exceeds the threshold, creating a sequence of binary graphs.
  3. Betti Numbers: For each binary graph, we calculate the first two Betti numbers: $\beta_0$ (the number of connected components) and $\beta_1$ (the number of independent cycles).
  4. Classification: These Betti numbers are fed into a simple Logistic Regression classifier. This classifier has no access to original word embeddings or token order—only the "shape" of the attention.

Experimental Setup

The approach was evaluated on three distinct text classification benchmarks:

  • CoLA (Corpus of Linguistic Acceptability): Determining grammatical correctness.
  • IMDB: Sentiment analysis of movie reviews (restricted to $\le$ 128 tokens).
  • SPAM: Detecting mobile phone spam messages.

Key Results

The topological classifier (using all 144 heads) was compared against a standard fine-tuned BERT model using the Matthew Correlation Coefficient (MCC) and Accuracy. As shown in the results table below, the topological approach yielded superior results in the CoLA and SPAM tasks.

Performance comparison of standard BERT versus topological classifiers using varying numbers of attention heads.

Even when pruning the model down to just the 12 "best" heads, the topological classifier remained highly competitive, proving that a tiny fraction of the model's structural data carries the bulk of the classification power.

Head Specialization and Analysis

A striking insight from this work is that topological features of a pre-trained, fine-tuned BERT model are incredibly rich. The success of Betti numbers suggests that "linguistic acceptability" or "sentiment" leaves a distinct topological footprint in the attention mechanism.

Furthermore, not all attention heads are equally relevant. Specific heads are much more "topologically important" for certain tasks than others. Visualizing the importance of these heads across layers reveals clear patterns of task-relevant structure.

Heatmaps visualizing the importance of specific attention heads across BERT's 12 layers for different datasets.

Conclusion and Future Work

This study demonstrates that the "shape" of attention contains sufficient information to classify text effectively. While the current work converted directed attention graphs into undirected ones, future research could utilize directed graph invariants for even richer information. Additionally, identifying "topologically important" heads could lead to new strategies for model compression, allowing for smaller, faster models that maintain high accuracy through structural awareness.

Summary updated

BibTeX
@misc{kushnareva2022betti,
  author = {Laida Kushnareva and Dmitri Piontkovski and Irina Piontkovskaya},
  title = {{Betti numbers of attention graphs is all you really need}},
  year = {2022},
  eprint = {2207.01903},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2207.01903},
}
Figure