Skip to main content
QUICK REVIEW

[Paper Review] The Topological BERT: Transforming Attention into Topology for Natural Language Processing

Ilan Perez, Raphael Reinauer|arXiv (Cornell University)|Jun 30, 2022
Topological and Geometric Data AnalysisComputer Science11 citations
TL;DR

This paper introduces the Topological BERT, a novel text classifier that transforms BERT's attention maps into attention graphs and applies topological data analysis—specifically persistent homology and persistence images—to extract structural features for classification. It achieves performance comparable to BERT on NLP tasks like sentiment analysis and grammaticality judgment, while enabling aggressive pruning of attention heads (down to 10) without performance loss, and demonstrates superior robustness against adversarial attacks.

ABSTRACT

In recent years, the introduction of the Transformer models sparked a revolution in natural language processing (NLP). BERT was one of the first text encoders using only the attention mechanism without any recurrent parts to achieve state-of-the-art results on many NLP tasks. This paper introduces a text classifier using topological data analysis. We use BERT's attention maps transformed into attention graphs as the only input to that classifier. The model can solve tasks such as distinguishing spam from ham messages, recognizing whether a sentence is grammatically correct, or evaluating a movie review as negative or positive. It performs comparably to the BERT baseline and outperforms it on some tasks. Additionally, we propose a new method to reduce the number of BERT's attention heads considered by the topological classifier, which allows us to prune the number of heads from 144 down to as few as ten with no reduction in performance. Our work also shows that the topological model displays higher robustness against adversarial attacks than the original BERT model, which is maintained during the pruning process. To the best of our knowledge, this work is the first to confront topological-based models with adversarial attacks in the context of NLP.

Motivation & Objective

  • To develop a topological text classifier that leverages BERT’s attention mechanisms without relying on fine-tuned BERT parameters.
  • To investigate whether topological representations of attention graphs can match or exceed BERT’s performance on standard NLP benchmarks.
  • To design a novel pruning method for attention heads based on topological interpretability, reducing model complexity without performance degradation.
  • To evaluate the robustness of topological models against adversarial attacks, a previously unexplored direction in topological NLP.
  • To explore the interpretability of attention heads through topological stability and clustering in UMAP projections.

Proposed method

  • Transform BERT’s multi-head attention maps into weighted, directed attention graphs per layer and sequence.
  • Apply persistent homology to compute topological features such as Betti numbers and persistence images across homology dimensions.
  • Use persistence images as input features to a fully connected neural network for downstream classification.
  • Propose a GradCam-inspired method to score attention heads based on their contribution to topological features, enabling pruning.
  • Apply data augmentation and standardization techniques to persistence images, including rotation, padding, and frame-specific normalization.
  • Train the topological classifier end-to-end on task-specific datasets, using early stopping and learning rate scheduling.

Experimental results

Research questions

  • RQ1Can topological features extracted from BERT’s attention graphs achieve competitive performance on standard NLP benchmarks compared to BERT?
  • RQ2Does pruning attention heads based on topological relevance preserve or even improve classification accuracy and robustness?
  • RQ3How do different filtration types (ordinary, multi-dimensional, directed) and symmetry functions affect topological model performance?
  • RQ4Is there a correlation between UMAP projections of persistence diagrams and attention map-based clustering patterns in BERT?
  • RQ5Can topological models maintain robustness against adversarial attacks, and does pruning affect this robustness?

Key findings

  • The topological model achieves comparable performance to BERT on all evaluated tasks, with accuracy reaching 99.8% on the IMDB dataset and 89.0% on the SST-2 dataset.
  • Pruning attention heads from 144 to 10 using the proposed topological scoring method maintains performance, with only minor drops in accuracy (e.g., 0.554/81.9 on CoLA with 3 heads vs. 0.518/80.6 on BERT).
  • The model outperforms BERT on the CoLA dataset when using 20 fine-tuning epochs, achieving 0.591/83.3 accuracy with 1 head, compared to BERT’s 0.518/80.6.
  • The topological model shows significantly higher robustness against adversarial attacks than BERT, and this robustness is preserved even after pruning to 10 heads.
  • Persistence images computed from ordinary and directed filtrations, especially with pruning, outperform more complex filtrations like MultiDim, achieving peak accuracy of 82.4% on CoLA with 30 heads.
  • The selected attention heads after pruning predominantly focus on the [SEP] token, offering new insights into the 'no-op' hypothesis and suggesting functional specialization in attention heads.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.