Skip to content

Commit da174c7

Browse files
committed
Update files and add run_benchmark.py
1 parent 289d6d7 commit da174c7

12 files changed

+321
-46
lines changed

README.md

Lines changed: 104 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,117 @@
33
<!-- TITLE -->
44
# Positional Encoding Benchmark for Time Series Classification
55

6-
This repository provides a comprehensive benchmark for evaluating different positional encoding techniques in Transformer models, specifically for time series classification tasks. The project includes implementations of several positional encoding methods and Transformer architectures to test their effectiveness on various time series datasets.
7-
8-
9-
10-
<!-- DESCRIPTION -->
11-
## Description
12-
13-
14-
This project aims to analyze how positional encodings impact Transformer-based models in time series classification. The benchmark includes both fixed and learnable encoding techniques and explores advanced approaches like relative positional encoding. The project evaluates performance on a diverse set of datasets from different domains, such as human activity recognition, financial data, EEG recordings, etc.
15-
16-
17-
## Positional Encoding Methods:
18-
- Absolute Positional Encoding (APE)
19-
- Learnable Positional Encoding (LPE)
20-
- Relative Positional Encoding (RPE)
21-
- Temporal Pseudo-Gaussian augmented Self-Attention (TPS)
22-
- Temporal Uncertainty Positional Encoding (TUPE)
23-
- time Absolute Positional Encoding (tAPE)
24-
- efficient Relative Position Encoding (eRPE)
25-
- Stochastic Positional Encoding (SPE)
26-
6+
This repository provides a comprehensive evaluation framework for positional encoding methods in transformer-based time series models, along with implementations and benchmarking results.
7+
8+
Our work is available on arXiv: [Positional Encoding in Transformer-Based Time Series Models: A Survey](https://arxiv.org/abs/2502.12370)
9+
10+
## Models
11+
12+
We present a systematic analysis of positional encoding methods evaluated on two transformer architectures:
13+
1. [Multivariate Time Series Transformer Framework (TST)](https://github.com/gzerveas/mvts_transformer)
14+
2. Time Series Transformer with Patch Embedding
15+
16+
17+
18+
### Positional Encoding Methods
19+
We implement and evaluate eight positional encoding methods:
20+
21+
| Method | Type | Injection Technique | Parameters |
22+
|--------|------|-------------------|------------|
23+
| Sinusoidal PE | Absolute | Additive | 0 |
24+
| Learnable PE | Absolute | Additive | L×d |
25+
| RPE | Relative | MAM | 2(2L-1)dl |
26+
| tAPE | Absolute | Additive | Ld |
27+
| eRPE | Relative | MAM | (L²+L)l |
28+
| TUPE | Rel+Abs | MAM | 2dl |
29+
| ConvSPE | Relative | MAM | 3Kdh+dl |
30+
| T-PE | Rel+Abs | Combined | 2d²l/h+(2L+2l)d |
31+
32+
Where:
33+
- L: sequence length
34+
- d: embedding dimension
35+
- h: number of attention heads
36+
- K: kernel size
37+
- l: number of layers
38+
39+
## Dataset Characteristics
40+
41+
| Dataset | Train Size | Test Size | Length | Classes | Channels | Type |
42+
|---------|------------|-----------|---------|----------|-----------|------|
43+
| Sleep | 478,785 | 90,315 | 178 | 5 | 1 | EEG |
44+
| ElectricDevices | 8,926 | 7,711 | 96 | 7 | 1 | Device |
45+
| FaceDetection | 5,890 | 3,524 | 62 | 2 | 144 | EEG |
46+
| MelbournePedestrian | 1,194 | 2,439 | 24 | 10 | 1 | Traffic |
47+
| SharePriceIncrease | 965 | 965 | 60 | 2 | 1 | Financial |
48+
| LSST | 2,459 | 2,466 | 36 | 14 | 6 | Other |
49+
| RacketSports | 151 | 152 | 30 | 4 | 6 | HAR |
50+
| SelfRegulationSCP1 | 268 | 293 | 896 | 2 | 6 | EEG |
51+
| UniMiB-SHAR | 4,601 | 1,524 | 151 | 9 | 3 | HAR |
52+
| RoomOccupancy | 8,103 | 2,026 | 30 | 4 | 18 | Sensor |
53+
| EMGGestures | 1,800 | 450 | 30 | 8 | 9 | EMG |
2754

2855
## Dependencies
2956
- Python 3.10
57+
- PyTorch 2.4.1+cu121
3058
- NumPy
31-
- SciPy
32-
- Matplotlib
33-
- Seaborn
34-
- Pandas
3559
- Scikit-learn
36-
- tsai
37-
- PyTorch
38-
39-
40-
## Installation
60+
- CUDA 12.2
4161

42-
To install and run the Positional Encoding Benchmark, follow these steps:
62+
## Clone and Installation
4363

4464
```bash
65+
# Clone the repository
4566
git clone https://github.com/imics-lab/positional-encoding-benchmark.git
4667
cd positional-encoding-benchmark
68+
69+
# Create virtual environment
70+
python -m venv venv
71+
source venv/bin/activate # Linux/Mac
72+
# or
73+
.\venv\Scripts\activate # Windows
74+
75+
# Install dependencies
4776
pip install -r requirements.txt
77+
78+
# Run benchmark with default config
79+
python examples/run_benchmark.py
80+
81+
# Or with custom config
82+
python examples/run_benchmark.py --config path/to/custom_config.yaml
4883
```
4984

85+
## Results
86+
87+
Our experimental evaluation encompasses eight distinct positional encoding methods tested across eleven diverse time series datasets using two transformer architectures.
88+
89+
### Key Findings
90+
91+
#### 1. Sequence Length Impact
92+
- **Long sequences** (>100 steps): 5-6% improvement with advanced methods
93+
- **Medium sequences** (50-100 steps): 3-4% improvement
94+
- **Short sequences** (<50 steps): 2-3% improvement
95+
96+
#### 2. Architecture Performance
97+
- **TST**: More distinct performance gaps
98+
- **Patch Embedding**: More balanced performance among top methods
99+
100+
#### 3. Average Rankings
101+
- **SPE**: 1.727 (batch norm), 2.090 (patch embed)
102+
- **TUPE**: 1.909 (batch norm), 2.272 (patch embed)
103+
- **T-PE**: 2.636 (batch norm), 2.363 (patch embed)
104+
105+
### Performance Analysis
106+
107+
#### Biomedical Signals (EEG, EMG)
108+
- TUPE achieves highest average accuracy
109+
- SPE shows strong performance
110+
- Both methods demonstrate effectiveness in capturing long-range dependencies
111+
112+
#### Environmental and Sensor Data
113+
- SPE exhibits superior performance
114+
- TUPE maintains competitive accuracy
115+
- Relative encoding methods show improved local pattern recognition
116+
50117
<!-- CONTRIBUTING -->
51118
## Contributing
52119
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
@@ -58,5 +125,10 @@ Please make sure to update tests as appropriate.
58125
## Citation
59126

60127
```bibtex
61-
TBD
128+
@article{irani2025positional,
129+
title={Positional Encoding in Transformer-Based Time Series Models: A Survey},
130+
author={Irani, Habib and Metsis, Vangelis},
131+
journal={arXiv preprint arXiv:2502.12370},
132+
year={2025}
133+
}
62134
```

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies:
2525
- pandas
2626
- scikit-learn
2727
- matplotlib
28+
2829
# Uncomment the below to add pip dependencies if necessary"
2930
# - pip:
3031
# - mydep==1.0

src/config/config.yalm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
model:
2+
type: 'batch_norm' # or 'patch_embed'
3+
encoding: 'tupe' # ['pe', 'lpe', 'rpe', 'tape', 'erpe', 'tupe', 'convspe', 't-pe']
4+
input_timesteps: 100
5+
patch_size: 16
6+
embedding_dim: 128
7+
num_layers: 4
8+
num_heads: 8
9+
dim_feedforward: 256
10+
dropout: 0.1
11+
12+
training:
13+
batch_size: 32
14+
learning_rate: 0.001
15+
epochs: 100
16+
device: 'cuda'
17+
18+
dataset:
19+
name: 'Sleep'

src/encodings/__init__.py

Whitespace-only changes.
File renamed without changes.

src/positional_encodings.py renamed to src/encodings/positional_encodings.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,6 @@ def get_relative_positions(
122122
x - y, bidirectional, num_buckets, max_distance
123123
)
124124
return relative_positions
125-
126-
def get_pos_encoder(pos_encoding):
127-
if pos_encoding == 'fixed':
128-
return FixedPositionalEncoding
129-
elif pos_encoding == 'learned':
130-
return LearnedPositionalEncoding
131-
elif pos_encoding == 'tape':
132-
return tAPE
133-
elif pos_encoding == 'absolute':
134-
return AbsolutePositionalEncoding
135-
else:
136-
raise ValueError(f"Unknown positional encoding type: {pos_encoding}")
137125

138126

139127

@@ -204,3 +192,17 @@ def __init__(self, d_model, num_variables):
204192
def forward(self, x, variable_idx):
205193
variable_embed = self.variable_embedding(variable_idx)
206194
return x + variable_embed.unsqueeze(0)
195+
196+
197+
198+
def get_pos_encoder(pos_encoding):
199+
if pos_encoding == 'fixed':
200+
return FixedPositionalEncoding
201+
elif pos_encoding == 'learned':
202+
return LearnedPositionalEncoding
203+
elif pos_encoding == 'tape':
204+
return tAPE
205+
elif pos_encoding == 'absolute':
206+
return AbsolutePositionalEncoding
207+
else:
208+
raise ValueError(f"Unknown positional encoding type: {pos_encoding}")

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from load_data import get_dataset
1010

1111
import torch.optim as optim
12-
from time_series_transformer import TimeSeriesTransformer
12+
from src.models.TimeSeriesTransformer import TimeSeriesTransformer
1313
from time_series_transformer_batchnorm import TSTransformerEncoder
1414
from utils import get_dataloaders
1515

src/TSTransformer_batchnorm.py renamed to src/models/TSTransformerEncoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import torch
22
import torch.nn as nn
3-
from patch_embedding_layer import TimeSeriesPatchEmbeddingLayer
43
import math
4+
from src.encodings.positional_encodings import get_pos_encoder
55

66
class TransformerBatchNormEncoderLayer(nn.Module):
77
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation="gelu"):
File renamed without changes.

src/models/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)