Skip to main content
QUICK REVIEW

[Paper Review] Don't Judge a Language Model by Its Last Layer: Contrastive Learning with Layer-Wise Attention Pooling

Dongsuk Oh, Yejin Kim|arXiv (Cornell University)|Sep 13, 2022
Topic ModelingComputer Science5 citations
TL;DR

This paper proposes layer-wise attention pooling to improve sentence representation learning in pre-trained language models (PLMs) by adaptively weighting features across all layers, rather than relying on fixed pooling like the last layer. Using contrastive learning with this attention mechanism, the method achieves state-of-the-art performance on semantic textual similarity and semantic search tasks, outperforming fixed pooling strategies even after removing the attention layer at inference.

ABSTRACT

Recent pre-trained language models (PLMs) achieved great success on many natural language processing tasks through learning linguistic features and contextualized sentence representation. Since attributes captured in stacked layers of PLMs are not clearly identified, straightforward approaches such as embedding the last layer are commonly preferred to derive sentence representations from PLMs. This paper introduces the attention-based pooling strategy, which enables the model to preserve layer-wise signals captured in each layer and learn digested linguistic features for downstream tasks. The contrastive learning objective can adapt the layer-wise attention pooling to both unsupervised and supervised manners. It results in regularizing the anisotropic space of pre-trained embeddings and being more uniform. We evaluate our model on standard semantic textual similarity (STS) and semantic search tasks. As a result, our method improved the performance of the base contrastive learned BERT_base and variants.

Motivation & Objective

  • To address the limitation of fixed pooling strategies (e.g., last layer or average pooling) in capturing diverse linguistic features across PLM layers.
  • To explore whether layer-wise attention pooling can better preserve and integrate task-relevant signals from multiple layers in PLMs.
  • To evaluate the effectiveness of the proposed pooling strategy under both supervised and unsupervised contrastive learning objectives.
  • To demonstrate that the method improves semantic search performance without increasing inference cost, even after detaching the attention layer.
  • To establish a task-agnostic, learnable pooling mechanism that outperforms existing InfoNCE-based contrastive learning methods for sentence embeddings.

Proposed method

  • Proposes a multiplicative attention mechanism that computes layer importance scores based on the compatibility between [CLS] token representations and average token representations across layers.
  • Uses learnable query, key, and value projections to compute attention weights αi for each layer, enabling dynamic weighting of layer-specific features.
  • Aggregates weighted representations across layers via a weighted sum (Equation 2), followed by a global average over the weighted representations (Equation 3).
  • Concatenates the last-layer [CLS] representation with the aggregated layer-wise representation and passes through a learnable MLP to produce the final sentence embedding (Equations 4–5).
  • Applies the pooling strategy within three contrastive learning schemes: basic supervised contrastive learning, self-supervised contrastive learning, and a hybrid approach.
  • Trains the model using InfoNCE loss to pull semantically similar sentence pairs closer and push dissimilar ones apart, regularizing the embedding space to be more uniform and less anisotropic.

Experimental results

Research questions

  • RQ1Can a learnable, layer-wise attention pooling mechanism outperform fixed pooling strategies like [CLS] or average pooling in sentence representation learning?
  • RQ2Does contrastive learning with layer-wise attention pooling lead to more uniform and less anisotropic sentence embeddings compared to standard methods?
  • RQ3Can the proposed pooling strategy improve performance on both semantic textual similarity (STS) and semantic search tasks without increasing inference cost?
  • RQ4Does the performance gain from layer-wise attention pooling persist when the attention mechanism is removed during inference?
  • RQ5How does the performance of the proposed method compare to existing state-of-the-art contrastive learning models for sentence embeddings?

Key findings

  • The proposed layer-wise attention pooling with contrastive learning achieved a Spearman’s correlation of 86.76 on the STS-B test set, outperforming all baseline fixed pooling strategies, including [CLS] Last + AVG Last concatenation (85.11).
  • On the Quora duplicate questions dataset, the model trained with layer-wise attention pooling achieved an MRR@10 of 66.32 with RoBERTa large, compared to 65.85 for the baseline, while maintaining identical memory usage and reduced retrieval time.
  • Even after removing the attention layer during inference, models trained with layer-wise attention pooling still outperformed baselines, demonstrating the effectiveness of the learned representation.
  • The ablation study showed that combining [CLS] and AVG attention across all layers (LayerAttPooler(CLS All + AVG All attention)) yielded the highest performance (86.57 on STS-B), with further improvement when concatenated with the last-layer [CLS] (86.76).
  • The method reduced embedding anisotropy and improved uniformity in the embedding space, as evidenced by the contrastive learning objective, leading to better generalization.
  • The model achieved better semantic search performance with the same parameters and lower latency, showing that the method enables more efficient and accurate retrieval.

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.