NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Transformers with Learnable Activation Functions

Haishuo Fang, Ji-Ung Lee, Nafise Sadat Moosavi, Iryna Gurevych · DOI

Introduction

Standard Transformer architectures typically employ a "one-size-fits-all" approach to activation functions. Fixed formulas like GELU or ReLU are applied uniformly across all layers and tasks. However, this approach ignores the functional diversity of different layers within a deep network. This paper introduces the Rational Activation Function Transformer (RAFT), a novel architecture that allows models to learn and adapt the shape of their activation functions for every layer and specific downstream task.

The RAFT Approach

The core innovation of RAFT is the replacement of fixed activation functions with Rational Activation Functions (RAFs). Mathematically, an RAF is defined as the ratio of two polynomials, $P(x)/Q(x)$. These functions act as universal function approximators, capable of mimicking standard functions like GELU or evolving into entirely new, non-traditional shapes.

To ensure training stability, RAFT models are initialized so that the RAFs perfectly mimic the standard GELU function. During pre-training and fine-tuning, the coefficients of these polynomials are treated as trainable parameters. Remarkably, this flexibility adds negligible overhead; each RAF adds only 9 parameters per layer, totaling just 108 extra parameters for a standard 12-layer BERT model—less than 0.0001% of the total model size.

Key Insights and Visual Analysis

Layer Heterogeneity and Task Adaptation

Experimental results demonstrate that the "optimal" activation function is rarely a standard curve. After training, the learned functions vary significantly across different layers of the model. This suggests that different depths of the Transformer require different types of non-linearity to process information effectively, ranging from surface-level features to complex semantic relationships.

Figure 2: Rational functions across different layers for pre-training and various fine-tuning tasks (SQuAD, MNLI, SST2).

As shown in the figure above, the activation functions in the top layers undergo the most significant changes during fine-tuning. This indicates that the model actively adapts its internal logic to meet the specific requirements of a downstream task, such as span prediction in SQuAD versus sentiment classification in SST2.

Stability Across Tasks

While activation functions vary wildly between layers, the behavior of a specific layer across different tasks can show surprising consistency. For instance, layer 6 tends to maintain a similar functional shape regardless of whether the model is performing natural language inference or linguistic acceptability tasks.

Figure 6: Rational functions in layer 6 across different tasks, showing relative stability compared to inter-layer variation.

Performance Results

  • Low-Data Efficiency: RAFT's most significant advantage appears in data-scarce environments. When trained on only 100 examples, RAFT outperformed standard fixed-activation baselines by an average of 5.71 points across GLUE tasks. The learnable nature of the activation functions provides a powerful inductive bias when data is limited.
  • Benchmark Success: In full-data scenarios, RAFT improved average GLUE scores by nearly 1 point and achieved a 2.05 point F1 improvement on SQuAD v1.1.
  • Inference Speed: Despite the mathematical complexity of ratios, a custom CUDA implementation of RAFT was 13.8% faster than the GELU baseline during inference, as it avoids the transcendental calculations required by standard functions.

Conclusion

RAFT demonstrates that the choice of activation function should not be a static hyperparameter. By making these functions learnable, Transformers can achieve higher accuracy and better generalization, particularly in low-data regimes, without significant increases in parameter count. Future work will explore the potential of RAFT for structural pruning and its performance at the scale of billions of parameters.

Summary updated

BibTeX
@inproceedings{fang2023transformers,
  author = {Haishuo Fang and Ji-Ung Lee and Nafise Sadat Moosavi and Iryna Gurevych},
  title = {{Transformers with Learnable Activation Functions}},
  booktitle = {EACL (Findings)},
  year = {2023},
  doi = {10.18653/v1/2023.findings-eacl.181},
}
Figure