Not All Tokens Are Equal: Per-Dimension Top-K Pooling for Adversarially Robust BERT Classification
Summary
A new pooling strategy, Per-Dimension Top-K Average Pooling, enhances the adversarial robustness of BERT-based contextual text classification. Traditional BERT models, which rely on the [CLS] token for downstream prediction, are vulnerable to adversarial perturbations because their single-vector representation is indiscriminately influenced by injected tokens. This proposed method addresses this by selectively aggregating only the top-K token activations for each hidden dimension, effectively filtering out less relevant tokens and suppressing adversarial injections. Evaluated across spam detection (Enron, LingSpam), automated essay scoring (ASAP), and hate speech classification, the approach significantly improves resilience. On the Enron spam dataset under adversarial attack, the best Hybrid (K=3) variant reduced the Attack Success Rate from 70.65% to 37.07%, while maintaining over 99% clean accuracy. In contrast, the [CLS] token approach degraded to 63.64% adversarial accuracy. Representation-level analyses confirm that Top-K pooling variants exhibit lower cosine similarity shift under attack, with adversarial tokens entering top-K selection in fewer dimensions. This lightweight mechanism offers a principled way to improve BERT classifier robustness without architectural changes.
Key takeaway
For machine learning engineers developing BERT-based text classifiers, especially in security-sensitive applications like spam or hate speech detection, you should consider replacing standard [CLS] token pooling with Per-Dimension Top-K Average Pooling. This lightweight strategy significantly improves adversarial robustness, reducing attack success rates from 70.65% to 37.07% on datasets like Enron spam, while maintaining high clean accuracy. Implementing this method offers a principled defense against adversarial injections without requiring complex architectural changes to your existing BERT models.
Key insights
Per-dimension top-K pooling enhances BERT's adversarial robustness by selectively aggregating token activations.
Principles
- Selective token aggregation filters adversarial injections.
- Robustness is achievable without architectural changes.
- "[CLS]"-based pooling is vulnerable to perturbations.
Method
Aggregate only the top-K token activations per hidden dimension, suppressing non-top-K tokens to filter adversarial input.
In practice
- Apply Per-Dimension Top-K Average Pooling to BERT.
- Consider K=3 for Hybrid pooling variants.
- Monitor cosine similarity shift for attack resilience.
Topics
- BERT
- Adversarial Robustness
- Text Classification
- Top-K Pooling
- [CLS] Token
- NLP Security
Best for: Research Scientist, AI Scientist, Machine Learning Engineer, NLP Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Paper Index on ACL Anthology.