commonpower.data_forecasting.nn_forecasting.nn_forecasting.NNForecaster

class NNForecaster(model_class: type, targets: list[str], frequency: ~datetime.timedelta = datetime.timedelta(seconds=3600), horizon: ~datetime.timedelta = datetime.timedelta(seconds=43200), feature_transform: ~commonpower.data_forecasting.nn_forecasting.transform.Transformation = <commonpower.data_forecasting.nn_forecasting.transform.IdentityTransform object>, target_transform: ~commonpower.data_forecasting.nn_forecasting.transform.Transformation = <commonpower.data_forecasting.nn_forecasting.transform.IdentityTransform object>)[source]

Bases: Forecaster

Neural-Network-based Forecaster.

All featues of the data source (including targets) will be used as model inputs. We make the assumption that all features besides the targets are static in the sense that they are available across the entire forecast horizon (e.g. time features). This is is necessary to apply the model iteratively. If this assumption cannot reasonably made in practice, the model output must cover the entire horizon in one step.

When the forecaster is deployed, we assume that the targets are the first “columns” of the data source.

Parameters:
  • model_class (NNModule.__class__) – Model class.

  • targets (list[str]) – Target variables.

  • frequency (timedelta, optional) – Frequency of the data. Defaults to timedelta(hours=1).

  • horizon (timedelta, optional) – Forecast horizon. Defaults to timedelta(hours=12).

  • feature_transform (Transformation, optional) – Feature transformation. Defaults to IdentityTransform().

  • target_transform (Transformation, optional) – Target transformation. Defaults to IdentityTransform().

Methods

get_train_val_loaders

Return training and validation data loaders.

setup

Setup the forecaster for training.

with_model

This can be called to pass an already trained model to the forecaster.

Attributes

input_range

Returns the min and max timedelta of observations which are required for the prediction.

is_uncertain

look_back

__call__(data: ndarray) ndarray[source]

Make a prediction. If the model prediction horizon (steps ahead) are less than the forecast horizon, we iteratively apply the model to make predictions covering the entire horizon. For this to work, the model feature and target variables must be identical.

Parameters:

data (ndarray) – Input data. Expected shape: (N, n_features).

Returns:

ndarray – Forecasted values. Shape: (N, n_targets).

get_train_val_loaders(data_source: DataSource, param_space: ParameterSpace, dataset_wrapper_class: DatasetWrapper.__class__ = <class 'commonpower.data_forecasting.nn_forecasting.dataset_wrappers.NStepAhead'>, dataset_split_class: DatasetSplit.__class__ = <class 'commonpower.data_forecasting.nn_forecasting.data_splitting.SimpleFractionalSplit'>) tuple[DataLoader, DataLoader][source]

Return training and validation data loaders.

Parameters:
  • data_source (DataSource) – Data source.

  • param_space (ParameterSpace) – Parameter space for the forecaster.

  • dataset_wrapper_class (DatasetWrapper.__class__, optional) – Dataset wrapper class. Defaults to NStepAhead.

  • dataset_split_class (DatasetSplit.__class__, optional) – Dataset split class. Defaults to SimpleFractionalSplit.

Returns:

tuple[DataLoader, DataLoader] – Training and validation data loaders.

property input_range: tuple[timedelta]

Returns the min and max timedelta of observations which are required for the prediction. To indicate a timestamp before the current time, the timedelta must be negative.

Returns:

tuple[timedelta] – (td before, td after)

setup(data_source: DataSource, param_space: ParameterSpace) NNForecaster[source]

Setup the forecaster for training. This is usually called from the NNTrainer. This means anything passed to the setup method can be tuned. Here, we check some model dimensions and fit the transformations.

Parameters:
  • data_source (DataSource) – Data source.

  • param_space (ParameterSpace) – Parameter space for the forecaster.

Returns:

NNForecaster – The setup forecaster.

with_model(model: NNModule) NNForecaster[source]

This can be called to pass an already trained model to the forecaster. We expect the transformations passed in the contructor to be already fitted.

Parameters:

model (NNModule) – Forecast model.