In [1]:
import tensorflow as tf
import tensorflow.contrib.timeseries as ts
import multiprocessing
import pandas as pd
import numpy as np
import shutil
from datetime import datetime
import matplotlib.pyplot as plt
#%matplotlib inline

print(tf.__version__)


/Users/khalidsalama/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
1.4.0

In [2]:
MODEL_NAME = 'ts-model-01'

TRAIN_DATA_FILE = 'data/train-data.csv'
TEST_DATA_FILE = 'data/test-data.csv'

RESUME_TRAINING = False
MULTI_THREADING = True

Steps to use the ARRegressor Estimator API

  1. Define the metadata
  2. Define a data (Numpy) input function
  3. Define a create Estimator function
  4. Instantiate an esimator with the run_config and required params
  5. Train the estimaor
  6. Evaluate the estimator
  7. Predict using the estimator

1. Define Metadata


In [3]:
HEADER = ['time_index','value']
TIME_INDEX_FEATURE_NAME = 'time_index'
VALUE_FEATURE_NAMES = ['value']

2. Define a Data Input Function


In [4]:
def generate_input_fn(file_name, mode, header_lines=1, batch_size = None, windows_size = None, tail_count=None):
    
    dataframe = pd.read_csv(file_name, names=HEADER, skiprows=header_lines)
    

    if tail_count is not None:
        dataframe = dataframe.tail(tail_count)
    
    print("Dataset Size: {}".format(len(dataframe)))
    print("")
    
    
    data = {
        ts.TrainEvalFeatures.TIMES: dataframe[TIME_INDEX_FEATURE_NAME],
        ts.TrainEvalFeatures.VALUES: dataframe[VALUE_FEATURE_NAMES],
    }
    
    reader = ts.NumpyReader(data)
    
    num_threads = multiprocessing.cpu_count() if MULTI_THREADING else 1
    
    if mode == tf.estimator.ModeKeys.TRAIN:
        input_fn = tf.contrib.timeseries.RandomWindowInputFn(
            reader, 
            batch_size=batch_size, 
            window_size=windows_size,
            num_threads= num_threads
        )
        
    elif mode == tf.estimator.ModeKeys.EVAL:
        input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)
    
    return input_fn

3. Define a Create Estimator Function


In [5]:
def create_estimator(run_config, hparams):

    estimator = ts.ARRegressor(
        periodicities= hparams.periodicities, 
        input_window_size= hparams.input_window_size, 
        output_window_size= hparams.output_window_size,
        num_features=len(VALUE_FEATURE_NAMES),
        loss=hparams.loss,
        hidden_layer_sizes = hparams.hidden_units,
        optimizer = tf.train.AdagradOptimizer(learning_rate=hparams.learning_rate),
        config=run_config
    )
    
    print("")
    print("Estimator Type: {}".format(type(estimator)))
    print("")

    return estimator

4. Instantiate and Estimator with the Required Parameters


In [6]:
CHECKPOINT_STEPS=1000

hparams  = tf.contrib.training.HParams(
    training_steps = 10000,
    periodicities = [200],
    input_window_size = 40,
    output_window_size=10,
    batch_size = 15,
    loss = tf.contrib.timeseries.ARModel.NORMAL_LIKELIHOOD_LOSS, # NORMAL_LIKELIHOOD_LOSS | SQUARED_LOSS
    hidden_units = None,
    learning_rate = 0.1
    
)


model_dir = 'trained_models/{}'.format(MODEL_NAME)

run_config = tf.estimator.RunConfig().replace(
    save_checkpoints_steps=CHECKPOINT_STEPS,
    tf_random_seed=19830610,
    model_dir=model_dir
)
                                             
print("Model directory: {}".format(run_config.model_dir))
print("Hyper-parameters: {}".format(hparams))
print("")

train_input_fn = generate_input_fn(
    file_name=TRAIN_DATA_FILE,
    mode = tf.estimator.ModeKeys.TRAIN,
    batch_size=hparams.batch_size,
    windows_size = hparams.input_window_size + hparams.output_window_size
)

estimator = create_estimator(run_config, hparams)


Model directory: trained_models/ts-model-01
Hyper-parameters: [('batch_size', 15), ('hidden_units', None), ('input_window_size', 40), ('learning_rate', 0.1), ('loss', 'normal_likelihood_loss'), ('output_window_size', 10), ('periodicities', [200]), ('training_steps', 10000)]

Dataset Size: 700

INFO:tensorflow:Using config: {'_model_dir': 'trained_models/ts-model-01', '_tf_random_seed': 19830610, '_save_summary_steps': 100, '_save_checkpoints_steps': 1000, '_save_checkpoints_secs': None, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x125fb5ac8>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

Estimator Type: <class 'tensorflow.contrib.timeseries.python.timeseries.estimators.ARRegressor'>

5. Train the Estimator


In [7]:
if not RESUME_TRAINING:
    shutil.rmtree(model_dir, ignore_errors=True)
    
tf.logging.set_verbosity(tf.logging.INFO)

time_start = datetime.utcnow() 
print("Estimator training started at {}".format(time_start.strftime("%H:%M:%S")))
print(".......................................")

estimator.train(input_fn=train_input_fn, steps=hparams.training_steps)

time_end = datetime.utcnow() 
print(".......................................")
print("Estimator training finished at {}".format(time_end.strftime("%H:%M:%S")))
print("")
time_elapsed = time_end - time_start
print("Estimator training elapsed time: {} seconds".format(time_elapsed.total_seconds()))


Estimator training started at 18:14:08
.......................................
WARNING:tensorflow:From /Users/khalidsalama/anaconda/lib/python3.6/site-packages/tensorflow/contrib/timeseries/python/timeseries/head.py:63: get_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Please switch to tf.train.get_global_step
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:loss = 1.66503, step = 1
INFO:tensorflow:global_step/sec: 543.585
INFO:tensorflow:loss = 0.850968, step = 101 (0.187 sec)
INFO:tensorflow:global_step/sec: 731.17
INFO:tensorflow:loss = 0.649927, step = 201 (0.138 sec)
INFO:tensorflow:global_step/sec: 684.665
INFO:tensorflow:loss = 0.487094, step = 301 (0.145 sec)
INFO:tensorflow:global_step/sec: 833.445
INFO:tensorflow:loss = 0.569605, step = 401 (0.120 sec)
INFO:tensorflow:global_step/sec: 623.916
INFO:tensorflow:loss = 0.460963, step = 501 (0.159 sec)
INFO:tensorflow:global_step/sec: 597.425
INFO:tensorflow:loss = 0.502778, step = 601 (0.168 sec)
INFO:tensorflow:global_step/sec: 470.979
INFO:tensorflow:loss = 0.493022, step = 701 (0.212 sec)
INFO:tensorflow:global_step/sec: 343.837
INFO:tensorflow:loss = 0.412257, step = 801 (0.290 sec)
INFO:tensorflow:global_step/sec: 371.964
INFO:tensorflow:loss = 0.350348, step = 901 (0.269 sec)
INFO:tensorflow:Saving checkpoints for 1001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 103.922
INFO:tensorflow:loss = 0.516232, step = 1001 (0.962 sec)
INFO:tensorflow:global_step/sec: 646.596
INFO:tensorflow:loss = 0.404557, step = 1101 (0.155 sec)
INFO:tensorflow:global_step/sec: 672.509
INFO:tensorflow:loss = 0.321329, step = 1201 (0.148 sec)
INFO:tensorflow:global_step/sec: 289.56
INFO:tensorflow:loss = 0.344648, step = 1301 (0.346 sec)
INFO:tensorflow:global_step/sec: 297.505
INFO:tensorflow:loss = 0.335411, step = 1401 (0.337 sec)
INFO:tensorflow:global_step/sec: 310.657
INFO:tensorflow:loss = 0.383602, step = 1501 (0.321 sec)
INFO:tensorflow:global_step/sec: 312.375
INFO:tensorflow:loss = 0.383221, step = 1601 (0.320 sec)
INFO:tensorflow:global_step/sec: 330.774
INFO:tensorflow:loss = 0.386425, step = 1701 (0.302 sec)
INFO:tensorflow:global_step/sec: 332.913
INFO:tensorflow:loss = 0.366886, step = 1801 (0.300 sec)
INFO:tensorflow:global_step/sec: 384.255
INFO:tensorflow:loss = 0.349388, step = 1901 (0.261 sec)
INFO:tensorflow:Saving checkpoints for 2001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 87.2636
INFO:tensorflow:loss = 0.251073, step = 2001 (1.145 sec)
INFO:tensorflow:global_step/sec: 617.752
INFO:tensorflow:loss = 0.187764, step = 2101 (0.163 sec)
INFO:tensorflow:global_step/sec: 429.953
INFO:tensorflow:loss = 0.234391, step = 2201 (0.232 sec)
INFO:tensorflow:global_step/sec: 340.35
INFO:tensorflow:loss = 0.141446, step = 2301 (0.294 sec)
INFO:tensorflow:global_step/sec: 364.523
INFO:tensorflow:loss = 0.276592, step = 2401 (0.274 sec)
INFO:tensorflow:global_step/sec: 336.206
INFO:tensorflow:loss = 0.376084, step = 2501 (0.297 sec)
INFO:tensorflow:global_step/sec: 363.079
INFO:tensorflow:loss = 0.397516, step = 2601 (0.276 sec)
INFO:tensorflow:global_step/sec: 322.682
INFO:tensorflow:loss = 0.353017, step = 2701 (0.310 sec)
INFO:tensorflow:global_step/sec: 347.165
INFO:tensorflow:loss = 0.233479, step = 2801 (0.288 sec)
INFO:tensorflow:global_step/sec: 360.952
INFO:tensorflow:loss = 0.1621, step = 2901 (0.277 sec)
INFO:tensorflow:Saving checkpoints for 3001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 104.188
INFO:tensorflow:loss = 0.238634, step = 3001 (0.960 sec)
INFO:tensorflow:global_step/sec: 846.443
INFO:tensorflow:loss = 0.156613, step = 3101 (0.119 sec)
INFO:tensorflow:global_step/sec: 536.073
INFO:tensorflow:loss = 0.281067, step = 3201 (0.186 sec)
INFO:tensorflow:global_step/sec: 360.744
INFO:tensorflow:loss = 0.25406, step = 3301 (0.278 sec)
INFO:tensorflow:global_step/sec: 335.564
INFO:tensorflow:loss = 0.0482451, step = 3401 (0.300 sec)
INFO:tensorflow:global_step/sec: 289.561
INFO:tensorflow:loss = 0.212445, step = 3501 (0.344 sec)
INFO:tensorflow:global_step/sec: 341.376
INFO:tensorflow:loss = 0.248023, step = 3601 (0.296 sec)
INFO:tensorflow:global_step/sec: 363.465
INFO:tensorflow:loss = 0.269942, step = 3701 (0.272 sec)
INFO:tensorflow:global_step/sec: 334.795
INFO:tensorflow:loss = 0.33778, step = 3801 (0.299 sec)
INFO:tensorflow:global_step/sec: 334.494
INFO:tensorflow:loss = 0.337147, step = 3901 (0.298 sec)
INFO:tensorflow:Saving checkpoints for 4001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 102.928
INFO:tensorflow:loss = 0.237877, step = 4001 (0.972 sec)
INFO:tensorflow:global_step/sec: 730.177
INFO:tensorflow:loss = 0.254031, step = 4101 (0.136 sec)
INFO:tensorflow:global_step/sec: 521.267
INFO:tensorflow:loss = 0.201583, step = 4201 (0.192 sec)
INFO:tensorflow:global_step/sec: 347.999
INFO:tensorflow:loss = 0.35523, step = 4301 (0.287 sec)
INFO:tensorflow:global_step/sec: 284
INFO:tensorflow:loss = 0.341858, step = 4401 (0.357 sec)
INFO:tensorflow:global_step/sec: 336.694
INFO:tensorflow:loss = 0.21107, step = 4501 (0.292 sec)
INFO:tensorflow:global_step/sec: 362.771
INFO:tensorflow:loss = 0.263154, step = 4601 (0.276 sec)
INFO:tensorflow:global_step/sec: 307.023
INFO:tensorflow:loss = 0.347717, step = 4701 (0.326 sec)
INFO:tensorflow:global_step/sec: 313.093
INFO:tensorflow:loss = 0.203005, step = 4801 (0.320 sec)
INFO:tensorflow:global_step/sec: 302.496
INFO:tensorflow:loss = 0.303553, step = 4901 (0.330 sec)
INFO:tensorflow:Saving checkpoints for 5001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 86.1413
INFO:tensorflow:loss = 0.291631, step = 5001 (1.162 sec)
INFO:tensorflow:global_step/sec: 646.119
INFO:tensorflow:loss = 0.228301, step = 5101 (0.154 sec)
INFO:tensorflow:global_step/sec: 402.704
INFO:tensorflow:loss = 0.244433, step = 5201 (0.248 sec)
INFO:tensorflow:global_step/sec: 348.478
INFO:tensorflow:loss = 0.146858, step = 5301 (0.287 sec)
INFO:tensorflow:global_step/sec: 331.618
INFO:tensorflow:loss = 0.200547, step = 5401 (0.302 sec)
INFO:tensorflow:global_step/sec: 360.025
INFO:tensorflow:loss = 0.197802, step = 5501 (0.277 sec)
INFO:tensorflow:global_step/sec: 344.448
INFO:tensorflow:loss = 0.216406, step = 5601 (0.290 sec)
INFO:tensorflow:global_step/sec: 293.962
INFO:tensorflow:loss = 0.175904, step = 5701 (0.341 sec)
INFO:tensorflow:global_step/sec: 322.13
INFO:tensorflow:loss = 0.0980146, step = 5801 (0.311 sec)
INFO:tensorflow:global_step/sec: 336.292
INFO:tensorflow:loss = 0.234304, step = 5901 (0.297 sec)
INFO:tensorflow:Saving checkpoints for 6001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 100.437
INFO:tensorflow:loss = -0.0058751, step = 6001 (0.998 sec)
INFO:tensorflow:global_step/sec: 507.949
INFO:tensorflow:loss = 0.186584, step = 6101 (0.196 sec)
INFO:tensorflow:global_step/sec: 410.836
INFO:tensorflow:loss = 0.154873, step = 6201 (0.242 sec)
INFO:tensorflow:global_step/sec: 468.466
INFO:tensorflow:loss = 0.0428394, step = 6301 (0.216 sec)
INFO:tensorflow:global_step/sec: 327.601
INFO:tensorflow:loss = 0.375421, step = 6401 (0.302 sec)
INFO:tensorflow:global_step/sec: 266.047
INFO:tensorflow:loss = 0.220757, step = 6501 (0.377 sec)
INFO:tensorflow:global_step/sec: 251.078
INFO:tensorflow:loss = 0.250991, step = 6601 (0.398 sec)
INFO:tensorflow:global_step/sec: 290.443
INFO:tensorflow:loss = 0.222176, step = 6701 (0.346 sec)
INFO:tensorflow:global_step/sec: 232.003
INFO:tensorflow:loss = 0.1618, step = 6801 (0.429 sec)
INFO:tensorflow:global_step/sec: 219.744
INFO:tensorflow:loss = 0.0912539, step = 6901 (0.456 sec)
INFO:tensorflow:Saving checkpoints for 7001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 117.685
INFO:tensorflow:loss = 0.0487571, step = 7001 (0.851 sec)
INFO:tensorflow:global_step/sec: 556.62
INFO:tensorflow:loss = 0.236346, step = 7101 (0.178 sec)
INFO:tensorflow:global_step/sec: 364.922
INFO:tensorflow:loss = -0.0562252, step = 7201 (0.274 sec)
INFO:tensorflow:global_step/sec: 325.071
INFO:tensorflow:loss = 0.027012, step = 7301 (0.308 sec)
INFO:tensorflow:global_step/sec: 269.127
INFO:tensorflow:loss = 0.0512254, step = 7401 (0.371 sec)
INFO:tensorflow:global_step/sec: 320.375
INFO:tensorflow:loss = 0.232202, step = 7501 (0.312 sec)
INFO:tensorflow:global_step/sec: 328.149
INFO:tensorflow:loss = 0.311213, step = 7601 (0.305 sec)
INFO:tensorflow:global_step/sec: 334.126
INFO:tensorflow:loss = 0.130004, step = 7701 (0.300 sec)
INFO:tensorflow:global_step/sec: 323.778
INFO:tensorflow:loss = 0.232519, step = 7801 (0.309 sec)
INFO:tensorflow:global_step/sec: 344.553
INFO:tensorflow:loss = 0.277928, step = 7901 (0.289 sec)
INFO:tensorflow:Saving checkpoints for 8001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 92.4579
INFO:tensorflow:loss = 0.206559, step = 8001 (1.081 sec)
INFO:tensorflow:global_step/sec: 745.307
INFO:tensorflow:loss = 0.119412, step = 8101 (0.135 sec)
INFO:tensorflow:global_step/sec: 659.322
INFO:tensorflow:loss = 0.186125, step = 8201 (0.152 sec)
INFO:tensorflow:global_step/sec: 326.153
INFO:tensorflow:loss = 0.0857025, step = 8301 (0.308 sec)
INFO:tensorflow:global_step/sec: 314.509
INFO:tensorflow:loss = 0.196802, step = 8401 (0.317 sec)
INFO:tensorflow:global_step/sec: 344.262
INFO:tensorflow:loss = 0.114738, step = 8501 (0.290 sec)
INFO:tensorflow:global_step/sec: 339.656
INFO:tensorflow:loss = -0.0322237, step = 8601 (0.296 sec)
INFO:tensorflow:global_step/sec: 364.029
INFO:tensorflow:loss = 0.0980474, step = 8701 (0.273 sec)
INFO:tensorflow:global_step/sec: 361.378
INFO:tensorflow:loss = 0.121637, step = 8801 (0.277 sec)
INFO:tensorflow:global_step/sec: 325.178
INFO:tensorflow:loss = 0.302071, step = 8901 (0.308 sec)
INFO:tensorflow:Saving checkpoints for 9001 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:global_step/sec: 83.2551
INFO:tensorflow:loss = 0.181996, step = 9001 (1.201 sec)
INFO:tensorflow:global_step/sec: 634.091
INFO:tensorflow:loss = 0.142162, step = 9101 (0.157 sec)
INFO:tensorflow:global_step/sec: 455.043
INFO:tensorflow:loss = 0.195377, step = 9201 (0.221 sec)
INFO:tensorflow:global_step/sec: 296.346
INFO:tensorflow:loss = 0.0804031, step = 9301 (0.336 sec)
INFO:tensorflow:global_step/sec: 310.874
INFO:tensorflow:loss = 0.128598, step = 9401 (0.322 sec)
INFO:tensorflow:global_step/sec: 267.677
INFO:tensorflow:loss = 0.00914577, step = 9501 (0.375 sec)
INFO:tensorflow:global_step/sec: 322.664
INFO:tensorflow:loss = 0.208348, step = 9601 (0.307 sec)
INFO:tensorflow:global_step/sec: 324.176
INFO:tensorflow:loss = 0.0411719, step = 9701 (0.309 sec)
INFO:tensorflow:global_step/sec: 318.406
INFO:tensorflow:loss = 0.144736, step = 9801 (0.314 sec)
INFO:tensorflow:global_step/sec: 293.695
INFO:tensorflow:loss = 0.0380919, step = 9901 (0.340 sec)
INFO:tensorflow:Saving checkpoints for 10000 into trained_models/ts-model-01/model.ckpt.
INFO:tensorflow:Loss for final step: 0.0386189.
.......................................
Estimator training finished at 18:14:48

Estimator training elapsed time: 40.041888 seconds

6. Evalute the Estimator


In [8]:
hparams  = tf.contrib.training.HParams(
    training_steps = 10000,
    periodicities = [200],
    input_window_size = 40,
    output_window_size=10,
    batch_size = 15,
    loss = tf.contrib.timeseries.ARModel.SQUARED_LOSS, # NORMAL_LIKELIHOOD_LOSS | SQUARED_LOSS
    hidden_units = None,
    learning_rate = 0.1
    
)

estimator = create_estimator(run_config, hparams)

eval_input_fn = generate_input_fn(
    file_name=TRAIN_DATA_FILE,
    mode = tf.estimator.ModeKeys.EVAL,
)

tf.logging.set_verbosity(tf.logging.WARN)
evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1)
print("")
print(evaluation.keys())
print("")
print("Evaluation Loss ({}) : {}".format(hparams.loss, evaluation['loss']))


INFO:tensorflow:Using config: {'_model_dir': 'trained_models/ts-model-01', '_tf_random_seed': 19830610, '_save_summary_steps': 100, '_save_checkpoints_steps': 1000, '_save_checkpoints_secs': None, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x125fb5ac8>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

Estimator Type: <class 'tensorflow.contrib.timeseries.python.timeseries.estimators.ARRegressor'>

Dataset Size: 700

WARNING:tensorflow:Skipping summary for covariance, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for mean, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for observed, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for start_tuple, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for times, must be a float, np.float32, np.int64, np.int32 or int.

dict_keys(['covariance', 'loss', 'mean', 'observed', 'start_tuple', 'times', 'global_step'])

Evaluation Loss (squared_loss) : 0.19893397390842438

In [9]:
def compute_rmse(a, b):
    rmse =  np.sqrt(np.sum(np.square(a - b)) / len(a))
    return round(rmse,5)

def compute_mae(a, b):
    mae =  np.sqrt(np.sum(np.abs(a - b)) / len(a))
    return round(mae,5)

In [10]:
x_current = evaluation['times'][0]
y_current_actual = evaluation['observed'][0].reshape(-1)
y_current_estimated = evaluation['mean'][0].reshape(-1)

rmse = compute_rmse(y_current_actual, y_current_estimated)
mae = compute_mae(y_current_actual, y_current_estimated)
print("Evaluation RMSE {}".format(rmse))
print("Evaluation MAE {}".format(mae))


Evaluation RMSE 0.44602
Evaluation MAE 0.58438

In [11]:
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_current, y_current_actual, label='actual')
plt.plot(x_current, y_current_estimated, label='estimated')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()


7. Predict using the Estimator


In [12]:
FORECAST_STEPS = [10,50,100,150,200,250,300]

tf.logging.set_verbosity(tf.logging.ERROR)

eval_input_fn = generate_input_fn(
    file_name=TRAIN_DATA_FILE,
    mode = tf.estimator.ModeKeys.EVAL,
    tail_count = hparams.input_window_size + hparams.output_window_size
)


evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1)

df_test = pd.read_csv(TEST_DATA_FILE, names=['time_index','value'], header=0)
print("Test Dataset Size: {}".format(len(df_test)))
print("")

for steps in FORECAST_STEPS:

    forecasts = estimator.predict(input_fn=ts.predict_continuation_input_fn(evaluation, steps=steps))
    forecasts = tuple(forecasts)[0]
    
    x_next = forecasts['times']
    
    y_next_forecast = forecasts['mean']
    y_next_actual = df_test.value[:steps].values
    
    rmse =  compute_rmse(y_next_actual, y_next_forecast)
    mae =  compute_mae(y_next_actual, y_next_forecast)

    print("Forecast Steps {}: RMSE {} - MAE {}".format(steps,rmse,mae))

print("")
print(forecasts.keys())


Dataset Size: 50

Test Dataset Size: 300

Forecast Steps 10: RMSE 2.15341 - MAE 2.34381
Forecast Steps 50: RMSE 4.85698 - MAE 5.30662
Forecast Steps 100: RMSE 7.76817 - MAE 7.88263
Forecast Steps 150: RMSE 16.45679 - MAE 12.72724
Forecast Steps 200: RMSE 20.7832 - MAE 15.48061
Forecast Steps 250: RMSE 21.42506 - MAE 16.60133
Forecast Steps 300: RMSE 22.28004 - MAE 17.69507

dict_keys(['mean', 'covariance', 'times'])

In [13]:
plt.close('all')
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_next, y_next_actual, label='actual')
plt.plot(x_next, y_next_forecast, label='forecasted')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()



In [14]:
x_all = np.concatenate( (x_current, x_next) , axis=0)
y_actual_all = np.concatenate((y_current_actual, y_next_actual), axis=0)

plt.close('all')
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_all, y_actual_all, label='actual')
plt.plot(x_current, y_current_estimated, label='estimated')
plt.plot(x_next, y_next_forecast, label='forecasted')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()


Questions:

  1. how to create a serving function and save the model?
  2. forecast errror range?

In [ ]: