TransfoRNN: Capturing the Sequential Information in Self-Attention Representations for Language Modeling

Tze Yuang Chong, Xuyang Wang, Lin Yang, Junjie Wang · DOI

Introduction

The landscape of Natural Language Processing (NLP) has been transformed by the rise of the Transformer architecture. By leveraging self-attention, Transformers excel at capturing long-range dependencies. However, this mechanism is inherently permutation-invariant, meaning it treats input sequences as unordered sets unless explicitly modified. While positional encodings are typically used to inject sequence information, this paper argues that such methods are implicit workarounds. We propose TransfoRNN, a hybrid model that cascades Recurrent Neural Networks (RNNs) onto a Transformer stack to explicitly capture sequential order.

The Sequential Challenge in Self-Attention

In standard Transformers, the model often relies heavily on the representation of the immediate preceding token for next-word prediction. This can overlook the nuanced, step-by-step syntactic structures that RNNs were designed to handle. The core challenge is combining the global contextual reach of Transformers with the local, temporal precision of RNNs.

Methodology: The TransfoRNN Architecture

TransfoRNN employs a cascaded approach to sequence modeling. The architecture consists of three primary stages:

  1. Input Layer: Standard input embeddings combined with optional positional encodings.
  2. Transformer Stack: The sequence passes through $N$ layers of multi-head self-attention and feed-forward networks to extract high-level contextual features.
  3. RNN Stack: The entire sequence of representations from the Transformer is then fed into $M$ layers of Long Short-Term Memory (LSTM) units.
The TransfoRNN architecture: a cascade of Transformer layers followed by RNN layers.

By placing the RNN after the Transformer, the model uses self-attention for global feature extraction and the RNN to organize those features into a coherent temporal sequence for final prediction.

Experimental Setup

The model was evaluated on three benchmarks:

  • Penn Treebank (PTB): A dataset of 0.9M words focused on syntax and grammar.
  • WikiText-2: A mid-sized dataset (2.0M words) of Wikipedia articles.
  • LibriSpeech: A large-scale speech recognition task (960 hours) used for N-best re-ranking.

Results and Analysis

TransfoRNN demonstrates significant improvements in both perplexity (PPL) and parameter efficiency.

Performance comparison on PTB and WikiText-2 datasets.

Efficiency and Performance

One of the most striking findings is that a shallow TransfoRNN (e.g., $N=2$) can outperform much deeper vanilla Transformers (e.g., $N=16$). On the PTB corpus, TransfoRNN reduced perplexity by up to 5.5% while being 10.5% smaller. On WikiText-2, it achieved a 2.2% PPL reduction with a 27.7% smaller model size.

Positional Encoding Redundancy

Interestingly, the inclusion of RNN layers often makes traditional positional encodings redundant. Experimental results showed that for certain configurations, removing positional encodings actually improved performance, as the RNN naturally handles token ordering.

Conclusion

TransfoRNN offers a compelling alternative to deep Transformer stacks. By integrating a shallow RNN layer, the model achieves state-of-the-art results with fewer parameters and lower computational overhead. This hybrid approach suggests that the most effective way to model language may lie in combining the strengths of both attention-based and recurrent architectures.

Summary updated

BibTeX
@misc{chong2021transfornn,
  author = {Tze Yuang Chong and Xuyang Wang and Lin Yang and Junjie Wang},
  title = {{TransfoRNN: Capturing the Sequential Information in Self-Attention Representations for Language Modeling}},
  year = {2021},
  eprint = {2104.01572},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2104.01572},
}
Figure