NBeats#
- class pytorch_forecasting.models.nbeats._nbeats.NBeats(stack_types: list[str] | None = None, num_blocks: list[int] | None = None, num_block_layers: list[int] | None = None, widths: list[int] | None = None, sharing: list[bool] | None = None, expansion_coefficient_lengths: list[int] | None = None, prediction_length: int = 1, context_length: int = 1, dropout: float = 0.1, learning_rate: float = 0.01, log_interval: int = -1, log_gradient_flow: bool = False, log_val_interval: int = None, weight_decay: float = 0.001, loss: MultiHorizonMetric = None, reduce_on_plateau_patience: int = 1000, backcast_loss_ratio: float = 0.0, logging_metrics: ModuleList = None, **kwargs)[source]#
Bases:
NBeatsAdapterInitialize NBeats Model - use its
from_dataset()method if possible.Based on the article `N-BEATS: Neural basis expansion analysis for interpretable time series
forecasting <http://arxiv.org/abs/1905.10437>`_. The network 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.
The
NHiTSnetwork has recently shown to consistently outperform N-BEATS.- Parameters:
stack_types (list of str) – One of the following values “generic”, “seasonality” or “trend”. A list of strings of length 1 or num_stacks. Default and recommended value for generic mode is [“generic”]. Recommended value for interpretable mode is [“trend”,”seasonality”].
num_blocks (list of int) – The number of blocks per stack. Length 1 or num_stacks. Default for generic mode is [1], interpretable mode is [3].
num_block_layers (list of int) – Number of fully connected layers with ReLU activation per block. Length 1 or num_stacks. Default [4] for both modes.
width (list of int) – Widths of fully connected layers with ReLU activation. List length 1 or num_stacks. Default [512] for generic; [256, 2048] for interpretable.
sharing (list of bool) – Whether weights are shared across blocks in a stack. List length 1 or num_stacks. Default [False] for generic; [True] for interpretable.
expansion_coefficient_length (list of int) – If type is “G”, length of expansion coefficient; if “T”, degree of polynomial; if “S”, minimum period (e.g., 2 for every timestep). List length 1 or num_stacks. Default [32] for generic; [3] for interpretable.
prediction_length (int) – Length of the forecast horizon.
context_length (int) – Number of time units conditioning the predictions (lookback period). Should be between 1-10x prediction_length.
dropout (float) – Dropout probability applied in the network. Helps prevent overfitting. Default is 0.1.
learning_rate (float) – Learning rate used by the optimizer during training. Default is 1e-2.
log_interval (int) – Interval (in steps) at which training logs are recorded. If -1, logging is disabled. Default is -1.
log_gradient_flow (bool) – Whether to log gradient flow during training. Useful for diagnosing vanishing/exploding gradients. Default is False.
log_val_interval (int) – Interval (in steps) at which validation metrics are logged. If None, uses default logging behavior. Default is None.
weight_decay (float) – Weight decay (L2 regularization) coefficient used by the optimizer to reduce overfitting. Default is 1e-3.
loss – Loss to optimize. Defaults to MASE().
reduce_on_plateau_patience (int) – Patience after which learning rate is reduced by factor of 10.
backcast_loss_ratio (float) – Weight of backcast loss relative to forecast loss. 1.0 gives equal weight; default 0.0 means no backcast loss.
logging_metrics (nn.ModuleList of MultiHorizonMetric) – List of metrics logged during training. Defaults to nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]).
**kwargs – Additional arguments forwarded to
BaseModel.
BaseModel for timeseries forecasting from which to inherit from
- Parameters:
log_interval (Union[int, float], optional) – Batches after which predictions are logged. If < 1.0, will log multiple entries per batch. Defaults to -1.
log_val_interval (Union[int, float], optional) – batches after which predictions for validation are logged. Defaults to None/log_interval.
learning_rate (float, optional) – Learning rate. Defaults to 1e-3.
log_gradient_flow (bool) – If to log gradient flow, this takes time and should be only done to diagnose training failures. Defaults to False.
loss (Metric, optional) – metric to optimize, can also be list of metrics. Defaults to SMAPE().
logging_metrics (nn.ModuleList[MultiHorizonMetric]) – list of metrics that are logged during training. Defaults to [].
reduce_on_plateau_patience (int) – patience after which learning rate is reduced by a factor of 10. Defaults to 1000
reduce_on_plateau_reduction (float) – reduction in learning rate when encountering plateau. Defaults to 2.0.
reduce_on_plateau_min_lr (float) – minimum learning rate for reduce on plateau learning rate scheduler. Defaults to 1e-5
weight_decay (float) – weight decay. Defaults to 0.0.
optimizer_params (Dict[str, Any]) – additional parameters for the optimizer. Defaults to {}.
monotone_constraints (Dict[str, int]) – dictionary of monotonicity constraints for continuous decoder variables mapping position (e.g.
"0"for first position) to constraint (-1for negative and+1for positive, larger numbers add more weight to the constraint vs. the loss but are usually not necessary). This constraint significantly slows down training. Defaults to {}.output_transformer (Callable) – transformer that takes network output and transforms it to prediction space. Defaults to None which is equivalent to
lambda out: out["prediction"].optimizer (str) – Optimizer, “ranger”, “sgd”, “adam”, “adamw” or class name of optimizer in
torch.optimorpytorch_optimizer. Alternatively, a class or function can be passed which takes parameters as first argument and a lr argument (optionally also weight_decay). Defaults to “adam”.