Skip to main content

Pytorch implementation of the ESRNN

Project description

Build PyPI version fury.io Downloads Python 3.6+ License: MIT

Pytorch Implementation of the ES-RNN

Pytorch implementation of the ES-RNN algorithm proposed by Smyl, winning submission of the M4 Forecasting Competition. The class wraps fit and predict methods to facilitate interaction with Machine Learning pipelines along with evaluation and data wrangling utility. Developed by Autonlab’s members at Carnegie Mellon University.

Installation Prerequisites

  • numpy>=1.16.1
  • pandas>=0.25.2
  • pytorch>=1.3.1

Installation

This code is a work in progress, any contributions or issues are welcome on GitHub at: https://github.com/kdgutier/esrnn_torch

You can install the released version of ESRNN from the Python package index with:

pip install ESRNN

Usage

Input data

The fit method receives X_df, y_df training pandas dataframes in long format. Optionally X_test_df and y_test_df to compute out of sample performance.

  • X_df must contain the columns ['unique_id', 'ds', 'x']
  • y_df must contain the columns ['unique_id', 'ds', 'y']
  • X_test_df must contain the columns ['unique_id', 'ds', 'x']
  • y_test_df must contain the columns ['unique_id', 'ds', 'y'] and a benchmark model to compare against (default 'y_hat_naive2').

For all the above:

  • The column 'unique_id' is a time series identifier, the column 'ds' stands for the datetime.
  • Column 'x' is an exogenous categorical feature.
  • Column 'y' is the target variable.
  • Column 'y' does not allow negative values and the first entry for all series must be grater than 0.

The X and y dataframes must contain the same values for 'unique_id', 'ds' columns and be balanced, ie.no gaps between dates for the frequency.

X_df y_df X_test_df y_test_df

M4 example

from ESRNN.m4_data import prepare_m4_data
from ESRNN.utils_evaluation import evaluate_prediction_owa

from ESRNN import ESRNN

X_train_df, y_train_df, X_test_df, y_test_df = prepare_m4_data(dataset_name='Yearly',
                                                               directory = './data',
                                                               num_obs=1000)

# Instantiate model
model = ESRNN(max_epochs=25, freq_of_test=5, batch_size=4, learning_rate=1e-4,
              per_series_lr_multip=0.8, lr_scheduler_step_size=10,
              lr_decay=0.1, gradient_clipping_threshold=50,
              rnn_weight_decay=0.0, level_variability_penalty=100,
              testing_percentile=50, training_percentile=50,
              ensemble=False, max_periods=25, seasonality=[],
              input_size=4, output_size=6,
              cell_type='LSTM', state_hsize=40,
              dilations=[[1], [6]], add_nl_layer=False,
              random_seed=1, device='cpu')

# Fit model
# If y_test_df is provided the model
# will evaluate predictions on
# this set every freq_test epochs
model.fit(X_train_df, y_train_df, X_test_df, y_test_df)

# Predict on test set
y_hat_df = model.predict(X_test_df)

# Evaluate predictions
final_owa, final_mase, final_smape = evaluate_prediction_owa(y_hat_df, y_train_df,
                                                             X_test_df, y_test_df,
                                                             naive2_seasonality=1)

Overall Weighted Average

A metric that is useful for quantifying the aggregate error of a specific model for various time series is the Overall Weighted Average (OWA) proposed for the M4 competition. This metric is calculated by obtaining the average of the symmetric mean absolute percentage error (sMAPE) and the mean absolute scaled error (MASE) for all the time series of the model and also calculating it for the Naive2 predictions. Both sMAPE and MASE are scale independent. These measurements are calculated as follows:

OWA

Current Results

Here we used the model directly to compare to the original implementation. It is worth noticing that these results do not include the ensemble methods mentioned in the ESRNN paper.
Results of the M4 competition.

DATASET OUR OWA M4 OWA (Smyl)
Yearly 0.785 0.778
Quarterly 0.879 0.847
Monthly 0.872 0.836
Hourly 0.615 0.920
Weekly 0.952 0.920
Daily 0.968 0.920

Replicating M4 results

Replicating the M4 results is as easy as running the following line of code (for each frequency) after installing the package via pip:

python -m ESRNN.m4_run --dataset 'Yearly' --results_directory '/some/path' \
                       --gpu_id 0 --use_cpu 0

Use --help to get the description of each argument:

python -m ESRNN.m4_run --help

Authors

This repository was developed with joint efforts from AutonLab researchers at Carnegie Mellon University and Orax data scientists.

License

This project is licensed under the MIT License - see the LICENSE file for details.

REFERENCES

  1. A hybrid method of exponential smoothing and recurrent neural networks for time series forecasting
  2. The M4 Competition: Results, findings, conclusion and way forward
  3. M4 Competition Data
  4. Dilated Recurrent Neural Networks
  5. Residual LSTM: Design of a Deep Recurrent Architecture for Distant Speech Recognition
  6. A Dual-Stage Attention-Based recurrent neural network for time series prediction

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ESRNN-0.1.3.tar.gz (694.7 kB view details)

Uploaded Source

Built Distribution

ESRNN-0.1.3-py3-none-any.whl (42.6 kB view details)

Uploaded Python 3

File details

Details for the file ESRNN-0.1.3.tar.gz.

File metadata

  • Download URL: ESRNN-0.1.3.tar.gz
  • Upload date:
  • Size: 694.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for ESRNN-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7f7227bf43f61183a101d9a7c82645d6aee2c180d004eae73a86eb74c5e4e5e9
MD5 631290a3fb650137fe5320d1520372d9
BLAKE2b-256 8c36105c4bf3d4abf5128b7083b5b5805118daa3b46c7ca3ff5282121372bb0e

See more details on using hashes here.

File details

Details for the file ESRNN-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ESRNN-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for ESRNN-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7c8a7eeb242f33befbcc871f3eb0869c07f1a7bace0af406b16b45429946d6db
MD5 1dad8c8eff43a49defaa37bca27cdcac
BLAKE2b-256 db94aba0b159eccf32f85c51ed04c885fbb13f8b89ac35b7b0efef1df900db9a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page