Skip to main content

NBEATS

N-BEATS: Neural basis expansion analysis for interpretable time series forecasting

NBEATS is a pytorch based library for deep learning based time series forecasting (https://arxiv.org/pdf/1905.10437v3.pdf) and utilises nbeats-pytorch and (https://github.com/amitesh863/nbeats_forecast) with modifications.It uses the architecture provided in (https://github.com/philipperemy/n-beats) and modifies it to provide a choice of 'No Residual' version of the architecture along with 'DRESS' version.

Dependencies: Python >=3.6

Installation

$ pip install NBEATS

Import

from NBEATS import NeuralBeats

Mandatory Parameters:

  • data
  • forecast_length

Basic model with only mandatory parameters can be used to get forecasted values as shown below:

import pandas as pd
from NBEATS import NeuralBeats

data = pd.read_csv('test.csv')   
data = data.values        # (nx1 array)

model = NeuralBeats(data=data, forecast_length=5)
model.fit()
forecast = model.predict()

Optional parameters to the model object

Parameter Default Value
backcast_length 3* forecast_length
Architecture 'DRESS' or 'No Residual' (Default is 'DRESS')
path ' ' (path to save intermediate training checkpoint)
checkpoint_name 'NBEATS-checkpoint.th'
mode 'cpu'
batch_size len(data)/10
thetas_dims [4, 8]
nb_blocks_per_stack 3
share_weights_in_stack False
train_percent 0.8
save_model False
hidden_layer_units 128
stack [1,1] (As per the paper- Mapping is as follows -- 1: GENERIC_BLOCK, 2: TREND_BLOCK , 3: SEASONALITY_BLOCK)

Functions

fit()

This is used for training the model. The default value of parameters passed are epoch=25, optimiser=Adam, plot=True, verbose=True

ex:

model.fit(epoch=25,optimiser=torch.optim.AdamW(model.parameters, lr=0.001, betas=(0.9, 0.999), eps=1e-07, weight_decay=0.01, amsgrad=False),plot=False, verbose=True)
predict_data ()

The argument to the method could be empty or a numpy array of length backcast_length x 1 which means if no argument is passed and training data is till month m then prediction will be for month m+1,m+2 and m+3 when forecast_length=3.If forecast is needed for month m+3 onwards then numpy array of backcast_length (3 x forecast_length -This is by default) i.e 9(3 x 3) previous months (m-6 to m+2) needs to be provided to predict for month m+3,m+4 and m+5.

Important Note : Backcast length can be provided as a model argument along with forecast_length eg: backcast_length=6,backcast_length=9,backcast_length=12......till backcast_length=21 for forecast_length=3 ,as the paper suggests values between 2 x forecast_length to 7 x forecast_length .The default is 3 x forecast_length .

Returns forecasted values.

save(file) & load(file,optimizer):

Save and load the model after training respectively.

Example: model.save('NBEATS.th') or model.load('NBEATS.th')

DEMO

1: GENERIC_BLOCK and 3: SEASONALITY_BLOCK stacks are used below (stack=[1,3]).Go through the paper for more details.Playing around with the 3 blocks(GENERIC,SEASONALITY and TREND) might improve accuracy.

import pandas as pd
from NBEATS import NeuralBeats
from torch import optim

data = pd.read_csv('test.csv')   
data = data.values # nx1(numpy array)

model=NeuralBeats(data=data,forecast_length=5,stack=[1,3],nb_blocks_per_stack=3,thetas_dims=[3,7])

#or use prebuilt models
#model.load(file='NBEATS.th')


#use customised optimiser with parameters
model.fit(epoch=35,optimiser=optim.AdamW(model.parameters, lr=0.001, betas=(0.9, 0.999), eps=1e-07, weight_decay=0.01, amsgrad=False)) 
#or 
#model.fit()

forecast=model.predict()
#or
#model.predict(predict_data=pred_data) where pred_data is numpy array of size backcast_length*1

Metadata

Release files for NBEATS 1.3.11

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 1.3.11
File Size Uploaded
NBEATS-1.3.11.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for NBEATS 1.3.11
File Interpreter ABI Platform
NBEATS-1.3.11-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / NBEATS-1.3.11.tar.gz

Download URL NBEATS-1.3.11.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d78fed64b27916e85a3095a2f7dd1a74aedfb17c8b7fcd39dd05859db5d69bca
BLAKE2b-256 checksum
How to use checksums
bb1f45045592aa0007544ae8d17005d155383f5fd0509ad9009adf1db96e3f0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release files / NBEATS-1.3.11-py3-none-any.whl

Download URL NBEATS-1.3.11-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d5db68f6d4d05e1560ddc04638f73a21ce916ec89316e7f257ca64b8baf1c7d
BLAKE2b-256 checksum
How to use checksums
9e7afcd20b30745d3e5fb9906cb0d43cded58abed635f1f380fc8cdb4302ec58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

1.3.11 This release

2 release files

1.3.10

2 release files

1.3.9

2 release files

1.3.8

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