Harnessing Attention Mechanisms: Efficient Sequence Reduction using Attention-based Autoencoders

Daniel Biermann, Fabrizio Palumbo, Morten Goodwin, Ole-Christoffer GranmoarXiv 2023 · DOI

Introduction

Transformer-based architectures have revolutionized Natural Language Processing (NLP), yet they often treat sequence length as a static constraint. In tasks requiring sequence compression—such as document classification or generating compact latent representations—standard approaches typically rely on simple heuristics like selecting the [CLS] token or applying global average pooling. This paper argues that such methods lack the nuance required for optimal information retention. We propose a novel mechanism to directly tune sequence length by integrating a learnable scaling matrix into the attention mechanism, allowing for deliberate and efficient sequence reduction.

Methodology

The primary innovation lies in modifying the standard Scaled Dot-Product Attention. In conventional attention layers, the output dimensionality is determined by the number of Query vectors, which typically matches the input sequence length ($n$). We introduce a trainable scaling matrix $W^S \in \mathbb{R}^{n_q \times n}$, where $n_q$ represents the target sequence length.

The Query matrix ($Q$) is redefined as: $$Q = W^S \times (X \times W^Q)$$

By setting $n_q < n$, the attention mechanism inherently produces a reduced sequence. To evaluate this approach, we implemented an Attention-based Autoencoder consisting of a Reducing Attention Encoder and a corresponding Decoder. The encoder compresses an input of $N$ tokens into a latent space of $N-k$ tokens, while the decoder attempts to reconstruct the original $N$ tokens from this compressed state.

Architecture of the Attention-based Autoencoder for sequence reduction.

Experimental Setup

The model was trained on a 30% subset of the English Wikipedia dataset (20220301.en dump), comprising approximately 1.9 million samples. Text was tokenized using a pretrained BertTokenizer. We employed an 80/20 train-test split and evaluated various input lengths ($N$) to observe the impact of sequence scale on compression efficiency.

Results and Discussion

Our experiments reveal that sequence length is a viable and effective axis for model optimization. A key finding is the significant redundancy present in longer natural language sequences.

Reconstruction accuracy as a function of the percentage of input tokens retained in the latent space.

As shown in the results, the model achieves near-perfect reconstruction accuracy when compressing 512-token sequences by 50% (to 256 tokens). Even at a 4x reduction (75% compression), accuracy remains above 90%. However, a performance degradation threshold is observed when the latent representation falls below 20-25% of the original input length.

Furthermore, the data suggests that shorter sequences are more difficult to compress. In a 32-token sequence, each token represents a larger fraction of the total information compared to a 512-token sequence. Consequently, the "information cost" of reduction is higher for shorter contexts. We also noted that smaller sequences were more susceptible to local minima during training, requiring higher initial learning rates and linear decay for stable convergence.

Conclusion and Future Work

This study demonstrates that learnable attention-based reduction can effectively compress sequences while preserving high reconstruction accuracy. While promising, the current approach is limited by static input shapes requiring padding. Future research should explore multi-step hierarchical reductions and evaluate the utility of these compressed latent spaces in downstream tasks such as summarization or sentiment analysis. Additionally, refining the initialization of the scaling matrix $W^S$ remains a priority for improving model robustness.

Summary updated

Abstract

Many machine learning models use the manipulation of dimensions as a driving force to enable models to identify and learn important features in data. In the case of sequential data this manipulation usually happens on the token dimension level. Despite the fact that many tasks require a change in sequence length itself, the step of sequence length reduction usually happens out of necessity and in a single step. As far as we are aware, no model uses the sequence length reduction step as an additional opportunity to tune the models performance. In fact, sequence length manipulation as a whole seems to be an overlooked direction. In this study we introduce a novel attention-based method that allows for the direct manipulation of sequence lengths. To explore the method's capabilities, we employ it in an autoencoder model. The autoencoder reduces the input sequence to a smaller sequence in latent space. It then aims to reproduce the original sequence from this reduced form. In this setting, we explore the methods reduction performance for different input and latent sequence lengths. We are able to show that the autoencoder retains all the significant information when reducing the original sequence to half its original size. When reducing down to as low as a quarter of its original size, the autoencoder is still able to reproduce the original sequence with an accuracy of around 90%.
BibTeX
@misc{biermann2023harnessing,
  author = {Daniel Biermann and Fabrizio Palumbo and Morten Goodwin and Ole-Christoffer Granmo},
  title = {{Harnessing Attention Mechanisms: Efficient Sequence Reduction using Attention-based Autoencoders}},
  year = {2023},
  eprint = {2310.14837},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.14837},
}
Figure