How yuqinie98/patchtst works
This repository is a research codebase for long-term time-series forecasting centered on PatchTST, with two largely separate execution paths: a supervised forecasting stack and a self-supervised pretrain/fine-tune stack. Both use the same core modeling idea—patching the input series into tokens and applying Transformer-style sequence modeling—but they organize training and data handling differently.
1. Top-level split: supervised vs self-supervised
- PatchTST_supervised/ contains the main benchmark training pipeline for forecasting.
- PatchTST_self_supervised/ contains masked pretraining plus downstream fine-tuning/linear probing.
- The README explicitly treats them as separate workflows with separate entry scripts and script folders.
2. Supervised training flow
- Main entry point is PatchTST_supervised/run_longExp.py.
- Training logic is organized around exp/exp_basic.py and exp/exp_main.py, which define the experiment lifecycle: dataset loading, model construction, training, validation, testing, and result logging.
- Shell scripts under PatchTST_supervised/scripts/ parameterize runs for specific datasets and model variants.
3. Supervised model zoo
- PatchTST_supervised/models/ includes PatchTST plus baseline forecasting models: Linear, NLinear, DLinear, Transformer, Informer, Autoformer, and some statistical baselines.
- The model implementations rely on shared building blocks in layers/, especially PatchTST-specific patch/backbone modules and generic transformer embedding/attention components.
4. Supervised data ingestion
- PatchTST_supervised/data_provider/ is the dataset boundary.
- data_loader.py and data_factory.py turn CSV-based benchmark datasets into train/validation/test loaders.
- utils/timefeatures.py and utils/masking.py support calendar feature extraction and sequence masking needed by the forecasting pipelines.
5. PatchTST architectural core
- The PatchTST-specific implementation is split across layers/PatchTST_backbone.py, layers/PatchTST_layers.py, and models/PatchTST.py.
- The key architectural idea is to convert each channel’s history into patches, embed those patches as tokens, and process them with shared Transformer weights across channels.
- layers/RevIN.py adds reversible normalization, which is an important preprocessing/postprocessing boundary for time-series stability.
6. Self-supervised training flow
- Entry points are PatchTST_self_supervised/patchtst_pretrain.py, patchtst_finetune.py, and patchtst_supervised.py.
- The self-supervised path is organized more like a modern training stack, with src/learner.py coordinating the optimization loop and src/callback/ providing modular training behaviors.
- Pretraining saves a model artifact under saved_model/ for reuse in downstream tasks.
7. Self-supervised data and task abstraction
- src/data/datamodule.py and src/data/pred_dataset.py define the data module and prediction datasets.
- datautils.py acts as a dataset preparation utility layer.
- This path is built around masked patch reconstruction, so src/callback/patch_mask.py is a central training-time transformation.
8. Self-supervised model stack
- src/models/patchTST.py contains the self-supervised PatchTST model definition.
- Shared layers live under src/models/layers/, including attention, positional encoding, heads, and RevIN.
- The model consumes patchified sequences and produces either reconstruction or forecasting outputs depending on the training phase.
9. Training infrastructure and callbacks
- src/callback/ contains reusable training infrastructure: scheduling, tracking/logging, distributed support, patch masking, and transforms.
- This suggests the self-supervised code is more framework-like than the supervised code, with training behavior separated from the model itself.
- src/basics.py, src/utils.py, and src/metrics.py provide common helpers and evaluation support.
10. Baseline model families under Formers
- PatchTST_supervised/Formers/ and its nested FEDformer/ and Pyraformer/ directories are preserved baseline implementations from related forecasting papers.
- These subtrees have their own run.py, exp/, data_provider/, layers/, and shell scripts, meaning they are effectively self-contained benchmarking mini-projects inside the repo.
- Their presence matters architecturally because the repo is not just PatchTST; it is also a benchmarking harness against prior Transformer-based forecasters.
11. Execution and configuration boundary
- Most experimentation is driven by shell scripts in scripts/ and Formers/*/*.sh.
- Datasets are expected externally in a local dataset/ directory and are not bundled with the repository.
- Outputs are written to model/result artifacts such as result.txt and saved_model/, making the repo oriented around offline experiment runs rather than a serving system.
12. Common design patterns across the repo
- Shared concerns recur across both stacks: time-feature extraction, masking, metrics, normalization, and Transformer-style layer blocks.
- The major architectural boundary is not frontend/backend, but rather “data ingestion → patch/token construction → sequence model → forecast/reconstruction → metrics/logging.”
- The most important diagram elements are the two training pipelines, the shared PatchTST backbone, the dataset loaders, and the baseline model zoo.
Main entry points: exp_main.py Experiment loop · models Model zoo · learner.py Learner · patchTST.py PatchTST model
Components 17
Supervised stack
Benchmark forecasting pipeline with multiple model families and shared experiment utilities.
- Run exp · Main supervised forecasting entry script.
PatchTST_supervised/run_longExp.py - Experiment loop · Loads data, builds models, runs train/val/test, and records results.
PatchTST_supervised/exp/exp_main.py - Data provider · CSV benchmark loading and split construction for forecasting experiments.
PatchTST_supervised/data_provider/data_factory.py - Model zoo · PatchTST plus linear, transformer, informer, autoformer, and statistical baselines.
PatchTST_supervised/models - PatchTST core · Patchify-embed-transformer backbone for supervised forecasting.
PatchTST_supervised/models/PatchTST.py - Shared layers · Patch backbone, embeddings, attention, encoder-decoder modules, and RevIN.
PatchTST_supervised/layers - Utils · Time features, masking, metrics, and experiment helpers used across the supervised stack.
PatchTST_supervised/utils
Self-supervised stack
Masked pretrain and downstream fine-tuning workflow built around a learner/callback training framework.
- Pretrain / finetune · Self-supervised pretraining, finetuning, and supervised downstream scripts.
PatchTST_self_supervised/patchtst_pretrain.py - Learner · Coordinates optimization and phase-specific behavior for self-supervised runs.
PatchTST_self_supervised/src/learner.py - Callbacks · Scheduling, tracking, distributed support, masking, and transforms.
PatchTST_self_supervised/src/callback - Data module · Data module and prediction dataset abstractions for self-supervised workflows.
PatchTST_self_supervised/src/data/datamodule.py - Dataset prep · Dataset preparation helpers and shared time-feature logic.
PatchTST_self_supervised/datautils.py - PatchTST model · Patchified sequence model used for masked reconstruction and forecasting.
PatchTST_self_supervised/src/models/patchTST.py - Model layers · Attention, positional encoding, heads, and RevIN for the self-supervised stack.
PatchTST_self_supervised/src/models/layers
Baseline formers
Self-contained benchmark subprojects preserved under the supervised tree.
- FEDformer · Standalone FEDformer/Informer/Autoformer benchmark subtree with its own data, exp, layers, and scripts.
PatchTST_supervised/Formers/FEDformer - Pyraformer · Standalone Pyraformer benchmark subtree with preprocessing, model code, and scripts.
PatchTST_supervised/Formers/Pyraformer - Former scripts · Shell entry points for baseline benchmarking under Formers.
PatchTST_supervised/Formers/Autoformer.sh
Connections 18
- Run exp → Experiment loop: launches · Main script creates and runs experiment objects.
- Experiment loop → Data provider: loads · Experiment lifecycle constructs train/val/test data loaders.
- Experiment loop → Model zoo: builds · Experiment selects a forecasting model implementation.
- Model zoo → PatchTST core: includes · PatchTST is the central model family in the supervised zoo.
- PatchTST core → Shared layers: uses · PatchTST depends on shared patching, attention, and normalization layers.
- Experiment loop → Utils: reports · Training and evaluation use shared metrics and helper utilities.
- Data provider → Utils: formats · Dataset handling relies on time features and masking helpers.
- Pretrain / finetune → Learner: orchestrates · Entry scripts instantiate the learner for pretrain and finetune phases.
- Learner → Callbacks: composes · Learner delegates training behavior to modular callbacks.
- Pretrain / finetune → Data module: binds · Training scripts wire in the data module and prediction datasets.
- Data module → Dataset prep: prepares · Data module relies on dataset preparation utilities.
- Learner → PatchTST model: trains · Learner optimizes the PatchTST self-supervised model.
- PatchTST model → Model layers: uses · Model is built from reusable attention, heads, encoding, and RevIN layers.
- Callbacks → PatchTST model: masks · Patch masking is a central training-time transformation for pretraining.
- Model zoo → FEDformer: contains · FEDformer-family baselines are preserved alongside the main supervised stack.
- Model zoo → Pyraformer: contains · Pyraformer is included as a separate baseline subtree.
- Former scripts → FEDformer: runs · Shell scripts launch baseline benchmark experiments.
- Former scripts → Pyraformer: runs · Shell scripts also cover Pyraformer benchmark runs.