Top Related Projects
Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.
A unified framework for machine learning with time series
A python library for user-friendly forecasting and anomaly detection on time series.
Scalable and user friendly neural :brain: forecasting algorithms.
Quick Overview
PyTorch Forecasting is an open-source library for time series forecasting with PyTorch. It provides a collection of state-of-the-art neural network models for forecasting, along with utilities for data preprocessing, model training, and evaluation. The library aims to make advanced forecasting techniques accessible and easy to use for both researchers and practitioners.
Pros
- Offers a wide range of modern forecasting models, including Temporal Fusion Transformers and DeepAR
- Provides seamless integration with PyTorch ecosystem and GPU acceleration
- Includes built-in dataset classes and preprocessing utilities for time series data
- Supports both probabilistic and point forecasts
Cons
- Steeper learning curve compared to traditional statistical forecasting libraries
- Requires more computational resources, especially for large datasets and complex models
- Documentation could be more comprehensive for some advanced features
- Limited support for classical statistical models compared to specialized forecasting libraries
Code Examples
- Creating a dataset:
from pytorch_forecasting import TimeSeriesDataSet
data = TimeSeriesDataSet(
df,
time_idx="timestamp",
target="sales",
group_ids=["store", "product"],
static_categoricals=["store_type"],
time_varying_known_reals=["price", "promotion"],
max_encoder_length=30,
max_prediction_length=7,
)
- Defining and training a model:
from pytorch_forecasting import TemporalFusionTransformer, TimeSeriesDataSet
from pytorch_forecasting.metrics import MAE
model = TemporalFusionTransformer.from_dataset(
data,
learning_rate=1e-3,
hidden_size=32,
attention_head_size=1,
dropout=0.1,
loss=MAE(),
)
trainer = pl.Trainer(max_epochs=10, gpus=1)
trainer.fit(model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader)
- Making predictions:
predictions = model.predict(test_dataloader)
Getting Started
To get started with PyTorch Forecasting:
- Install the library:
pip install pytorch-forecasting
- Import necessary modules:
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer
import pytorch_lightning as pl
- Prepare your data, create a dataset, and split it into train and validation sets:
data = TimeSeriesDataSet(
your_dataframe,
time_idx="timestamp",
target="target_variable",
group_ids=["group_column"],
# Add other relevant parameters
)
train, val = data.split_samples(val_size=0.2)
- Create dataloaders, define your model, and start training:
train_dataloader = train.to_dataloader(batch_size=32, num_workers=0)
val_dataloader = val.to_dataloader(batch_size=32, num_workers=0)
model = TemporalFusionTransformer.from_dataset(data)
trainer = pl.Trainer(max_epochs=10)
trainer.fit(model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader)
Competitor Comparisons
Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.
Pros of Prophet
- Easier to use for beginners with minimal configuration required
- Handles seasonality and holidays automatically
- Built-in visualization tools for forecasting results
Cons of Prophet
- Less flexible for custom model architectures
- Limited support for external regressors and covariates
- May struggle with complex, non-linear patterns in data
Code Comparison
Prophet:
from fbprophet import Prophet
model = Prophet()
model.fit(df)
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)
pytorch-forecasting:
from pytorch_forecasting import TemporalFusionTransformer, TimeSeriesDataSet
training = TimeSeriesDataSet(
train_data,
time_idx="timestamp",
target="target",
group_ids=["group"],
static_categoricals=["static_cat"],
time_varying_known_reals=["time_varying_real"],
)
model = TemporalFusionTransformer.from_dataset(training)
model.fit(training)
Prophet focuses on simplicity and ease of use, while pytorch-forecasting offers more flexibility and advanced modeling techniques. Prophet is better suited for quick forecasting tasks with minimal setup, whereas pytorch-forecasting excels in complex time series problems requiring custom architectures and additional features.
A unified framework for machine learning with time series
Pros of sktime
- Broader scope covering various time series tasks beyond forecasting
- Scikit-learn compatible API, making it familiar for many data scientists
- Extensive collection of algorithms and tools for time series analysis
Cons of sktime
- Less specialized for deep learning forecasting models
- May have a steeper learning curve for users primarily interested in forecasting
Code Comparison
sktime:
from sktime.forecasting.arima import ARIMA
from sktime.datasets import load_airline
y = load_airline()
forecaster = ARIMA(order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
forecaster.fit(y)
y_pred = forecaster.predict(fh=[1, 2, 3])
pytorch-forecasting:
from pytorch_forecasting import TemporalFusionTransformer, TimeSeriesDataSet
training = TimeSeriesDataSet(
data,
time_idx="timestamp",
target="target",
group_ids=["group"],
static_categoricals=["group"],
)
model = TemporalFusionTransformer.from_dataset(training)
model.fit(training)
predictions = model.predict(data)
Both libraries offer powerful forecasting capabilities, but sktime provides a more comprehensive toolkit for time series analysis, while pytorch-forecasting specializes in deep learning models for forecasting tasks.
A python library for user-friendly forecasting and anomaly detection on time series.
Pros of Darts
- Broader range of models, including classical statistical methods and machine learning algorithms
- More flexible data handling, supporting various input formats and multivariate time series
- Extensive built-in preprocessing and feature engineering capabilities
Cons of Darts
- Less focus on deep learning models compared to PyTorch Forecasting
- May have a steeper learning curve for users new to time series forecasting
- Potentially slower performance for large-scale forecasting tasks
Code Comparison
Darts example:
from darts import TimeSeries
from darts.models import Prophet
series = TimeSeries.from_dataframe(df, 'date', 'value')
model = Prophet()
model.fit(series)
forecast = model.predict(n=30)
PyTorch Forecasting example:
from pytorch_forecasting import TemporalFusionTransformer, TimeSeriesDataSet
training = TimeSeriesDataSet(
train_data,
time_idx="timestamp",
target="target",
group_ids=["id"],
static_categoricals=["category"],
)
model = TemporalFusionTransformer.from_dataset(training)
model.fit(training)
predictions = model.predict(test_data)
Both libraries offer powerful time series forecasting capabilities, with Darts providing a wider range of traditional and machine learning models, while PyTorch Forecasting specializes in deep learning approaches.
Scalable and user friendly neural :brain: forecasting algorithms.
Pros of neuralforecast
- More focused on neural network-based forecasting models
- Includes implementation of advanced models like Temporal Fusion Transformers
- Designed for scalability and handling large datasets
Cons of neuralforecast
- Less comprehensive in terms of classical statistical models
- May have a steeper learning curve for users new to deep learning
Code Comparison
neuralforecast:
from neuralforecast import NeuralForecast
from neuralforecast.models import NBEATS
model = NeuralForecast(models=[NBEATS(input_size=7, h=1, loss="MAPE")])
model.fit(df)
forecast = model.predict()
pytorch-forecasting:
from pytorch_forecasting import TemporalFusionTransformer, TimeSeriesDataSet
training = TimeSeriesDataSet(
train_data,
time_idx="timestamp",
target="target",
group_ids=["id"],
static_categoricals=["category"],
)
model = TemporalFusionTransformer.from_dataset(training)
model.fit(train_dataloader, val_dataloader)
predictions = model.predict(test_dataloader)
Both libraries offer powerful forecasting capabilities, but neuralforecast is more specialized in neural network models, while pytorch-forecasting provides a broader range of forecasting techniques and more extensive data preprocessing options.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PyTorch Forecasting is a PyTorch-based package for forecasting with state-of-the-art deep learning architectures. It provides a high-level API and uses PyTorch Lightning to scale training on GPU or CPU, with automatic logging.
Documentation · Tutorials · Release Notes | |
---|---|
Open Source | |
Community | |
CI/CD | |
Code |
Our article on Towards Data Science introduces the package and provides background information.
PyTorch Forecasting aims to ease state-of-the-art timeseries forecasting with neural networks for real-world cases and research alike. The goal is to provide a high-level API with maximum flexibility for professionals and reasonable defaults for beginners. Specifically, the package provides
- A timeseries dataset class which abstracts handling variable transformations, missing values, randomized subsampling, multiple history lengths, etc.
- A base model class which provides basic training of timeseries models along with logging in tensorboard and generic visualizations such actual vs predictions and dependency plots
- Multiple neural network architectures for timeseries forecasting that have been enhanced for real-world deployment and come with in-built interpretation capabilities
- Multi-horizon timeseries metrics
- Hyperparameter tuning with optuna
The package is built on pytorch-lightning to allow training on CPUs, single and multiple GPUs out-of-the-box.
Installation
If you are working on windows, you need to first install PyTorch with
pip install torch -f https://download.pytorch.org/whl/torch_stable.html
.
Otherwise, you can proceed with
pip install pytorch-forecasting
Alternatively, you can install the package via conda
conda install pytorch-forecasting pytorch -c pytorch>=1.7 -c conda-forge
PyTorch Forecasting is now installed from the conda-forge channel while PyTorch is install from the pytorch channel.
To use the MQF2 loss (multivariate quantile loss), also install
pip install pytorch-forecasting[mqf2]
Documentation
Visit https://pytorch-forecasting.readthedocs.io to read the documentation with detailed tutorials.
Available models
The documentation provides a comparison of available models.
- Temporal Fusion Transformers for Interpretable Multi-horizon Time Series Forecasting which outperforms DeepAR by Amazon by 36-69% in benchmarks
- N-BEATS: Neural basis expansion analysis for interpretable time series forecasting which has (if used as ensemble) outperformed all other methods including ensembles of traditional statical methods in the M4 competition. The M4 competition is arguably the most important benchmark for univariate time series forecasting.
- N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting which supports covariates and has consistently beaten N-BEATS. It is also particularly well-suited for long-horizon forecasting.
- DeepAR: Probabilistic forecasting with autoregressive recurrent networks which is the one of the most popular forecasting algorithms and is often used as a baseline
- Simple standard networks for baselining: LSTM and GRU networks as well as a MLP on the decoder
- A baseline model that always predicts the latest known value
To implement new models or other custom components, see the How to implement new models tutorial. It covers basic as well as advanced architectures.
Usage example
Networks can be trained with the PyTorch Lighning Trainer on pandas Dataframes which are first converted to a TimeSeriesDataSet.
# imports for training
import lightning.pytorch as pl
from lightning.pytorch.loggers import TensorBoardLogger
from lightning.pytorch.callbacks import EarlyStopping, LearningRateMonitor
# import dataset, network to train and metric to optimize
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer, QuantileLoss
from lightning.pytorch.tuner import Tuner
# load data: this is pandas dataframe with at least a column for
# * the target (what you want to predict)
# * the timeseries ID (which should be a unique string to identify each timeseries)
# * the time of the observation (which should be a monotonically increasing integer)
data = ...
# define the dataset, i.e. add metadata to pandas dataframe for the model to understand it
max_encoder_length = 36
max_prediction_length = 6
training_cutoff = "YYYY-MM-DD" # day for cutoff
training = TimeSeriesDataSet(
data[lambda x: x.date <= training_cutoff],
time_idx= ..., # column name of time of observation
target= ..., # column name of target to predict
group_ids=[ ... ], # column name(s) for timeseries IDs
max_encoder_length=max_encoder_length, # how much history to use
max_prediction_length=max_prediction_length, # how far to predict into future
# covariates static for a timeseries ID
static_categoricals=[ ... ],
static_reals=[ ... ],
# covariates known and unknown in the future to inform prediction
time_varying_known_categoricals=[ ... ],
time_varying_known_reals=[ ... ],
time_varying_unknown_categoricals=[ ... ],
time_varying_unknown_reals=[ ... ],
)
# create validation dataset using the same normalization techniques as for the training dataset
validation = TimeSeriesDataSet.from_dataset(training, data, min_prediction_idx=training.index.time.max() + 1, stop_randomization=True)
# convert datasets to dataloaders for training
batch_size = 128
train_dataloader = training.to_dataloader(train=True, batch_size=batch_size, num_workers=2)
val_dataloader = validation.to_dataloader(train=False, batch_size=batch_size, num_workers=2)
# create PyTorch Lighning Trainer with early stopping
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4, patience=1, verbose=False, mode="min")
lr_logger = LearningRateMonitor()
trainer = pl.Trainer(
max_epochs=100,
accelerator="auto", # run on CPU, if on multiple GPUs, use strategy="ddp"
gradient_clip_val=0.1,
limit_train_batches=30, # 30 batches per epoch
callbacks=[lr_logger, early_stop_callback],
logger=TensorBoardLogger("lightning_logs")
)
# define network to train - the architecture is mostly inferred from the dataset, so that only a few hyperparameters have to be set by the user
tft = TemporalFusionTransformer.from_dataset(
# dataset
training,
# architecture hyperparameters
hidden_size=32,
attention_head_size=1,
dropout=0.1,
hidden_continuous_size=16,
# loss metric to optimize
loss=QuantileLoss(),
# logging frequency
log_interval=2,
# optimizer parameters
learning_rate=0.03,
reduce_on_plateau_patience=4
)
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
# find the optimal learning rate
res = Tuner(trainer).lr_find(
tft, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader, early_stop_threshold=1000.0, max_lr=0.3,
)
# and plot the result - always visually confirm that the suggested learning rate makes sense
print(f"suggested learning rate: {res.suggestion()}")
fig = res.plot(show=True, suggest=True)
fig.show()
# fit the model on the data - redefine the model with the correct learning rate if necessary
trainer.fit(
tft, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader,
)
Top Related Projects
Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.
A unified framework for machine learning with time series
A python library for user-friendly forecasting and anomaly detection on time series.
Scalable and user friendly neural :brain: forecasting algorithms.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot