Time series prediction

This is a full example of how to do time series prediction with TensorFlow high level APIs. We'll use the weather dataset available at big query and can be generated with the following query:

SELECT year, mo, da, 
avg(temp) as avg_tmp, 
avg(dewp) as avg_dewp, 
avg(slp) as avg_slp
FROM `bigquery-public-data.noaa_gsod.gsod*`
WHERE temp <> 9999.9 and dewp <> 9999.9 and slp <> 9999.9
GROUP BY year, mo, da
ORDER BY year asc, mo asc, da asc

You can download the data from here.

We'll implement a RNN model using the Estimators API that given the average temperature of 10 days can predict the average temperature of the following day (11th).

Dependecies


In [46]:
# tensorflow
import tensorflow as tf

# rnn common functions
from tensorflow.contrib.learn.python.learn.estimators import rnn_common

# visualization
import seaborn as sns
import matplotlib.pyplot as plt

# helpers
import numpy as np
import pandas as pd
import csv

# enable tensorflow logs
tf.logging.set_verbosity(tf.logging.INFO)

Describing the data set and the model

We're using a weather dataset....[DESCRIBE DATA SET, HOW THE DATA WAS GENERATED AND OTHER DETAILS].

The goal is: based on features from the past days predict the avg temperature in the next day. More specifically we'll use the data from 10 days in sequence to predict the avg temperature in the next day.

Preparing the data

First let's prepare the data for the RNN, the RNN input is:

  • x = sequence of features
  • y = what we want to predict/classify from x, in our casewe want to predict the next avg temperature

Then we have to separe trainning data from test data.


In [47]:
df = pd.read_csv('weather.csv')

number_of_rows = len(df)
print('number of rows in the dataset:', number_of_rows)

print('how a row looks like:')
print(df.head(11))

print()
print("we don't the year mo da columns, so let's forget about them")
df = df[['avg_tmp', 'avg_dewp', 'avg_slp']]
print(df.head(11))


number of rows in the dataset: 32094
how a row looks like:
    year  mo  da  avg_tmp  avg_dewp   avg_slp
0   1929   8   1   58.200    52.875   994.950
1   1929   8   2   55.925    49.500  1013.075
2   1929   8   3   56.650    51.350  1013.675
3   1929   8   4   60.625    55.350  1004.625
4   1929   8   5   58.450    48.300  1010.400
5   1929   8   6   58.700    56.525  1008.000
6   1929   8   7   58.525    54.975  1007.825
7   1929   8   8   58.200    49.925  1015.800
8   1929   8   9   58.850    52.150  1016.400
9   1929   8  10   62.225    56.175  1018.250
10  1929   8  11   62.975    57.900  1017.550

we don't the year mo da columns, so let's forget about them
    avg_tmp  avg_dewp   avg_slp
0    58.200    52.875   994.950
1    55.925    49.500  1013.075
2    56.650    51.350  1013.675
3    60.625    55.350  1004.625
4    58.450    48.300  1010.400
5    58.700    56.525  1008.000
6    58.525    54.975  1007.825
7    58.200    49.925  1015.800
8    58.850    52.150  1016.400
9    62.225    56.175  1018.250
10   62.975    57.900  1017.550

In [48]:
SEQ_LEN = 10
VALID_ROWS = number_of_rows - SEQ_LEN - 1
NUM_FEATURES = 3

# then we can use indexes to access rows easily
df = np.asarray(df)

# sequences will have shape: [VALID_ROWS, SEQ_LEN, NUM_FEATURES]
sequences = np.zeros((VALID_ROWS, SEQ_LEN, NUM_FEATURES), dtype=np.float32)
labels = np.zeros((VALID_ROWS, 1))

# sequences are 10 days
# label is the avg_tmp for the following day (11th)
for i in range(VALID_ROWS):
    sequences[i] = df[i: i + SEQ_LEN]
    labels[i] = df[i + SEQ_LEN][0]

print('-' * 20)
print('Example')
print('-' * 20)

print('sequence:')
print(sequences[0])
print('prediction:', labels[0])


--------------------
Example
--------------------
sequence:
[[   58.20000076    52.875        994.95001221]
 [   55.92499924    49.5         1013.07501221]
 [   56.65000153    51.34999847  1013.67498779]
 [   60.625         55.34999847  1004.625     ]
 [   58.45000076    48.29999924  1010.40002441]
 [   58.70000076    56.52500153  1008.        ]
 [   58.52500153    54.97499847  1007.82501221]
 [   58.20000076    49.92499924  1015.79998779]
 [   58.84999847    52.15000153  1016.40002441]
 [   62.22499847    56.17499924  1018.25      ]]
prediction: [ 62.975]

Separating training, evaluation and a small test data

The test data is going to be very small (10 sequences) and is being used just for visualization.


In [49]:
# these values are based on the number of valid rows which is 32083
TRAIN_SIZE = 30000
EVAL_SIZE = 2073
TEST_SIZE = 10

# TODO(@monteirom): suffle

train_seq = sequences[:TRAIN_SIZE]
train_label = np.asarray(labels[:TRAIN_SIZE], dtype=np.float32)

eval_seq = sequences[TRAIN_SIZE: TRAIN_SIZE + EVAL_SIZE]
eval_label = np.asarray(labels[TRAIN_SIZE:TRAIN_SIZE + EVAL_SIZE], dtype=np.float32)

test_seq = sequences[TRAIN_SIZE + EVAL_SIZE: ]
test_label = np.asarray(labels[TRAIN_SIZE + EVAL_SIZE: ], dtype=np.float32)

print('train shape:', train_seq.shape)
print('eval shape:', eval_seq.shape)
print('test shape:', test_seq.shape)


train shape: (30000, 10, 3)
eval shape: (2073, 10, 3)
test shape: (10, 10, 3)

What we want to predict

This is the plot of the labels from the test data.


In [50]:
# getting test labels
test_plot_data = [test_label[i][0] for i in range(TEST_SIZE)]

# plotting
sns.tsplot(test_plot_data)
plt.show()


/usr/local/lib/python3.4/dist-packages/seaborn/timeseries.py:183: UserWarning: The tsplot function is deprecated and will be removed or replaced (in a substantially altered version) in a future release.
  warnings.warn(msg, UserWarning)

Defining Input functions


In [51]:
BATCH_SIZE = 64

FEATURE_KEY = 'x'
SEQ_LEN_KEY = 'sequence_length'
def make_dict(x):
    d = {}
    d[FEATURE_KEY] = x
    # [SIZE OF DATA SET, 1]
    # where the second dimesion contains the sequence of each
    # sequence in the data set
    d[SEQ_LEN_KEY] = np.asarray(x.shape[0] * [SEQ_LEN], dtype=np.int32)
    return d

# Make input function for training: 
#   num_epochs=None -> will cycle through input data forever
#   shuffle=True -> randomize order of input data
train_input_fn = tf.estimator.inputs.numpy_input_fn(x=make_dict(train_seq),
                                                    y=train_label,
                                                    batch_size=BATCH_SIZE,
                                                    shuffle=True,
                                                    num_epochs=None)

# Make input function for evaluation:
# shuffle=False -> do not randomize input data
eval_input_fn = tf.estimator.inputs.numpy_input_fn(x=make_dict(eval_seq),
                                                    y=eval_label,
                                                    batch_size=BATCH_SIZE,
                                                    shuffle=False)

# Make input function for testing:
# shuffle=False -> do not randomize input data
test_input_fn = tf.estimator.inputs.numpy_input_fn(x=make_dict(test_seq),
                                                    y=test_label,
                                                    batch_size=1,
                                                    shuffle=False)

RNN Model


In [52]:
N_OUTPUTS = 1 # 1 prediction
NUM_FEATURES = 3

def get_model_fn(rnn_cell_sizes,
                 label_dimension,
                 dnn_layer_sizes=[],
                 optimizer='SGD',
                 learning_rate=0.01):
    
    def model_fn(features, labels, mode, params):

        x = features[FEATURE_KEY]
        sequence_length = features[SEQ_LEN_KEY]

        # 1. configure the RNN
        
        # Each RNN layer will consist of a LSTM cell
        rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in rnn_cell_sizes]
        
        # Construct the layers
        multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
        
        outputs, _ = tf.nn.dynamic_rnn(multi_rnn_cell, x, dtype=tf.float32)

        # Slice to keep only the last cell of the RNN
        last_activations = rnn_common.select_last_activations(outputs,
                                                              sequence_length)

        # Construct dense layers on top of the last cell of the RNN
        for units in dnn_layer_sizes:
            last_activations = tf.layers.dense(last_activations,
                                               units,
                                               activation=tf.nn.relu)
        
        # Final dense layer for prediction
        predictions = tf.layers.dense(last_activations, label_dimension)
        
        # 2. Define the loss function for training/evaluation
        loss = None
        eval_metric_ops = None
        train_op = None

        # if predicting labels can be None
        if mode != tf.estimator.ModeKeys.PREDICT:
            loss = tf.losses.mean_squared_error(labels, predictions)
            eval_metric_ops = {
              "rmse": tf.metrics.root_mean_squared_error(labels, predictions)
            }

            # 3. Define the training operation/optimizer
            train_op = tf.contrib.layers.optimize_loss(
              loss=loss,
              global_step=tf.contrib.framework.get_global_step(),
              learning_rate=learning_rate,
              optimizer=optimizer)

        # 4. Create predictions
        predictions_dict = {"predicted": predictions}

        # 5. return ModelFnOps
        return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=predictions_dict,
          loss=loss,
          train_op=train_op,
          eval_metric_ops=eval_metric_ops)
    
    return model_fn

Running model


In [53]:
model_fn = get_model_fn(rnn_cell_sizes=[64], # size of the hidden layers
                        label_dimension=1, # since is just 1 prediction
                        dnn_layer_sizes=[32], # size of units in the dense layers on top of the RNN
                        optimizer='Adam',
                        learning_rate=0.001)

estimator = tf.estimator.Estimator(model_fn=model_fn)


INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmps11p7sjm
INFO:tensorflow:Using config: {'_save_summary_steps': 100, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_session_config': None, '_save_checkpoints_secs': 600, '_model_dir': '/tmp/tmps11p7sjm', '_tf_random_seed': 1, '_keep_checkpoint_max': 5, '_save_checkpoints_steps': None}
WARNING:tensorflow:Estimator's model_fn (<function get_model_fn.<locals>.model_fn at 0x7ff8ac7e4840>) includes params argument, but params are not passed to Estimator.

Trainning


In [54]:
estimator.train(input_fn=train_input_fn, steps=10000)


WARNING:tensorflow:From <ipython-input-52-0393b090cf77>:53: 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
WARNING:tensorflow:From /usr/local/lib/python3.4/dist-packages/tensorflow/contrib/layers/python/layers/optimizers.py:160: assert_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.assert_global_step
INFO:tensorflow:Create CheckpointSaverHook.
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gradients_impl.py:95: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
INFO:tensorflow:Saving checkpoints for 1 into /tmp/tmps11p7sjm/model.ckpt.
INFO:tensorflow:loss = 2432.35, step = 1
INFO:tensorflow:global_step/sec: 162.07
INFO:tensorflow:loss = 2108.45, step = 101 (0.618 sec)
INFO:tensorflow:global_step/sec: 181.016
INFO:tensorflow:loss = 685.139, step = 201 (0.552 sec)
INFO:tensorflow:global_step/sec: 214.737
INFO:tensorflow:loss = 244.134, step = 301 (0.466 sec)
INFO:tensorflow:global_step/sec: 223.519
INFO:tensorflow:loss = 92.9953, step = 401 (0.447 sec)
INFO:tensorflow:global_step/sec: 217.433
INFO:tensorflow:loss = 50.4184, step = 501 (0.460 sec)
INFO:tensorflow:global_step/sec: 222.926
INFO:tensorflow:loss = 87.9727, step = 601 (0.449 sec)
INFO:tensorflow:global_step/sec: 215.832
INFO:tensorflow:loss = 94.4517, step = 701 (0.463 sec)
INFO:tensorflow:global_step/sec: 223.48
INFO:tensorflow:loss = 8.63677, step = 801 (0.448 sec)
INFO:tensorflow:global_step/sec: 208.979
INFO:tensorflow:loss = 2.39279, step = 901 (0.478 sec)
INFO:tensorflow:global_step/sec: 209.057
INFO:tensorflow:loss = 17.6544, step = 1001 (0.478 sec)
INFO:tensorflow:global_step/sec: 207.158
INFO:tensorflow:loss = 6.09578, step = 1101 (0.483 sec)
INFO:tensorflow:global_step/sec: 208.916
INFO:tensorflow:loss = 3.08206, step = 1201 (0.479 sec)
INFO:tensorflow:global_step/sec: 204.758
INFO:tensorflow:loss = 1.49777, step = 1301 (0.488 sec)
INFO:tensorflow:global_step/sec: 204.487
INFO:tensorflow:loss = 4.62279, step = 1401 (0.489 sec)
INFO:tensorflow:global_step/sec: 212.203
INFO:tensorflow:loss = 4.08888, step = 1501 (0.471 sec)
INFO:tensorflow:global_step/sec: 201.688
INFO:tensorflow:loss = 1.57109, step = 1601 (0.496 sec)
INFO:tensorflow:global_step/sec: 208.56
INFO:tensorflow:loss = 1.28143, step = 1701 (0.480 sec)
INFO:tensorflow:global_step/sec: 200.892
INFO:tensorflow:loss = 1.12934, step = 1801 (0.498 sec)
INFO:tensorflow:global_step/sec: 222.958
INFO:tensorflow:loss = 7.44331, step = 1901 (0.449 sec)
INFO:tensorflow:global_step/sec: 214.967
INFO:tensorflow:loss = 3.20228, step = 2001 (0.465 sec)
INFO:tensorflow:global_step/sec: 223.208
INFO:tensorflow:loss = 11.1714, step = 2101 (0.448 sec)
INFO:tensorflow:global_step/sec: 217.262
INFO:tensorflow:loss = 1.15603, step = 2201 (0.461 sec)
INFO:tensorflow:global_step/sec: 197.246
INFO:tensorflow:loss = 0.703166, step = 2301 (0.507 sec)
INFO:tensorflow:global_step/sec: 213.341
INFO:tensorflow:loss = 7.65022, step = 2401 (0.469 sec)
INFO:tensorflow:global_step/sec: 222.54
INFO:tensorflow:loss = 2.37742, step = 2501 (0.449 sec)
INFO:tensorflow:global_step/sec: 222.486
INFO:tensorflow:loss = 2.97222, step = 2601 (0.449 sec)
INFO:tensorflow:global_step/sec: 211.077
INFO:tensorflow:loss = 0.48259, step = 2701 (0.474 sec)
INFO:tensorflow:global_step/sec: 202.806
INFO:tensorflow:loss = 2.44409, step = 2801 (0.493 sec)
INFO:tensorflow:global_step/sec: 209.52
INFO:tensorflow:loss = 2.34389, step = 2901 (0.477 sec)
INFO:tensorflow:global_step/sec: 204.16
INFO:tensorflow:loss = 1.16919, step = 3001 (0.490 sec)
INFO:tensorflow:global_step/sec: 215.858
INFO:tensorflow:loss = 0.887743, step = 3101 (0.463 sec)
INFO:tensorflow:global_step/sec: 210.114
INFO:tensorflow:loss = 0.433529, step = 3201 (0.476 sec)
INFO:tensorflow:global_step/sec: 219.568
INFO:tensorflow:loss = 3.86377, step = 3301 (0.455 sec)
INFO:tensorflow:global_step/sec: 224.478
INFO:tensorflow:loss = 2.99067, step = 3401 (0.445 sec)
INFO:tensorflow:global_step/sec: 221.096
INFO:tensorflow:loss = 5.21679, step = 3501 (0.452 sec)
INFO:tensorflow:global_step/sec: 227.464
INFO:tensorflow:loss = 0.974926, step = 3601 (0.440 sec)
INFO:tensorflow:global_step/sec: 214.255
INFO:tensorflow:loss = 0.918701, step = 3701 (0.467 sec)
INFO:tensorflow:global_step/sec: 215.435
INFO:tensorflow:loss = 10.2067, step = 3801 (0.464 sec)
INFO:tensorflow:global_step/sec: 223.911
INFO:tensorflow:loss = 2.29104, step = 3901 (0.447 sec)
INFO:tensorflow:global_step/sec: 224.306
INFO:tensorflow:loss = 0.968132, step = 4001 (0.446 sec)
INFO:tensorflow:global_step/sec: 220.097
INFO:tensorflow:loss = 0.487839, step = 4101 (0.454 sec)
INFO:tensorflow:global_step/sec: 208.709
INFO:tensorflow:loss = 3.04669, step = 4201 (0.479 sec)
INFO:tensorflow:global_step/sec: 217.575
INFO:tensorflow:loss = 2.73367, step = 4301 (0.460 sec)
INFO:tensorflow:global_step/sec: 222.263
INFO:tensorflow:loss = 0.77022, step = 4401 (0.450 sec)
INFO:tensorflow:global_step/sec: 222.668
INFO:tensorflow:loss = 0.768263, step = 4501 (0.449 sec)
INFO:tensorflow:global_step/sec: 222.417
INFO:tensorflow:loss = 0.515308, step = 4601 (0.450 sec)
INFO:tensorflow:global_step/sec: 217.189
INFO:tensorflow:loss = 5.13322, step = 4701 (0.460 sec)
INFO:tensorflow:global_step/sec: 221.741
INFO:tensorflow:loss = 2.37877, step = 4801 (0.451 sec)
INFO:tensorflow:global_step/sec: 223.568
INFO:tensorflow:loss = 2.40216, step = 4901 (0.447 sec)
INFO:tensorflow:global_step/sec: 198.432
INFO:tensorflow:loss = 0.655012, step = 5001 (0.504 sec)
INFO:tensorflow:global_step/sec: 207.052
INFO:tensorflow:loss = 0.688889, step = 5101 (0.483 sec)
INFO:tensorflow:global_step/sec: 197.119
INFO:tensorflow:loss = 6.95615, step = 5201 (0.507 sec)
INFO:tensorflow:global_step/sec: 217.012
INFO:tensorflow:loss = 1.7891, step = 5301 (0.461 sec)
INFO:tensorflow:global_step/sec: 219.421
INFO:tensorflow:loss = 1.12414, step = 5401 (0.456 sec)
INFO:tensorflow:global_step/sec: 226.242
INFO:tensorflow:loss = 0.387164, step = 5501 (0.442 sec)
INFO:tensorflow:global_step/sec: 219.485
INFO:tensorflow:loss = 0.442431, step = 5601 (0.456 sec)
INFO:tensorflow:global_step/sec: 216.958
INFO:tensorflow:loss = 4.21743, step = 5701 (0.461 sec)
INFO:tensorflow:global_step/sec: 210.281
INFO:tensorflow:loss = 0.701394, step = 5801 (0.476 sec)
INFO:tensorflow:global_step/sec: 221.239
INFO:tensorflow:loss = 0.542924, step = 5901 (0.452 sec)
INFO:tensorflow:global_step/sec: 205.431
INFO:tensorflow:loss = 0.442746, step = 6001 (0.487 sec)
INFO:tensorflow:global_step/sec: 202.422
INFO:tensorflow:loss = 4.46415, step = 6101 (0.494 sec)
INFO:tensorflow:global_step/sec: 221.754
INFO:tensorflow:loss = 2.94044, step = 6201 (0.451 sec)
INFO:tensorflow:global_step/sec: 207.593
INFO:tensorflow:loss = 2.12925, step = 6301 (0.482 sec)
INFO:tensorflow:global_step/sec: 213.965
INFO:tensorflow:loss = 0.562413, step = 6401 (0.467 sec)
INFO:tensorflow:global_step/sec: 214.756
INFO:tensorflow:loss = 0.326047, step = 6501 (0.466 sec)
INFO:tensorflow:global_step/sec: 211.933
INFO:tensorflow:loss = 6.7392, step = 6601 (0.472 sec)
INFO:tensorflow:global_step/sec: 209.162
INFO:tensorflow:loss = 7.94897, step = 6701 (0.478 sec)
INFO:tensorflow:global_step/sec: 203.252
INFO:tensorflow:loss = 4.78001, step = 6801 (0.492 sec)
INFO:tensorflow:global_step/sec: 193.814
INFO:tensorflow:loss = 0.545542, step = 6901 (0.516 sec)
INFO:tensorflow:global_step/sec: 216.26
INFO:tensorflow:loss = 0.40431, step = 7001 (0.462 sec)
INFO:tensorflow:global_step/sec: 212.642
INFO:tensorflow:loss = 4.98732, step = 7101 (0.470 sec)
INFO:tensorflow:global_step/sec: 221.373
INFO:tensorflow:loss = 1.51961, step = 7201 (0.452 sec)
INFO:tensorflow:global_step/sec: 205.786
INFO:tensorflow:loss = 0.626234, step = 7301 (0.486 sec)
INFO:tensorflow:global_step/sec: 215.614
INFO:tensorflow:loss = 0.387444, step = 7401 (0.464 sec)
INFO:tensorflow:global_step/sec: 223.059
INFO:tensorflow:loss = 3.72367, step = 7501 (0.448 sec)
INFO:tensorflow:global_step/sec: 210.782
INFO:tensorflow:loss = 1.80488, step = 7601 (0.474 sec)
INFO:tensorflow:global_step/sec: 222.427
INFO:tensorflow:loss = 1.34275, step = 7701 (0.450 sec)
INFO:tensorflow:global_step/sec: 211.223
INFO:tensorflow:loss = 0.460348, step = 7801 (0.473 sec)
INFO:tensorflow:global_step/sec: 220.994
INFO:tensorflow:loss = 0.292856, step = 7901 (0.452 sec)
INFO:tensorflow:global_step/sec: 220.971
INFO:tensorflow:loss = 5.08593, step = 8001 (0.453 sec)
INFO:tensorflow:global_step/sec: 219.072
INFO:tensorflow:loss = 1.94579, step = 8101 (0.456 sec)
INFO:tensorflow:global_step/sec: 213.417
INFO:tensorflow:loss = 2.90097, step = 8201 (0.469 sec)
INFO:tensorflow:global_step/sec: 199.368
INFO:tensorflow:loss = 0.470805, step = 8301 (0.502 sec)
INFO:tensorflow:global_step/sec: 215.525
INFO:tensorflow:loss = 0.371441, step = 8401 (0.464 sec)
INFO:tensorflow:global_step/sec: 226.412
INFO:tensorflow:loss = 2.93243, step = 8501 (0.442 sec)
INFO:tensorflow:global_step/sec: 195.523
INFO:tensorflow:loss = 1.52685, step = 8601 (0.511 sec)
INFO:tensorflow:global_step/sec: 222.687
INFO:tensorflow:loss = 0.921148, step = 8701 (0.449 sec)
INFO:tensorflow:global_step/sec: 217.159
INFO:tensorflow:loss = 0.326806, step = 8801 (0.460 sec)
INFO:tensorflow:global_step/sec: 213.31
INFO:tensorflow:loss = 3.81109, step = 8901 (0.469 sec)
INFO:tensorflow:global_step/sec: 223.972
INFO:tensorflow:loss = 1.76936, step = 9001 (0.446 sec)
INFO:tensorflow:global_step/sec: 218.241
INFO:tensorflow:loss = 0.748776, step = 9101 (0.458 sec)
INFO:tensorflow:global_step/sec: 220.002
INFO:tensorflow:loss = 1.11636, step = 9201 (0.455 sec)
INFO:tensorflow:global_step/sec: 223.48
INFO:tensorflow:loss = 0.342541, step = 9301 (0.447 sec)
INFO:tensorflow:global_step/sec: 221.62
INFO:tensorflow:loss = 6.021, step = 9401 (0.451 sec)
INFO:tensorflow:global_step/sec: 211.058
INFO:tensorflow:loss = 2.13318, step = 9501 (0.474 sec)
INFO:tensorflow:global_step/sec: 218.263
INFO:tensorflow:loss = 1.30814, step = 9601 (0.458 sec)
INFO:tensorflow:global_step/sec: 214.934
INFO:tensorflow:loss = 0.486767, step = 9701 (0.465 sec)
INFO:tensorflow:global_step/sec: 219.461
INFO:tensorflow:loss = 0.47932, step = 9801 (0.455 sec)
INFO:tensorflow:global_step/sec: 220.783
INFO:tensorflow:loss = 7.2142, step = 9901 (0.453 sec)
INFO:tensorflow:Saving checkpoints for 10000 into /tmp/tmps11p7sjm/model.ckpt.
INFO:tensorflow:Loss for final step: 1.19168.
Out[54]:
<tensorflow.python.estimator.estimator.Estimator at 0x7ff8ac7604a8>

Evaluating


In [55]:
ev = estimator.evaluate(input_fn=eval_input_fn)
print(ev)


WARNING:tensorflow:From <ipython-input-52-0393b090cf77>:53: 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
WARNING:tensorflow:From /usr/local/lib/python3.4/dist-packages/tensorflow/contrib/layers/python/layers/optimizers.py:160: assert_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.assert_global_step
INFO:tensorflow:Starting evaluation at 2017-08-14-17:53:00
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gradients_impl.py:95: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
INFO:tensorflow:Restoring parameters from /tmp/tmps11p7sjm/model.ckpt-10000
INFO:tensorflow:Finished evaluation at 2017-08-14-17:53:01
INFO:tensorflow:Saving dict for global step 10000: global_step = 10000, loss = 0.445435, rmse = 0.671269
{'rmse': 0.67126906, 'global_step': 10000, 'loss': 0.44543543}

Testing


In [56]:
preds = list(estimator.predict(input_fn=test_input_fn))
predictions = []
for p in preds:
    print(p)
    predictions.append(p["predicted"][0])


INFO:tensorflow:Restoring parameters from /tmp/tmps11p7sjm/model.ckpt-10000
{'predicted': array([ 64.33269501], dtype=float32)}
{'predicted': array([ 64.43370056], dtype=float32)}
{'predicted': array([ 64.95011139], dtype=float32)}
{'predicted': array([ 65.11882782], dtype=float32)}
{'predicted': array([ 65.12838745], dtype=float32)}
{'predicted': array([ 65.16017914], dtype=float32)}
{'predicted': array([ 65.70585632], dtype=float32)}
{'predicted': array([ 65.77359772], dtype=float32)}
{'predicted': array([ 65.74475098], dtype=float32)}
{'predicted': array([ 66.49647522], dtype=float32)}

Visualizing predictions


In [57]:
# plotting real values in black
sns.tsplot(test_plot_data, color="black")
# plotting predictions in red
sns.tsplot(predictions, color="red")

plt.show()


/usr/local/lib/python3.4/dist-packages/seaborn/timeseries.py:183: UserWarning: The tsplot function is deprecated and will be removed or replaced (in a substantially altered version) in a future release.
  warnings.warn(msg, UserWarning)

In [ ]: