Abstract
Transformers have revolutionized the world of deep learning, specially in the field of natural language processing. Recently, the Audio Spectrogram Transformer (AST) was proposed for audio classification, leading to state of the art results in several datasets. However, in order for ASTs to outperform CNNs, pretraining with ImageNet is needed. In this paper, we study one component of the AST, the positional encoding, and propose several variants to improve the performance of ASTs trained from scratch, without ImageNet pretraining. Our best model, which incorporates conditional positional encodings, significantly improves performance on Audioset and ESC-50 compared to the original AST.
Introduction
Transformers are inherently permutation-invariant, meaning they treat input sequences as unordered sets unless explicitly provided with positional information. In the audio domain, the Audio Spectrogram Transformer (AST) relies on positional encodings to understand the time-frequency structure of spectrograms. Traditionally, AST models achieve peak performance only after pre-training on massive image datasets like ImageNet, which helps them learn spatial relationships. This study explores whether sophisticated positional encoding (PE) strategies can provide the necessary inductive biases—such as locality and translation invariance—to allow ASTs to excel without external image data.
Methods
The research evaluates several positional encoding strategies to replace or augment the standard absolute positional encoding used in the original AST:
- Conditional Positional Encodings (CPE): Unlike fixed learned vectors, CPE utilizes a Positional Encoding Generator (PEG). As shown in Figure 1, this involves a 2D depth-wise convolutional layer that dynamically generates encodings based on the local neighborhood of input patches. This introduces a "locality" bias similar to that found in Convolutional Neural Networks (CNNs).
- Relative Attention: This method modifies the attention mechanism to account for the relative distance between patches in time and frequency axes, rather than adding position information at the input layer.
- ALiBi 2D (Attention with Linear Biases): A non-learned approach that penalizes attention scores based on the distance between patches, encouraging the model to prioritize local context.
Results and Analysis
Analysis of standard absolute PE revealed a significant limitation: the model primarily learns to distinguish frequency bands but struggles to differentiate temporal positions. This suggests that for many audio tasks, the frequency profile is more critical than the exact timestamp.
The introduction of CPE addresses this by providing translation invariance and locality. Because audio events (e.g., a dog bark) can occur at any point in time, the ability to recognize patterns regardless of their absolute coordinates is vital. CPE is also highly efficient, adding only ~38.4k parameters to the model.
As summarized in the results table below, the CPE-based model ("Conditional") outperformed the absolute PE baseline across both major benchmarks when trained from scratch.
On AudioSet, the CPE model achieved a Mean Average Precision (mAP) of 0.343, compared to 0.313 for the absolute PE baseline. On ESC-50, it reached 91.4% accuracy, a substantial improvement over the 87.5% baseline. Learned relative attention (0.329 mAP) also outperformed absolute PE, confirming that relative distance is more informative for audio than absolute coordinates.
Conclusion
By replacing standard absolute positional encodings with Conditional Positional Encodings, AST models can better capture the intrinsic time-frequency structure of audio. While these improvements significantly close the gap for "from-scratch" training, they do not yet match the performance of models pre-trained on ImageNet (0.485 mAP on AudioSet). This indicates that while better PE strategies provide essential inductive biases, the scale of ImageNet pre-training offers additional feature extraction benefits that remain a challenge for audio-only training to replicate.
Summary updated