{"id":13738190,"url":"https://github.com/philipperemy/n-beats","last_synced_at":"2025-05-15T11:08:40.818Z","repository":{"id":36164515,"uuid":"198568164","full_name":"philipperemy/n-beats","owner":"philipperemy","description":"Keras/Pytorch implementation of N-BEATS: Neural basis expansion analysis for interpretable time series forecasting.","archived":false,"fork":false,"pushed_at":"2023-03-03T02:21:29.000Z","size":219372,"stargazers_count":878,"open_issues_count":0,"forks_count":166,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-14T19:58:58.942Z","etag":null,"topics":["deep-learning","neural-networks","pytorch","series-forecasting"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philipperemy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["philipperemy"]}},"created_at":"2019-07-24T05:59:51.000Z","updated_at":"2025-04-01T07:08:12.000Z","dependencies_parsed_at":"2023-02-18T03:00:20.498Z","dependency_job_id":"e9de010e-3da2-4f0d-bc17-21822133418a","html_url":"https://github.com/philipperemy/n-beats","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Fn-beats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Fn-beats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Fn-beats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Fn-beats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philipperemy","download_url":"https://codeload.github.com/philipperemy/n-beats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254328385,"owners_count":22052632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","neural-networks","pytorch","series-forecasting"],"created_at":"2024-08-03T03:02:13.788Z","updated_at":"2025-05-15T11:08:40.798Z","avatar_url":"https://github.com/philipperemy.png","language":"Python","funding_links":["https://github.com/sponsors/philipperemy"],"categories":["Python"],"sub_categories":[],"readme":"## NBEATS\u003cbr/\u003eNeural basis expansion analysis for interpretable time series forecasting\n\nTensorflow/Pytorch implementation | [Paper](https://arxiv.org/abs/1905.10437)\n| [Results](https://github.com/fecet/NBeats-M4)\n\n![NBeats CI](https://github.com/philipperemy/n-beats/workflows/N%20Beats%20CI/badge.svg?branch=master)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/interpretable.png\"\u003e\u003cbr/\u003e\n  \u003ci\u003eOutputs of the generic and interpretable layers of NBEATS\u003c/i\u003e\n\u003c/p\u003e\n\n### Installation\n\nIt is possible to install the two backends at the same time.\n\n#### From PyPI\n\nInstall the Tensorflow/Keras backend: `pip install nbeats-keras`\n\n[![NBEATS - Keras - Downloads](https://pepy.tech/badge/nbeats-keras)](https://pepy.tech/project/nbeats-keras)\n\nInstall the Pytorch backend: `pip install nbeats-pytorch`\n\n[![NBEATS - PyTorch - Downloads](https://pepy.tech/badge/nbeats-pytorch)](https://pepy.tech/project/nbeats-pytorch)\n\n#### From the sources\n\nInstallation is based on a MakeFile.\n\nCommand to install N-Beats with Keras: `make install-keras`\n\nCommand to install N-Beats with Pytorch: `make install-pytorch`\n\n#### Run on the GPU\n\nThis trick is no longer necessary on the recent versions of Tensorflow.  To force the utilization of the GPU (with the Keras backend),\nrun: `pip uninstall -y tensorflow \u0026\u0026 pip install tensorflow-gpu`. \n\n### Example\n\nHere is an example to get familiar with both backends. Note that only the Keras backend supports `input_dim\u003e1` at the moment.\n\n```python\nimport warnings\n\nimport numpy as np\n\nfrom nbeats_keras.model import NBeatsNet as NBeatsKeras\nfrom nbeats_pytorch.model import NBeatsNet as NBeatsPytorch\n\nwarnings.filterwarnings(action='ignore', message='Setting attributes')\n\n\ndef main():\n    # https://keras.io/layers/recurrent/\n    # At the moment only Keras supports input_dim \u003e 1. In the original paper, input_dim=1.\n    num_samples, time_steps, input_dim, output_dim = 50_000, 10, 1, 1\n\n    # This example is for both Keras and Pytorch. In practice, choose the one you prefer.\n    for BackendType in [NBeatsKeras, NBeatsPytorch]:\n        # NOTE: If you choose the Keras backend with input_dim\u003e1, you have \n        # to set the value here too (in the constructor).\n        backend = BackendType(\n            backcast_length=time_steps, forecast_length=output_dim,\n            stack_types=(NBeatsKeras.GENERIC_BLOCK, NBeatsKeras.GENERIC_BLOCK),\n            nb_blocks_per_stack=2, thetas_dim=(4, 4), share_weights_in_stack=True,\n            hidden_layer_units=64\n        )\n\n        # Definition of the objective function and the optimizer.\n        backend.compile(loss='mae', optimizer='adam')\n\n        # Definition of the data. The problem to solve is to find f such as | f(x) - y | -\u003e 0.\n        # where f = np.mean.\n        x = np.random.uniform(size=(num_samples, time_steps, input_dim))\n        y = np.mean(x, axis=1, keepdims=True)\n\n        # Split data into training and testing datasets.\n        c = num_samples // 10\n        x_train, y_train, x_test, y_test = x[c:], y[c:], x[:c], y[:c]\n        test_size = len(x_test)\n\n        # Train the model.\n        print('Training...')\n        backend.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128)\n\n        # Save the model for later.\n        backend.save('n_beats_model.h5')\n\n        # Predict on the testing set (forecast).\n        predictions_forecast = backend.predict(x_test)\n        np.testing.assert_equal(predictions_forecast.shape, (test_size, backend.forecast_length, output_dim))\n\n        # Predict on the testing set (backcast).\n        predictions_backcast = backend.predict(x_test, return_backcast=True)\n        np.testing.assert_equal(predictions_backcast.shape, (test_size, backend.backcast_length, output_dim))\n\n        # Load the model.\n        model_2 = BackendType.load('n_beats_model.h5')\n\n        np.testing.assert_almost_equal(predictions_forecast, model_2.predict(x_test))\n\n\nif __name__ == '__main__':\n    main()\n```\n\nBrowse the [examples](examples) for more. It includes Jupyter notebooks.\n\nJupyter notebook: [NBeats.ipynb](examples/NBeats.ipynb): `make run-jupyter`.\n\n\n### Citation\n\n```\n@misc{NBeatsPRemy,\n  author = {Philippe Remy},\n  title = {N-BEATS: Neural basis expansion analysis for interpretable time series forecasting},\n  year = {2020},\n  publisher = {GitHub},\n  journal = {GitHub repository},\n  howpublished = {\\url{https://github.com/philipperemy/n-beats}},\n}\n```\n\n### Contributors\n\nThank you!\n\n\u003ca href=\"https://github.com/philipperemy/n-beats/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=philipperemy/n-beats\" /\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipperemy%2Fn-beats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilipperemy%2Fn-beats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipperemy%2Fn-beats/lists"}