nbeats-lightning


Namenbeats-lightning JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/realdanielbyrne/N-BEATS-Lightning
SummaryA Pytorch Lignthing implementation of N-BEATS.
upload_time2023-10-03 05:08:53
maintainer
docs_urlNone
authorDaniel Byrne
requires_python>=3.9
license
keywords time series forecasting
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # N-BEATS Lightning

This repository provides an implementation of N-BEATS in [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/). PyTorch Lightning is a lightweight PyTorch wrapper for high-performance AI research. It provides a high-level interface for PyTorch that makes it easier to train models, while still giving you the flexibility to customize your training loop. 

The N-BEATS implementation in this repository is based on the original [paper](https://arxiv.org/pdf/1905.10437.pdf.).  This model is designed to be easy to use and extend, and so you can apply N-BEATS to your own time series data and explore its capabilities.

## N-BEATS Algorithm

N-BEATS is a neural network based model for univariate time series forecasting. It stands for Neural Basis Expansion Analysis for Time Series. It was proposed by Boris N. Oreshkin and his co-authors at Element AI in 2019. N-BEATS consists of a deep stack of fully connected layers with backward and forward residual connections. The model can learn a set of basis functions that can decompose any time series into interpretable components, such as trend and seasonality. N-BEATS can also handle a wide range of forecasting problems without requiring any domain-specific modifications or feature engineering. N-BEATS has achieved state-of-the-art performance on several benchmark datasets, such as M3, M4, and TOURISM. This repository provides an implementation of N-BEATS in PyTorch Lightning, along with the code to reproduce the experimental results using the M4 dataset which is included as a reference in this repository. 

Here are some key points about the N-BEATS algorithm:

- **Block Architecture**:
  N-BEATS consists of a stack of fully connected neural networks called "blocks." Each block processes the input time series data and outputs a set of forecasts along with a backcast, which is the reconstructed version of the input.

- **Generic and Interpretable Blocks**:
  There are two types of blocks within N-BEATS: Generic and Interpretable. Generic blocks are designed to learn the underlying patterns in the data automatically, while Interpretable blocks incorporate prior knowledge about the data and are structured to provide insights into the learned patterns.

- **Stacked Ensemble**:
  The blocks are stacked together in an ensemble, and their forecasts are combined to produce the final prediction. This ensemble approach allows N-BEATS to handle a wide range of time series forecasting problems effectively.

- **Parameter Sharing and Scalability**:
  N-BEATS is designed with parameter sharing across the blocks, which promotes scalability and efficiency in training and inference.

- **Performance**:
  N-BEATS has shown state-of-the-art performance on a variety of benchmark time series forecasting datasets, making it a robust choice for many forecasting applications.

The N-BEATS algorithm is a powerful tool for time series forecasting, providing a blend of automatic learning, interpretability, and robust performance across different domains.

## Getting Started

**Installation**
```bash
  pip install nbeats-pytorch-lightning
```

First load the required libraries and your data.

```python
# Import necessary libraries
from nbeats_lightning.nbeats import *
from nbeats_lightning.loaders import *
import pandas as pd

# Load the milk.csv dataset
milk = pd.read_csv('data/milk.csv', index_col=0)
milkval = milk.values.flatten() # flat numpy array
milk.head()
```

Define the model and its hyperparameters. This model will forecast 6 steps into the future. The common practice is to use a multiple of teh forecast horizon for the backcast length.  In this case, we will use 4 times the forecast horizon. Larger batch sizes will result in faster training, but may require more memory.  The number of blocks per stack is a hyperparameter that can be tuned.  The share_weights parameter is set to True to share weights across the blocks.

```python
# Define hyperparameters
forecast_length = 6
backcast_length = 4 * forecast_length
batch_size = 64
n_blocks_per_stack = 3


# An Interpretable N-Beats Model, 
#  - 2 stacks (Fixed at 2 stacks)
#     - 3 trend(256) blocks in first stack (default size)
#     - 3 seasonality(2048) in second stack (default size)
interpretable_milkmodel = NBeatsNet(
  backcast = backcast_length,
  forecast = forecast_length, 
  generic_architecture = False,
  n_blocks_per_stack = n_blocks_per_stack,
  share_weights = True  
)

```

Train the model. The model will be trained for 500 epochs.  The model will be trained on the GPU if one is available.

```python
interpretable_trainer =  pl.Trainer(
  accelerator='auto' # use GPU if available
  ,max_epochs=500
)

interpretable_trainer.fit(interpretable_milkmodel, datamodule=dm)
interpretable_trainer.validate(interpretable_milkmodel, datamodule=dm)  
```

## Using CUDA

If you have a CUDA capable GPU, you will want to install the [CUDA Toolkit](https://developer.nvidia.com/cuda-toolkit) and the [PyTorch](https://pytorch.org/get-started/locally/) version that works with the toolkit.  Currently PyTorch only supports CUDA versions 11.7 and 11.8.  Installing these pacakged will allow you to train your model on the GPU.  You can check if you have a CUDA capable GPU by running the following command in your terminal:

```bash
  $ nvidia-smi
```

or in python environment

```python
  import torch
  torch.cuda.is_available()
```

## N-BEATS Extensions and Variations in this Repository

This repository provides an implementation of N-BEATS in PyTorch Lightning. The implementation is based on the original [paper](https://arxiv.org/pdf/1905.10437.pdf). However, the implementation in this repository has been extended to include the following features:

### ActiveG

This parameter when enabled applies the model's activation funtion to the linear funtions (gb and gf) which are found by the network in the last layer of each block using the functions' parameters found in the preceding layer. The parameter `active_g` is not a feature found in the original N-Beats paper.

You can enable this feature by setting `active_g` to `True`.  Enabling this activation function helps the Generic model converge.  Generally this results in a comparably accurate model in fewer training cycles.  Also, Generic models might sometimes not converge at all.  The fix or this would typically be to add or remove a stack, layers, or units per layer, to give the model more/less capacity or to just try retraining. However, enabling this parameter usualy fixes the problem without the need to modify any other parameters.

The intuition behind the inclusion of this parameter is that the generic model as originally designed connects two layers 
of Linear fully conencted nodes, the first to find the parameters of an expansion polynomial function and the second to find the functions that best fit the forecast and backcast outputs of the block. However, linear layers without activations are not able to learn non-linear functions.  This parameter allows the model to learn non-linear functions by applying the activation function to the linear functions found by the model in the last layer of each block.  This is concsistent with the interpretable arcitecture since the basis functions are also non-linear, and so this feature allows the interpretable and generic models to be more similar.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/realdanielbyrne/N-BEATS-Lightning",
    "name": "nbeats-lightning",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "time series forecasting",
    "author": "Daniel Byrne",
    "author_email": "realdanielbyrne@icloud.com",
    "download_url": "https://files.pythonhosted.org/packages/99/fc/d2bc487c65e64dde8dec8f9182be2b3d10fb19bbda58f81b5b6d9fa369e6/nbeats_lightning-0.1.0.tar.gz",
    "platform": null,
    "description": "# N-BEATS Lightning\n\nThis repository provides an implementation of N-BEATS in [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/). PyTorch Lightning is a lightweight PyTorch wrapper for high-performance AI research. It provides a high-level interface for PyTorch that makes it easier to train models, while still giving you the flexibility to customize your training loop. \n\nThe N-BEATS implementation in this repository is based on the original [paper](https://arxiv.org/pdf/1905.10437.pdf.).  This model is designed to be easy to use and extend, and so you can apply N-BEATS to your own time series data and explore its capabilities.\n\n## N-BEATS Algorithm\n\nN-BEATS is a neural network based model for univariate time series forecasting. It stands for Neural Basis Expansion Analysis for Time Series. It was proposed by Boris N. Oreshkin and his co-authors at Element AI in 2019. N-BEATS consists of a deep stack of fully connected layers with backward and forward residual connections. The model can learn a set of basis functions that can decompose any time series into interpretable components, such as trend and seasonality. N-BEATS can also handle a wide range of forecasting problems without requiring any domain-specific modifications or feature engineering. N-BEATS has achieved state-of-the-art performance on several benchmark datasets, such as M3, M4, and TOURISM. This repository provides an implementation of N-BEATS in PyTorch Lightning, along with the code to reproduce the experimental results using the M4 dataset which is included as a reference in this repository. \n\nHere are some key points about the N-BEATS algorithm:\n\n- **Block Architecture**:\n  N-BEATS consists of a stack of fully connected neural networks called \"blocks.\" Each block processes the input time series data and outputs a set of forecasts along with a backcast, which is the reconstructed version of the input.\n\n- **Generic and Interpretable Blocks**:\n  There are two types of blocks within N-BEATS: Generic and Interpretable. Generic blocks are designed to learn the underlying patterns in the data automatically, while Interpretable blocks incorporate prior knowledge about the data and are structured to provide insights into the learned patterns.\n\n- **Stacked Ensemble**:\n  The blocks are stacked together in an ensemble, and their forecasts are combined to produce the final prediction. This ensemble approach allows N-BEATS to handle a wide range of time series forecasting problems effectively.\n\n- **Parameter Sharing and Scalability**:\n  N-BEATS is designed with parameter sharing across the blocks, which promotes scalability and efficiency in training and inference.\n\n- **Performance**:\n  N-BEATS has shown state-of-the-art performance on a variety of benchmark time series forecasting datasets, making it a robust choice for many forecasting applications.\n\nThe N-BEATS algorithm is a powerful tool for time series forecasting, providing a blend of automatic learning, interpretability, and robust performance across different domains.\n\n## Getting Started\n\n**Installation**\n```bash\n  pip install nbeats-pytorch-lightning\n```\n\nFirst load the required libraries and your data.\n\n```python\n# Import necessary libraries\nfrom nbeats_lightning.nbeats import *\nfrom nbeats_lightning.loaders import *\nimport pandas as pd\n\n# Load the milk.csv dataset\nmilk = pd.read_csv('data/milk.csv', index_col=0)\nmilkval = milk.values.flatten() # flat numpy array\nmilk.head()\n```\n\nDefine the model and its hyperparameters. This model will forecast 6 steps into the future. The common practice is to use a multiple of teh forecast horizon for the backcast length.  In this case, we will use 4 times the forecast horizon. Larger batch sizes will result in faster training, but may require more memory.  The number of blocks per stack is a hyperparameter that can be tuned.  The share_weights parameter is set to True to share weights across the blocks.\n\n```python\n# Define hyperparameters\nforecast_length = 6\nbackcast_length = 4 * forecast_length\nbatch_size = 64\nn_blocks_per_stack = 3\n\n\n# An Interpretable N-Beats Model, \n#  - 2 stacks (Fixed at 2 stacks)\n#     - 3 trend(256) blocks in first stack (default size)\n#     - 3 seasonality(2048) in second stack (default size)\ninterpretable_milkmodel = NBeatsNet(\n  backcast = backcast_length,\n  forecast = forecast_length, \n  generic_architecture = False,\n  n_blocks_per_stack = n_blocks_per_stack,\n  share_weights = True  \n)\n\n```\n\nTrain the model. The model will be trained for 500 epochs.  The model will be trained on the GPU if one is available.\n\n```python\ninterpretable_trainer =  pl.Trainer(\n  accelerator='auto' # use GPU if available\n  ,max_epochs=500\n)\n\ninterpretable_trainer.fit(interpretable_milkmodel, datamodule=dm)\ninterpretable_trainer.validate(interpretable_milkmodel, datamodule=dm)  \n```\n\n## Using CUDA\n\nIf you have a CUDA capable GPU, you will want to install the [CUDA Toolkit](https://developer.nvidia.com/cuda-toolkit) and the [PyTorch](https://pytorch.org/get-started/locally/) version that works with the toolkit.  Currently PyTorch only supports CUDA versions 11.7 and 11.8.  Installing these pacakged will allow you to train your model on the GPU.  You can check if you have a CUDA capable GPU by running the following command in your terminal:\n\n```bash\n  $ nvidia-smi\n```\n\nor in python environment\n\n```python\n  import torch\n  torch.cuda.is_available()\n```\n\n## N-BEATS Extensions and Variations in this Repository\n\nThis repository provides an implementation of N-BEATS in PyTorch Lightning. The implementation is based on the original [paper](https://arxiv.org/pdf/1905.10437.pdf). However, the implementation in this repository has been extended to include the following features:\n\n### ActiveG\n\nThis parameter when enabled applies the model's activation funtion to the linear funtions (gb and gf) which are found by the network in the last layer of each block using the functions' parameters found in the preceding layer. The parameter `active_g` is not a feature found in the original N-Beats paper.\n\nYou can enable this feature by setting `active_g` to `True`.  Enabling this activation function helps the Generic model converge.  Generally this results in a comparably accurate model in fewer training cycles.  Also, Generic models might sometimes not converge at all.  The fix or this would typically be to add or remove a stack, layers, or units per layer, to give the model more/less capacity or to just try retraining. However, enabling this parameter usualy fixes the problem without the need to modify any other parameters.\n\nThe intuition behind the inclusion of this parameter is that the generic model as originally designed connects two layers \nof Linear fully conencted nodes, the first to find the parameters of an expansion polynomial function and the second to find the functions that best fit the forecast and backcast outputs of the block. However, linear layers without activations are not able to learn non-linear functions.  This parameter allows the model to learn non-linear functions by applying the activation function to the linear functions found by the model in the last layer of each block.  This is concsistent with the interpretable arcitecture since the basis functions are also non-linear, and so this feature allows the interpretable and generic models to be more similar.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A Pytorch Lignthing implementation of N-BEATS.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/realdanielbyrne/N-BEATS-Lightning"
    },
    "split_keywords": [
        "time",
        "series",
        "forecasting"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "99fcd2bc487c65e64dde8dec8f9182be2b3d10fb19bbda58f81b5b6d9fa369e6",
                "md5": "d477e3bcedee47789b4d7551ed47342b",
                "sha256": "ed0a1ea891d3d17ea8db20b0302efdeac2c66a58c54d1c7169733622fd907760"
            },
            "downloads": -1,
            "filename": "nbeats_lightning-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d477e3bcedee47789b4d7551ed47342b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 16112,
            "upload_time": "2023-10-03T05:08:53",
            "upload_time_iso_8601": "2023-10-03T05:08:53.250968Z",
            "url": "https://files.pythonhosted.org/packages/99/fc/d2bc487c65e64dde8dec8f9182be2b3d10fb19bbda58f81b5b6d9fa369e6/nbeats_lightning-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-03 05:08:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "realdanielbyrne",
    "github_project": "N-BEATS-Lightning",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "nbeats-lightning"
}
        
Elapsed time: 0.14772s