[Paper Review] KNN-BERT: Fine-Tuning Pre-Trained Models with KNN Classifier
This paper proposes KNN-BERT, a method that enhances pre-trained BERT fine-tuning by replacing the standard linear classifier with a K-Nearest Neighbors (KNN) classifier, while training robust, contrastively learned representations using class-wise positives and a momentum contrastive learning framework. The approach achieves significant gains in accuracy and robustness, especially in few-shot and adversarial settings.
Pre-trained models are widely used in fine-tuning downstream tasks with linear classifiers optimized by the cross-entropy loss, which might face robustness and stability problems. These problems can be improved by learning representations that focus on similarities in the same class and contradictions in different classes when making predictions. In this paper, we utilize the K-Nearest Neighbors Classifier in pre-trained model fine-tuning. For this KNN classifier, we introduce a supervised momentum contrastive learning framework to learn the clustered representations of the supervised downstream tasks. Extensive experiments on text classification tasks and robustness tests show that by incorporating KNNs with the traditional fine-tuning process, we can obtain significant improvements on the clean accuracy in both rich-source and few-shot settings and can improve the robustness against adversarial attacks. \footnote{all codes is available at https://github.com/LinyangLee/KNN-BERT}
Motivation & Objective
- To address the instability and poor generalization of standard cross-entropy fine-tuning in pre-trained models.
- To improve model robustness against adversarial attacks and noisy labels in low-resource settings.
- To leverage the KNN classifier’s similarity-based decision-making for better representation generalization.
- To develop a contrastive learning framework using class-wise positives to form tighter, more separable clusters.
- To combine KNN with linear classifiers via a weighted fusion strategy for improved inference performance.
Proposed method
- Replaces the standard linear classifier in BERT fine-tuning with a K-Nearest Neighbors (KNN) classifier that predicts based on similarity to training samples.
- Introduces a supervised momentum contrastive learning framework using class-wise positives (same-class samples) instead of data augmentations.
- Employs a momentum contrastive mechanism (MoCo-style) to maintain a queue of negative representations and update them via momentum, enabling large-scale negative mining.
- Uses a dual sampling strategy: selects both the most similar and least similar positives within each class to tighten intra-class clusters and widen inter-class margins.
- Fuses KNN predictions with linear classifier outputs using a learnable ratio φ during inference to balance robustness and accuracy.
- Trains representations using contrastive loss that minimizes intra-class distance and maximizes inter-class distance via the selected positive pairs.
Experimental results
Research questions
- RQ1Can replacing the linear classifier with a KNN classifier improve generalization and robustness in BERT fine-tuning?
- RQ2Does using class-wise positives in contrastive learning lead to better clustering of representations than augmentation-based positives?
- RQ3How does the combination of KNN and linear classifier via weighted fusion affect performance and robustness?
- RQ4What is the impact of selecting both most and least similar positives on representation quality and model performance?
- RQ5Can the proposed method significantly improve accuracy and defense against strong adversarial attacks in low-resource settings?
Key findings
- KNN-BERT achieves a 94.3% test accuracy on the RTE task in the rich-source setting, outperforming standard BERT fine-tuning.
- On the few-shot IMDB dataset, the method improves accuracy by over 10% compared to standard fine-tuning, demonstrating strong few-shot generalization.
- The model maintains 42.7% accuracy after Textfooler and Bert-Attack adversarial attacks, significantly outperforming standard BERT and linear classifiers.
- Ablation studies show that using both most and least similar positives improves clustering and performance, especially on diverse tasks like RTE.
- The optimal K in KNN is robust across settings, with large K not being necessary, reducing computational cost.
- The fusion ratio φ = 1.0 (pure KNN) performs best, but combining KNN with linear classifier (φ = 0.5) yields further gains, indicating complementary strengths.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.