Skip to main content

NBEATS
Neural basis expansion analysis for interpretable time series forecasting

Tensorflow/Pytorch implementation | Paper | Results

NBeats CI


Outputs of the generic and interpretable layers

Installation

It is possible to install the two backends at the same time.

From PyPI

Install the Tensorflow/Keras backend: pip install nbeats-keras

NBEATS - Keras - Downloads

Install the Pytorch backend: pip install nbeats-pytorch

NBEATS - PyTorch - Downloads

From the sources

Installation is based on a MakeFile.

Command to install N-Beats with Keras: make install-keras

Command to install N-Beats with Pytorch: make install-pytorch

Run on the GPU

It is possible that this is no longer necessary on the recent versions of Tensorflow. To force the utilization of the GPU (with the Keras backend), run: pip uninstall -y tensorflow && pip install tensorflow-gpu.

Example

Here is an example to get familiar with both backends. Note that only the Keras backend supports input_dim>1 at the moment.

import warnings

import numpy as np

from nbeats_keras.model import NBeatsNet as NBeatsKeras
from nbeats_pytorch.model import NBeatsNet as NBeatsPytorch

warnings.filterwarnings(action='ignore', message='Setting attributes')


def main():
    # https://keras.io/layers/recurrent/
    # At the moment only Keras supports input_dim > 1. In the original paper, input_dim=1.
    num_samples, time_steps, input_dim, output_dim = 50_000, 10, 1, 1

    # This example is for both Keras and Pytorch. In practice, choose the one you prefer.
    for BackendType in [NBeatsKeras, NBeatsPytorch]:
        # NOTE: If you choose the Keras backend with input_dim>1, you have 
        # to set the value here too (in the constructor).
        backend = BackendType(
            backcast_length=time_steps, forecast_length=output_dim,
            stack_types=(NBeatsKeras.GENERIC_BLOCK, NBeatsKeras.GENERIC_BLOCK),
            nb_blocks_per_stack=2, thetas_dim=(4, 4), share_weights_in_stack=True,
            hidden_layer_units=64
        )

        # Definition of the objective function and the optimizer.
        backend.compile(loss='mae', optimizer='adam')

        # Definition of the data. The problem to solve is to find f such as | f(x) - y | -> 0.
        # where f = np.mean.
        x = np.random.uniform(size=(num_samples, time_steps, input_dim))
        y = np.mean(x, axis=1, keepdims=True)

        # Split data into training and testing datasets.
        c = num_samples // 10
        x_train, y_train, x_test, y_test = x[c:], y[c:], x[:c], y[:c]
        test_size = len(x_test)

        # Train the model.
        print('Training...')
        backend.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128)

        # Save the model for later.
        backend.save('n_beats_model.h5')

        # Predict on the testing set (forecast).
        predictions_forecast = backend.predict(x_test)
        np.testing.assert_equal(predictions_forecast.shape, (test_size, backend.forecast_length, output_dim))

        # Predict on the testing set (backcast).
        predictions_backcast = backend.predict(x_test, return_backcast=True)
        np.testing.assert_equal(predictions_backcast.shape, (test_size, backend.backcast_length, output_dim))

        # Load the model.
        model_2 = BackendType.load('n_beats_model.h5')

        np.testing.assert_almost_equal(predictions_forecast, model_2.predict(x_test))


if __name__ == '__main__':
    main()

Browse the examples for more. It includes Jupyter notebooks.

Jupyter notebook: NBeats.ipynb: make run-jupyter.


Citation

@misc{NBeatsPRemy,
  author = {Philippe Remy},
  title = {N-BEATS: Neural basis expansion analysis for interpretable time series forecasting},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/philipperemy/n-beats}},
}

Contributors

Thank you!

Metadata

Release files for nbeats-pytorch 1.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbeats-pytorch 1.8.0
File Size Uploaded
nbeats-pytorch-1.8.0.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nbeats-pytorch 1.8.0
File Interpreter ABI Platform
nbeats_pytorch-1.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / nbeats-pytorch-1.8.0.tar.gz

Download URL nbeats-pytorch-1.8.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7a4e18ad7d4e61dcfe8a5a47ce3bf88c8970a2cd7c378499a3dfb8dd6084d838
BLAKE2b-256 checksum
How to use checksums
9c78f6464cfd436a07bc83fbdf15dd79f3122f17a459bb45aba2cfda0fb20334
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.0

Release files / nbeats_pytorch-1.8.0-py3-none-any.whl

Download URL nbeats_pytorch-1.8.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a23bca2026ad71d5aedcfa7386688252ebe7cb63a1d71952f178e1d2cadff68e
BLAKE2b-256 checksum
How to use checksums
108f44518c29d725405c03e13425a9d701858d156420085e4c7828afbcbcc69a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.0

Release history Release notifications | RSS feed

This release

1.8.0 This release

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page