Pytorch Forecasting

Time series forecasting with PyTorch

Last verified:

Visit Pytorch Forecasting

What is Pytorch Forecasting?

PyTorch Forecasting is an open-source Python library designed to simplify state-of-the-art time series forecasting with neural networks for both real-world production use cases and research. The library provides a high-level API that balances maximum flexibility for professionals with reasonable defaults for beginners, making advanced forecasting techniques accessible, reproducible, and production-ready.

The package provides a comprehensive timeseries dataset class that abstracts handling variable transformations, missing values, randomized subsampling, and multiple history lengths. It includes a base model class that provides basic training of timeseries models along with logging in TensorBoard and generic visualizations such as actual vs predictions and dependency plots. Multiple neural network architectures for timeseries forecasting are included, enhanced for real-world deployment with in-built interpretation capabilities. The library also offers multi-horizon timeseries metrics and hyperparameter tuning with Optuna.

PyTorch Forecasting is built on PyTorch Lightning to allow training on CPUs, single GPUs, and multiple GPUs out-of-the-box. It targets data scientists, machine learning engineers, researchers, and analysts working on demand forecasting, energy forecasting, financial forecasting, and other time-dependent prediction tasks. The library includes advanced models like Temporal Fusion Transformer, DeepAR, NBeats, NHiTS, TiDEModel, and xLSTMTime, each with different capabilities for covariates, uncertainty estimation, and computational requirements.

The library is completely free and open-source, available via pip install pytorch-forecasting or conda install pytorch-forecasting. Users can install additional dependencies like MQF2 loss with pip install pytorch-forecasting[mqf2]. There are no paid tiers or commercial licensing requirements.

Pytorch Forecasting pricing

Pricing model: Freemium

PyTorch Forecasting is completely free and open-source. It is available via pip (pip install pytorch-forecasting) and conda (conda install pytorch-forecasting pytorch>=1.7 -c pytorch -c conda-forge). There are no paid tiers, subscription plans, or commercial licensing requirements. Optional extra dependencies like MQF2 loss can be installed with pip install pytorch-forecasting[mqf2]. The library is hosted on Read the Docs and GitHub with no cost barriers.

Pytorch Forecasting pros

  • Open-source and completely free to use
  • High-level API simplifies complex forecasting tasks
  • Built on PyTorch Lightning for scalable training
  • Supports CPU, single GPU, and multi-GPU training out-of-the-box
  • Multiple state-of-the-art architectures included (TFT, DeepAR, NBeats, NHiTS)
  • Built-in model interpretability and visualization tools
  • TimeSeriesDataSet handles missing values and variable transformations automatically
  • Integrated hyperparameter tuning with Optuna
  • Multi-horizon forecasting metrics for proper evaluation
  • Supports covariates (known and unknown future inputs)
  • Handles both regression and classification tasks
  • Temporal Fusion Transformer supports heterogeneous targets
  • In-built uncertainty estimation for probabilistic forecasting
  • TensorBoard logging automatically integrated
  • Cold-start predictions supported by TemporalFusionTransformer
  • Variable encoder and decoder length by sample
  • Embedding support for categorical variables
  • Ranger optimizer available for faster training
  • Dependency plots and actual vs prediction visualizations
  • Maximum flexibility for professionals with sensible defaults for beginners

Pytorch Forecasting cons

  • Steep learning curve for beginners unfamiliar with PyTorch
  • Requires PyTorch installed separately before installation
  • Dataset creation can be very slow with allow_missing_timesteps=True
  • Models can be too big causing training to appear frozen
  • Learning rate finder may not finish without proper configuration
  • Temporal Fusion Transformer requires high computational resources (rating 4/5)
  • MQF2 loss requires separate extra installation
  • No built-in support for normalizing flows models
  • Autoregressive models like DeepAR slow at inference time
  • Missing NaN values must be pre-filled by user
  • Long encoder/decoder lengths significantly increase training time
  • No managed cloud service or commercial support available
  • Model selection requires understanding multiple criteria
  • Documentation assumes some familiarity with time series concepts
  • Windows users need special PyTorch installation steps

Frequently asked questions about Pytorch Forecasting

How do I create a dataset for new samples?

Use the from_dataset() method of your training dataset to create datasets on which you can run inference. The TimeSeriesDataSet class provides this functionality for generating inference datasets from your trained model's dataset configuration.

How long should the encoder and decoder/prediction length be?

Choose something reasonably long, but not much longer than 500 for the encoder length and 200 for the decoder length. Consider that longer lengths increase the time it takes for your model to train. The ratio of decoder and encoder length depends on the used algorithm, so check the model documentation for guidance.

It takes very long to create the dataset. Why is that?

If you set allow_missing_timesteps=True in your dataset, the creation of an index might take far more time as all missing values in the timeseries have to be identified. The algorithm might be possible to speed up but currently, it might be faster for you to not allow missing values and fill them yourself.

How are missing values treated?

Missing values between time points are either filled up with a fill forward or a constant fill-in strategy. Missing values indicated by NaNs are a problem and should be filled in up-front, e.g. with the median value and another missing indicator categorical variable. Missing values in the future (out of range) are not filled in and simply not predicted - you have to provide values into the future.

My training seems to freeze - nothing seems to be happening although my CPU/GPU is working at 100%. How to fix this issue?

Probably, your model is too big. Check the number of parameters with model.size() or the dataset encoder and decoder length are unrealistically large. See the model documentation to reduce model size or adjust encoder/decoder lengths.

Why does the learning rate finder not finish?

First, ensure that the trainer does not have the keyword fast_dev_run=True and limit_train_batches=... set. Second, use a target normalizer in your training dataset. Third, increase the early_stop_threshold argument of the lr_find method to a large number.

Why do I get lots of matplotlib warnings when running the learning rate finder?

This is because you keep on creating plots for logging but without a logger. Set log_interval=-1 in your model to avoid this behaviour.

How do I choose hyperparameters?

Consult the model documentation to understand which parameters are important and which ranges are reasonable. Choose the learning rate with the learning rate finder. To tune hyperparameters, the optuna package is a great place to start with, as PyTorch Forecasting integrates directly with Optuna for automated hyperparameter tuning.

What interpretation is built into PyTorch Forecasting?

Look up the documentation for the model you use for model-specific interpretation. Further, all models come with some basic methods inherited from BaseModel, including visualization capabilities for actual vs predictions and dependency plots.

Which model should I select for my use case?

Criteria for selecting an architecture depend heavily on the use-case. Consider: availability of covariates (NBeats cannot use them), length of time series (TFT performs better on short series), number of timeseries and their relation (DeepAR and TFT learn relationships between series), type of prediction task (NBeats only does regression, TFT supports regression and classification), and computational resources (NBeats and NHiTS are efficient with rating 1, TFT requires more resources with rating 4).

Categories

Use cases

Browse all AI tools on NeedAnAI