Skip to content

NeuralForecastLSTM

NeuralForecastLSTM

class NeuralForecastLSTM(freq: str | int = 'auto', local_scaler_type: Literal['standard', 'robust', 'robust-iqr', 'minmax', 'boxcox'] | None = None, futr_exog_list: list[str] | None = None, verbose_fit: bool = False, verbose_predict: bool = False, input_size: int = -1, inference_input_size: int = -1, encoder_n_layers: int = 2, encoder_hidden_size: int = 200, encoder_bias: bool = True, encoder_dropout: float = 0.0, context_size: int = 10, decoder_hidden_size: int = 200, decoder_layers: int = 2, loss=None, valid_loss=None, max_steps: int = 1000, learning_rate: float = 0.001, num_lr_decays: int = -1, early_stop_patience_steps: int = -1, val_check_steps: int = 100, batch_size=32, valid_batch_size: int | None = None, scaler_type: str = 'robust', random_seed=1, num_workers_loader=0, drop_last_loader=False, trainer_kwargs: dict | None = None, optimizer=None, optimizer_kwargs: dict | None = None, broadcasting: bool = False, lr_scheduler=None, lr_scheduler_kwargs: dict | None = None)[source]

NeuralForecast LSTM model.

Interface to neuralforecast.models.LSTM [1] through neuralforecast.NeuralForecast [2], from neuralforecast [3] by Nixtla.

The Long Short-Term Memory Recurrent Neural Network (LSTM), uses a multilayer LSTM encoder and an MLP decoder.

Parameters:
freqUnion[str, int] (default=”auto”)

frequency of the data, see available frequencies [4] from pandas use int freq when using RangeIndex in y

default (“auto”) interprets freq from ForecastingHorizon in fit

local_scaler_typestr (default=None)

scaler to apply per-series to all features before fitting, which is inverted after predicting

can be one of the following:

  • ‘standard’

  • ‘robust’

  • ‘robust-iqr’

  • ‘minmax’

  • ‘boxcox’

futr_exog_liststr list, (default=None)

future exogenous variables

verbose_fitbool (default=False)

print processing steps during fit

verbose_predictbool (default=False)

print processing steps during predict

input_sizeint (default=-1)

maximum sequence length for truncated train backpropagation

default (-1) uses all history

inference_input_sizeint (default=-1)

maximum sequence length for truncated inference

default (-1) uses all history

encoder_n_layersint (default=2)

number of layers for the LSTM

encoder_hidden_sizeint (default=200)

units for the LSTM hidden state size

encoder_biasbool (default=True)

whether or not to use biases b_ih, b_hh within LSTM units

encoder_dropoutfloat (default=0.0)

dropout regularization applied to LSTM outputs

context_sizeint (default=10)

size of context vector for each timestamp on the forecasting window

decoder_hidden_sizeint (default=200)

size of hidden layer for the MLP decoder

decoder_layersint (default=2)

number of layers for the MLP decoder

losspytorch module (default=None)

instantiated train loss class from losses collection [5]

valid_losspytorch module (default=None)

instantiated validation loss class from losses collection [5]

max_stepsint (default=1000)

maximum number of training steps

learning_ratefloat (default=1e-3)

learning rate between (0, 1)

num_lr_decaysint (default=-1)

number of learning rate decays, evenly distributed across max_steps

early_stop_patience_stepsint (default=-1)

number of validation iterations before early stopping

val_check_stepsint (default=100)

number of training steps between every validation loss check

batch_sizeint (default=32)

number of different series in each batch

valid_batch_sizeOptional[int] (default=None)

number of different series in each validation and test batch

scaler_typestr (default=”robust”)

type of scaler for temporal inputs normalization

random_seedint (default=1)

random_seed for pytorch initializer and numpy generators

num_workers_loaderint (default=0)

workers to be used by TimeSeriesDataLoader

drop_last_loaderbool (default=False)

whether TimeSeriesDataLoader drops last non-full batch

trainer_kwargsdict (default=None)

keyword trainer arguments inherited from PyTorch Lighning’s trainer [6]

optimizerpytorch optimizer (default=None) [7]

optimizer to use for training, if passed with None defaults to Adam

optimizer_kwargsdict (default=None) [8]

dict of parameters to pass to the user defined optimizer

broadcastingbool (default=False)

if True, a model will be fit per time series. Panels, e.g., multiindex data input, will be broadcasted to single series, and for each single series, one copy of this forecaster will be applied.

lr_schedulerpytorch learning rate scheduler (default=None) [9]

user specified lr_scheduler instead of the default choice StepLR [10]

lr_scheduler_kwargsdict (default=None)

list of parameters used by the user specified lr_scheduler

Attributes:
algorithm_class

Import underlying NeuralForecast algorithm class.

algorithm_exogenous_support

Set support for exogenous features.

algorithm_name

Set custom model name.

algorithm_parameters

Get keyword parameters for the underlying NeuralForecast algorithm class.

dict

keyword arguments for the underlying algorithm class

cutoff

Cut-off = “present time” state of forecaster.

fh

Forecasting horizon that was passed.

is_fitted

Whether fit has been called.

state

State of the estimator.

Notes

  • If loss is unspecified, MAE is used as the loss function for training.

  • Only futr_exog_list will be considered as exogenous variables.

References

Examples

>>>
>>> # importing necessary libraries
>>> from sktime.datasets import load_longley
>>> from sktime.forecasting.neuralforecast import NeuralForecastLSTM
>>> from sktime.split import temporal_train_test_split
>>>
>>> # loading the Longley dataset and splitting it into train and test subsets
>>> y, X = load_longley()
>>> y_train, y_test, X_train, X_test = temporal_train_test_split(y, X, test_size=4)
>>>
>>> # creating model instance configuring the hyperparameters
>>> model = NeuralForecastLSTM(
...     "A-DEC", futr_exog_list=["ARMED", "POP"], max_steps=5
... )
>>>
>>> # fitting the model
>>> model.fit(y_train, X=X_train, fh=[1, 2, 3, 4])
Seed set to 1
Epoch 4: 100%|█| 1/1 [00:00<00:00, 42.85it/s, v_num=870, train_loss_step=0.589, train_loss_epoc
NeuralForecastLSTM(freq='A-DEC', futr_exog_list=['ARMED', 'POP'], max_steps=5)
>>>
>>> # getting point predictions
>>> model.predict(X=X_test)
Predicting DataLoader 0: 100%|██████████████████████████████████| 1/1 [00:00<00:00, 198.64it/s]
1959    64083.226562
1960    64426.304688
1961    64754.886719
1962    64889.496094
Freq: A-DEC, Name: TOTEMP, dtype: float64
>>>

Methods

check_is_fitted([method_name])

Check if the estimator has been fitted.

clone()

Obtain a clone of the object with same hyper-parameters and config.

clone_tags(estimator[, tag_names])

Clone tags from another object as dynamic override.

create_test_instance([parameter_set])

Construct an instance of the class, using first test parameter set.

create_test_instances_and_names([parameter_set])

Create list of all test instances and a list of names for them.

fit(y[, X, fh])

Fit forecaster to training data.

fit_predict(y[, X, fh, X_pred])

Fit and forecast time series at future horizon.

get_class_tag(tag_name[, tag_value_default])

Get class tag value from class, with tag level inheritance from parents.

get_class_tags()

Get class tags from class, with tag level inheritance from parent classes.

get_config()

Get config flags for self.

get_fitted_params([deep])

Get fitted parameters.

get_param_defaults()

Get object's parameter defaults.

get_param_names([sort])

Get object's parameter names.

get_params([deep])

Get a dict of parameters values for this object.

get_pretrained_params([deep])

Get pretrained parameters of this estimator.

get_tag(tag_name[, tag_value_default, ...])

Get tag value from instance, with tag level inheritance and overrides.

get_tags()

Get tags from instance, with tag level inheritance and overrides.

get_test_params([parameter_set])

Return testing parameter settings for the estimator.

is_composite()

Check if the object is composed of other BaseObjects.

load_from_path(serial)

Load object from file location.

load_from_serial(serial)

Load object from serialized memory container.

predict([fh, X])

Forecast time series at future horizon.

predict_interval([fh, X, coverage])

Compute/return prediction interval forecasts.

predict_proba([fh, X, marginal])

Compute/return fully probabilistic forecasts.

predict_quantiles([fh, X, alpha])

Compute/return quantile forecasts.

predict_residuals([y, X])

Return residuals of time series forecasts.

predict_var([fh, X, cov])

Compute/return variance forecasts.

pretrain(y[, X, fh])

Pre-train forecaster on panel (global) data.

reset()

Reset the object to a clean post-init state.

save([path, serialization_format])

Save serialized self to bytes-like object or to (.zip) file.

score(y[, X, fh])

Scores forecast against ground truth, using MAPE (non-symmetric).

set_config(**config_dict)

Set config flags to given values.

set_params(**params)

Set the parameters of this object.

set_random_state([random_state, deep, ...])

Set random_state pseudo-random seed parameters for self.

set_tags(**tag_dict)

Set instance level tag overrides to given values.

update(y[, X, update_params])

Update cutoff value and, optionally, fitted parameters.

update_predict(y[, cv, X, update_params, ...])

Make predictions and update model iteratively over the test set.

update_predict_single([y, fh, X, update_params])

Update model with new data and make forecasts.