PyTorch-Forecasting A New Time Series Forecasting Library

Time series forecasting plays a vital role in various fields such as finance, weather forecasting, sales forecasting, and demand forecasting. PyTorch-forecasting is an open source Python package built on top of PyTorch, specifically designed to simplify and enhance time series work. In this article we introduce the features and functions of PyTorch-Forecasting, and demonstrate it with sample code.

The installation of PyTorch-Forecasting is very simple:

 pip install pytorch-forecasting

But it should be noted that he currently only supports Pytorch 1.7 and above, but I have not tested whether 2.0 supports it.

PyTorch-Forecasting provides several functions:

1. Provides a high-level interface that abstracts the complexity of time series modeling. A few lines of code can be used to define forecasting tasks, making it easy to experiment with different models and techniques.

2. Multiple prediction models are supported, including autoregressive models (AR, ARIMA), state space models (SARIMAX), neural networks (LSTM, GRU) and ensemble methods (Prophet, N-Beats). This diverse set of models ensures the flexibility to choose the most appropriate method for your time series data.

3. Provide various data preprocessing tools to handle common time series tasks, including: missing value input, scaling, feature extraction and rolling window transformation, etc. In addition to some data preprocessing tools, a Pytorch DS named TimeSeriesDataSet is also provided, which can easily process time series data.

4. Facilitate model evaluation through a unified interface: implement loss functions and verification indicators of time series such as QuantileLoss and SMAPE, and support Pytorch Lighting so that training methods such as early stopping and cross-validation can be used directly

The method of use is also very simple:

 frompytorch_forecastingimportTimeSeriesDataSet, TemporalFusionTransformer
 
 # Load and preprocess the data
 dataset=TimeSeriesDataSet.from_csv('data.csv', target='target', time_idx='time', group_ids=['id'])
 dataset.prepare_training(split_into_train_val_test=[0.8, 0.1, 0.1])
 
 # Initialize and train the model
 model=TemporalFusionTransformer.from_dataset(dataset)
 trainer=pl.Trainer()
 trainer.fit(model, dataset.train_dataloader())
 
 # Generate predictions
 predictions=model.predict(dataset.test_dataloader())
 
 # Evaluate the model
 metric=dataset.target_normalizer.metrics['mse']
 print(f'Test MSE: {metric(predictions, dataset.test_dataloader())}')

If you need classification encoding, you can use it like this:

 frompytorch_forecasting.dataimportGroupNormalizer
 
 # Load and preprocess the data with categorical variables
 dataset=TimeSeriesDataSet.from_pandas(data, target='target', time_idx='time', group_ids=['id'], 
                                         categorical_encoders={'cat_variable': GroupNormalizer()})
 dataset.prepare_training(...)
 
 # Initialize and train the model
 model=TemporalFusionTransformer.from_dataset(dataset)
 trainer.fit(model, dataset.train_dataloader())
 
 # Generate predictions
 predictions=model.predict(dataset.test_dataloader())
 
 # Evaluate the model
 print(f'Test MSE: {metric(predictions, dataset.test_dataloader())}')

PyTorch-Forecasting is a very useful toolkit. Even if you don’t use all its functions, you can use some of the functions it provides as tools to integrate into your own projects. If you are interested in using PyTorch to process time series data If you are interested, you can also take a look at his code as a reference for learning. His documentation is relatively comprehensive and provides many examples.

If you are interested, take a look at the official documentation and code samples:

https://avoid.overfit.cn/post/26c1ce20c45a46e181c6ee74eccfc0fa

Guess you like

Origin blog.csdn.net/m0_46510245/article/details/130880694