Skip to content

Instantly share code, notes, and snippets.

@pbruneau
Last active June 24, 2021 06:10
Show Gist options
  • Save pbruneau/1066dbf375033cfc35ea3e758b72dc32 to your computer and use it in GitHub Desktop.
Save pbruneau/1066dbf375033cfc35ea3e758b72dc32 to your computer and use it in GitHub Desktop.
DeepAREstimator Sample
import numpy as np
import pandas as pd
# uncomment to deactivate progress bar
#from gluonts.env import env
#env._push(use_tqdm=False)
from gluonts.dataset.common import ListDataset
from gluonts.dataset.field_names import FieldName
from gluonts.model.deepar import DeepAREstimator
from gluonts.mx.distribution.gaussian import GaussianOutput
from gluonts.mx.trainer import Trainer
def create_dataset(num_series, num_steps, period=24, mu=1, sigma=0.3):
# create target: noise + pattern
# noise
noise = np.random.normal(mu, sigma, size=(num_series, num_steps))
# pattern - sinusoid with different phase
# np.tile: Construct an array by repeating A the number of times given by reps
sin_minumPi_Pi = np.sin(np.tile(np.linspace(-np.pi, np.pi, period), int(num_steps / period)))
sin_Zero_2Pi = np.sin(np.tile(np.linspace(0, 2 * np.pi, period), int(num_steps / period)))
pattern = np.concatenate((np.tile(sin_minumPi_Pi.reshape(1, -1),
(int(np.ceil(num_series / 2)),1)),
np.tile(sin_Zero_2Pi.reshape(1, -1),
(int(np.floor(num_series / 2)), 1))
),
axis=0
)
target = noise + pattern
return target
target = create_dataset(1, 1460*96, 96)
train_ds = ListDataset([{FieldName.TARGET: target[0],
FieldName.START: pd.Timestamp("01-01-2016 00:00:00", freq='15T')
}],
freq='15T')
n = 2
prediction_length=96
estimator = DeepAREstimator(
prediction_length=prediction_length,
context_length=2*prediction_length,
num_cells=100,
freq="15T",
distr_output = GaussianOutput(),
trainer=Trainer(
ctx='gpu',
hybridize=True,
learning_rate=1e-5,
epochs=n,
num_batches_per_epoch=1000,
batch_size=32
)
)
predictor = estimator.train(train_ds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment