Hyperparameter tunning of DeepAREstimator from gluonTS with Ray

1k Views Asked by At

I want to create forecasting models using the DeepAREstimator from the gluonTS package. How can I use Ray for hyperparameter tuning? Here is sample code.

!pip install --upgrade mxnet-cu101==1.6.0.post0
!pip install --upgrade mxnet==1.6.0
!pip install gluonts


import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from gluonts.model.deepar import DeepAREstimator
from gluonts.mx.trainer import Trainer
import numpy as np
from gluonts.dataset.common import ListDataset
from gluonts.dataset.field_names import FieldName


#Download data
!wget https://archive.ics.uci.edu/ml/machine-learning-databases/00321/LD2011_2014.txt.zip
!unzip LD2011_2014.txt.zip

df=pd.read_csv('LD2011_2014.txt', sep=';', index_col=0, parse_dates=True, decimal=',')
df_input=df.reset_index(drop=True).T.reset_index()
ts_code=df_input["index"].astype('category').cat.codes.values

#Split to train and test
df_train=df_input.iloc[:,1:134999].values
df_test=df_input.iloc[:,134999:].values

freq="15min"
start_train = pd.Timestamp("2011-01-01 00:15:00", freq=freq)
start_test = pd.Timestamp("2014-11-07 05:30:00", freq=freq)
prediction_lentgh=672
estimator = DeepAREstimator(freq=freq, 
                            context_length=672,
                            prediction_length=prediction_lentgh,
                            use_feat_static_cat=True,
                            cardinality=[1],
                            num_layers=2,
                            num_cells=32,
                            cell_type='lstm',
                            trainer=Trainer(epochs=5))
                            
train_ds = ListDataset([
    {
        FieldName.TARGET: target,
        FieldName.START: start_train,
        FieldName.FEAT_STATIC_CAT: fsc
    }
    for (target, fsc) in zip(df_train,
                             ts_code.reshape(-1,1))
], freq=freq)

test_ds = ListDataset([
    {
        FieldName.TARGET: target,
        FieldName.START: start_test,
        FieldName.FEAT_STATIC_CAT: fsc
    }
    for (target, fsc) in zip(df_test,
                            ts_code.reshape(-1,1))
], freq=freq)

predictor = estimator.train(training_data=train_ds)
1

There are 1 best solutions below

0
On

I don't know about Ray, but this video gives some details on how to use GluonTs DeepAr in general, it's a good start: https://www.youtube.com/watch?v=xcbj0RE3kfI&t=758s

If you don't know, please submit it as a comment.