Die Pytorch-Prognose ist ein Pytorch-basierter Paket für die Prognose mit hochmodernen Deep-Learning-Architekturen. Es bietet eine API auf hoher Ebene und verwendet Pytorch Lightning, um das Training an GPU oder CPU mit automatischer Protokollierung zu skalieren.
| Dokumentation · Tutorials · Versionshinweise | |
|---|---|
| Open Source | |
| Gemeinschaft | |
| CI/CD | |
| Code |
Unser Artikel über in Richtung Data Science stellt das Paket vor und bietet Hintergrundinformationen.
Die Prognose von Pytorch zielt darauf ab, hochmoderne Timeseries-Prognosen mit neuronalen Netzwerken für reale Fälle und Forschungen gleichermaßen zu erleichtern. Ziel ist es, Fachleuten eine hochrangige API mit maximaler Flexibilität und angemessenen Ausfällen für Anfänger zu bieten. Insbesondere bietet das Paket
Das Paket basiert auf Pytorch-Lightning, damit das Training über CPUs, Einzel- und Mehrfach-GPUs außerhalb des Boxs trainiert werden kann.
Wenn Sie an Windows arbeiten, müssen Sie zuerst Pytorch mit installieren
pip install torch -f https://download.pytorch.org/whl/torch_stable.html .
Andernfalls können Sie mit fortfahren
pip install pytorch-forecasting
Alternativ können Sie das Paket über Conda installieren
conda install pytorch-forecasting pytorch -c pytorch>=1.7 -c conda-forge
Die Pytorch-Vorhersage wird jetzt aus dem Conda-Forge-Kanal installiert, während Pytorch aus dem Pytorch-Kanal installiert wird.
Um den MQF2-Verlust (multivariatem Quantilverlust) zu verwenden, installieren Sie auch pip install pytorch-forecasting[mqf2]
Besuchen Sie https://pytorch-forecasting.readthedocs.io, um die Dokumentation mit detaillierten Tutorials zu lesen.
Die Dokumentation bietet einen Vergleich der verfügbaren Modelle.
Um neue Modelle oder andere benutzerdefinierte Komponenten zu implementieren, finden Sie im Tutorial für das Implementieren neuer Modelle. Es deckt sowohl grundlegende als auch fortschrittliche Architekturen ab.
Netzwerke können mit dem Pytorch Ligbing Trainer auf PANDAS -Datenframes ausgebildet werden, die zuerst in ein TimeseriesDataset umgewandelt werden.
# 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 ,
)