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.
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.
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