La prévision de Pytorch est un package basé sur Pytorch pour les prévisions avec des architectures d'apprentissage en profondeur de pointe. Il fournit une API de haut niveau et utilise Pytorch Lightning pour évoluer une formation sur GPU ou CPU, avec journalisation automatique.
| Documentation · Tutoriels · Notes de publication | |
|---|---|
| Open source | |
| Communauté | |
| CI / CD | |
| Code |
Notre article sur Data Science présente le package et fournit des informations générales.
Les prévisions de Pytorch visent à atténuer les prévisions de temps en temps de pointe avec des réseaux de neurones pour les cas et la recherche du monde réel. L'objectif est de fournir une API de haut niveau avec une flexibilité maximale pour les professionnels et des défauts raisonnables pour les débutants. Plus précisément, le package fournit
Le package est construit sur Pytorch-Lightning pour permettre une formation sur des processeurs, des GPU simples et multiples prêts à l'emploi.
Si vous travaillez sur Windows, vous devez d'abord installer Pytorch avec
pip install torch -f https://download.pytorch.org/whl/torch_stable.html .
Sinon, vous pouvez procéder avec
pip install pytorch-forecasting
Alternativement, vous pouvez installer le package via conda
conda install pytorch-forecasting pytorch -c pytorch>=1.7 -c conda-forge
La prévision de Pytorch est désormais installée à partir du canal Conda-Forge tandis que Pytorch est installé à partir du canal Pytorch.
Pour utiliser la perte MQF2 (perte quantile multivariée), installez également pip install pytorch-forecasting[mqf2]
Visitez https://pytorch-forcasting.readthedocs.io pour lire la documentation avec des tutoriels détaillés.
La documentation fournit une comparaison des modèles disponibles.
Pour implémenter de nouveaux modèles ou d'autres composants personnalisés, consultez le tutoriel How To Implexe New Model. Il couvre les architectures de base et avancées.
Les réseaux peuvent être formés avec l'entraîneur de Lighnnning Pytorch sur Pandas Dataframes qui sont d'abord convertis en un TimesSeriesDataset.
# 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 ,
)