autopycoin.models.PoolNBEATS#

class autopycoin.models.PoolNBEATS(*args, **kwargs)[source]#

Tensorflow model defining a pool of N-BEATS models.

As described in the paper https://arxiv.org/abs/1905.10437, the state-of-the-art results are reached with a bagging method of N-BEATS models including interpretable and generic ones.

The aggregation function is used in predict method if it is possible, i.e when the outputs shape are not differents. As the reconstructed inputs are masked randomly the aggregation is not perfomed on them.

Fore more information about poll model see autopycoin.models.Pool.

Parameters
label_widthint

Width of the targets. It can be not defined if nbeats_model is a list of NBEATS instances. Default to None.

n_modelsint

Number of models inside the pool. The minimum value according to the paper to get SOTA results is 18. If NBEATS instances are provided then n_models is not used. Default to 18.

nbeats_modelslist[callable()] or list[NBEATS]

A list of callables which create a NBEATS model or a list of autopycoin.models.NBEATS instances. If None then use a mix of generic and interpretable NBEATs model. Default to None.

fn_aggCallable

Function of aggregation which takes an parameter axis. It aggregates the models outputs. Default to mean.

seed: int

Used in combination with tf.random.set_seed to create a reproducible sequence of tensors across multiple calls.

Returns
outputsTuple[Tuple[Tensor | QuantileTensor | UnivariateTensor], Tuple[Tensor | QuantileTensor | UnivariateTensor]]

Return the reconstructed inputs and inferred outputs as tuple (reconstructed inputs, outputs). Reconstructed inputs is a tuple of tensors as the mask is not the same through models. Outputs can be a tuple of tensors or an aggregated tensor if the prediction is used through predict method.

Notes

PoolNBEATS is just a wrapper around nbeats models hence you can use epistemic loss error or multivariates inputs. This class only applies mask to its inputs.

Input shape N-D tensor with shape: (batch_size, time step, variables) or (batch_size, time step). The most common situation would be a 2D input with shape (batch_size, time step).

Output shape n N-D tensors with shape: (batch_size, time step, variables, quantiles) or (batch_size, time step, quantiles) or (batch_size, time step) with n the number of models generated randomly or registered in the constructor.

For instance, for a 2D input with shape (batch_size, units) and three models, the output would have shape (((batch_size, units), (batch_size, units), (batch_size, units)), ((batch_size, units), (batch_size, units), (batch_size, units))) if call is used else (((batch_size, units), (batch_size, units), (batch_size, units)), (batch_size, units)) if predict is used.

The outputs tensors can be aggregated during predict method only if all tensors are similar in shape.

Examples

>>> from autopycoin.data import random_ts
>>> from autopycoin.models import PoolNBEATS, create_interpretable_nbeats
>>> from autopycoin.dataset import WindowGenerator
>>> import tensorflow as tf
>>> import pandas as pd
...
>>> data = random_ts(n_steps=1000,
...                  trend_degree=2,
...                  periods=[10],
...                  fourier_orders=[10],
...                  trend_mean=0,
...                  trend_std=1,
...                  seasonality_mean=0,
...                  seasonality_std=1,
...                  batch_size=1,
...                  n_variables=1,
...                  noise=True,
...                  seed=42)
>>> data = pd.DataFrame(data[0].numpy(), columns=['test'])
...
>>> w = WindowGenerator(
...        input_width=70,
...        label_width=10,
...        shift=10,
...        test_size=50,
...        valid_size=10,
...        flat=True,
...        batch_size=32,
...        preprocessing=lambda x,y: (x, (x, y))
...     )
...
>>> w = w.from_array(data=data,
...        input_columns=['test'],
...        label_columns=['test'])
...
>>> model = PoolNBEATS(
...             label_width=10,
...             n_models=2,
...             nbeats_models=create_interpretable_nbeats,
...             )
>>> model.compile(tf.keras.optimizers.Adam(
...    learning_rate=0.015, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=True,
...    name='Adam'), loss=[['mse', 'mse'], ['mae', 'mae'], ['mape', 'mape']], metrics=['mae'])
>>> history = model.fit(w.train, validation_data=w.valid, epochs=1, verbose=0)
>>> model.predict(w.test.take(1))[1].shape
(32, 10)
Attributes
seeclass:autopycoin.models.Pool
check_valid_structure(structure, name)[source]#

Check if loss and loss_weights are list of lists or a list of two elements.

checks(nbeats_models: List[autopycoin.models.nbeats.NBEATS]) None[source]#

Check if label_width are equals through models instances.

compile(optimizer='rmsprop', loss=None, metrics=None, loss_weights=[0.0, 1.0], weighted_metrics=None, run_eagerly=None, steps_per_execution=None, **kwargs) None[source]#

Compiles models one by one for training.

See tensorflow documentation for more informations.

postprocessing_y(y)[source]#

Apply mask inside predict_step

preprocessing_x(x: Union[None, tensorflow.python.framework.ops.Tensor, tensorflow.python.data.ops.dataset_ops.DatasetV2, Tuple[tensorflow.python.framework.ops.Tensor, ...]]) Union[Tuple[None, None], Tuple[Callable, tuple]][source]#

Apply mask inside train_step

preprocessing_y(y: Union[None, tensorflow.python.framework.ops.Tensor, tensorflow.python.data.ops.dataset_ops.DatasetV2, Tuple[tensorflow.python.framework.ops.Tensor, ...]]) Union[Tuple[None, None], Tuple[Callable, tuple]][source]#

Apply mask inside train_step, test_step