Beispiel #1
0
def _custom_time_series_regressor(
    model_dir, head_type, exogenous_feature_columns):
  return ts_estimators.TimeSeriesRegressor(
      model=lstm_example._LSTMModel(
          num_features=5, num_units=128,
          exogenous_feature_columns=exogenous_feature_columns),
      optimizer=adam.AdamOptimizer(0.001),
      config=estimator_lib.RunConfig(tf_random_seed=4),
      state_manager=state_management.ChainingStateManager(),
      head_type=head_type,
      model_dir=model_dir)
def _custom_time_series_regressor(
    model_dir, head_type, exogenous_feature_columns):
  return ts_estimators.TimeSeriesRegressor(
      model=lstm_example._LSTMModel(
          num_features=5, num_units=128,
          exogenous_feature_columns=exogenous_feature_columns),
      optimizer=adam.AdamOptimizer(0.001),
      config=estimator_lib.RunConfig(tf_random_seed=4),
      state_manager=state_management.ChainingStateManager(),
      head_type=head_type,
      model_dir=model_dir)
Beispiel #3
0
    def test_custom_metrics(self):
        """Tests that the custom metrics can be applied to the estimator."""
        model_dir = self.get_temp_dir()
        estimator = ts_estimators.TimeSeriesRegressor(
            model=lstm_example._LSTMModel(num_features=1, num_units=4),
            optimizer=adam.AdamOptimizer(0.001),
            config=estimator_lib.RunConfig(tf_random_seed=4),
            model_dir=model_dir)

        def input_fn():
            return {
                feature_keys.TrainEvalFeatures.TIMES: [[1, 2, 3], [7, 8, 9]],
                feature_keys.TrainEvalFeatures.VALUES:
                numpy.array([[[0.], [1.], [0.]], [[2.], [3.], [2.]]])
            }

        def metrics_fn(predictions, features):
            # checking that the inputs are properly passed.
            predict = predictions["mean"]
            target = features[feature_keys.TrainEvalFeatures.VALUES][:, -1, 0]
            return {
                "plain_boring_metric386":
                (math_ops.reduce_mean(math_ops.abs(predict - target)),
                 control_flow_ops.no_op()),
                "fun_metric101": (math_ops.reduce_sum(predict + target),
                                  control_flow_ops.no_op()),
            }

        # Evaluation without training is enough for testing custom metrics.
        estimator = extenders.add_metrics(estimator, metrics_fn)
        evaluation = estimator.evaluate(input_fn, steps=1)
        self.assertIn("plain_boring_metric386", evaluation)
        self.assertIn("fun_metric101", evaluation)
        self.assertIn("average_loss", evaluation)
        # The values are deterministic because of fixed tf_random_seed.
        # However if they become flaky, remove such exacts comparisons.
        self.assertAllClose(evaluation["plain_boring_metric386"], 1.130380)
        self.assertAllClose(evaluation["fun_metric101"], 10.435442)
  def test_custom_metrics(self):
    """Tests that the custom metrics can be applied to the estimator."""
    model_dir = self.get_temp_dir()
    estimator = ts_estimators.TimeSeriesRegressor(
        model=lstm_example._LSTMModel(num_features=1, num_units=4),
        optimizer=adam.AdamOptimizer(0.001),
        config=estimator_lib.RunConfig(tf_random_seed=4),
        model_dir=model_dir)

    def input_fn():
      return {
          feature_keys.TrainEvalFeatures.TIMES: [[1, 2, 3], [7, 8, 9]],
          feature_keys.TrainEvalFeatures.VALUES:
              numpy.array([[[0.], [1.], [0.]], [[2.], [3.], [2.]]])
      }

    def metrics_fn(predictions, features):
      # checking that the inputs are properly passed.
      predict = predictions["mean"]
      target = features[feature_keys.TrainEvalFeatures.VALUES][:, -1, 0]
      return {
          "plain_boring_metric386":
              (math_ops.reduce_mean(math_ops.abs(predict - target)),
               control_flow_ops.no_op()),
          "fun_metric101": (math_ops.reduce_sum(predict + target),
                            control_flow_ops.no_op()),
      }

    # Evaluation without training is enough for testing custom metrics.
    estimator = extenders.add_metrics(estimator, metrics_fn)
    evaluation = estimator.evaluate(input_fn, steps=1)
    self.assertIn("plain_boring_metric386", evaluation)
    self.assertIn("fun_metric101", evaluation)
    self.assertIn("average_loss", evaluation)
    # The values are deterministic because of fixed tf_random_seed.
    # However if they become flaky, remove such exacts comparisons.
    self.assertAllClose(evaluation["plain_boring_metric386"], 1.130380)
    self.assertAllClose(evaluation["fun_metric101"], 10.435442)
Beispiel #5
0
x = np.array(range(1000))
noise = np.random.uniform(-0.2, 0.2, 1000)
y = np.sin(np.pi * x / 50) + np.cos(np.pi * x / 50) + np.sin(
    np.pi * x / 25) + noise
data = {  #以numpy的形式读入
    tf.contrib.timeseries.TrainEvalFeatures.TIMES: x,
    tf.contrib.timeseries.TrainEvalFeatures.VALUES: y
}
reader = NumpyReader(data)
#建立batch数据集
train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(reader,
                                                           batch_size=4,
                                                           window_size=100)
#隐层为128
estimator = ts_estimators.TimeSeriesRegressor(
    model=_LSTMModel(num_features=1, num_units=128),
    optimizer=tf.train.AdadeltaOptimizer(0.001))
estimator.train(input_fn=train_input_fn, steps=2000)
evaluation_input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)
evaluation = estimator.evaluate(input_fn=evaluation_input_fn, steps=1)
(predictions, ) = tuple(
    estimator.predict(input_fn=tf.contrib.timeseries.
                      predict_continuation_input_fn(evaluation, steps=200)))
observed_times = evaluation['times'][0]
observed = evaluation['observed'][0, :, :]
evaluated_times = evaluation['times'][0]
evaluated = evaluation['mean'][0]
predicted_times = predictions['times']
predicted = predictions['mean']
plt.figure(figsize=(15, 5))
plt.axvline(999, linestyle='dotted', linewidth=4, color='r')
Beispiel #6
0
 def test_one_shot_prediction_head_export(self):
     model_dir = self.get_temp_dir()
     categorical_column = feature_column.categorical_column_with_hash_bucket(
         key="categorical_exogenous_feature", hash_bucket_size=16)
     exogenous_feature_columns = [
         feature_column.numeric_column("2d_exogenous_feature", shape=(2, )),
         feature_column.embedding_column(
             categorical_column=categorical_column, dimension=10)
     ]
     estimator = ts_estimators.TimeSeriesRegressor(
         model=lstm_example._LSTMModel(
             num_features=5,
             num_units=128,
             exogenous_feature_columns=exogenous_feature_columns),
         optimizer=adam.AdamOptimizer(0.001),
         config=estimator_lib.RunConfig(tf_random_seed=4),
         state_manager=state_management.ChainingStateManager(),
         head_type=ts_head_lib.OneShotPredictionHead,
         model_dir=model_dir)
     train_features = {
         feature_keys.TrainEvalFeatures.TIMES:
         numpy.arange(20, dtype=numpy.int64),
         feature_keys.TrainEvalFeatures.VALUES:
         numpy.tile(numpy.arange(20, dtype=numpy.float32)[:, None], [1, 5]),
         "2d_exogenous_feature":
         numpy.ones([20, 2]),
         "categorical_exogenous_feature":
         numpy.array(["strkey"] * 20)[:, None]
     }
     train_input_fn = input_pipeline.RandomWindowInputFn(
         input_pipeline.NumpyReader(train_features),
         shuffle_seed=2,
         num_threads=1,
         batch_size=16,
         window_size=16)
     estimator.train(input_fn=train_input_fn, steps=5)
     input_receiver_fn = estimator.build_raw_serving_input_receiver_fn()
     export_location = estimator.export_savedmodel(self.get_temp_dir(),
                                                   input_receiver_fn)
     graph = ops.Graph()
     with graph.as_default():
         with session_lib.Session() as session:
             signatures = loader.load(session, [tag_constants.SERVING],
                                      export_location)
             self.assertEqual([feature_keys.SavedModelLabels.PREDICT],
                              list(signatures.signature_def.keys()))
             predict_signature = signatures.signature_def[
                 feature_keys.SavedModelLabels.PREDICT]
             six.assertCountEqual(self, [
                 feature_keys.FilteringFeatures.TIMES,
                 feature_keys.FilteringFeatures.VALUES,
                 "2d_exogenous_feature", "categorical_exogenous_feature"
             ], predict_signature.inputs.keys())
             features = {
                 feature_keys.TrainEvalFeatures.TIMES:
                 numpy.tile(
                     numpy.arange(35, dtype=numpy.int64)[None, :], [2, 1]),
                 feature_keys.TrainEvalFeatures.VALUES:
                 numpy.tile(
                     numpy.arange(20, dtype=numpy.float32)[None, :, None],
                     [2, 1, 5]),
                 "2d_exogenous_feature":
                 numpy.ones([2, 35, 2]),
                 "categorical_exogenous_feature":
                 numpy.tile(
                     numpy.array(["strkey"] * 35)[None, :, None], [2, 1, 1])
             }
             feeds = {
                 graph.as_graph_element(input_value.name):
                 features[input_key]
                 for input_key, input_value in
                 predict_signature.inputs.items()
             }
             fetches = {
                 output_key: graph.as_graph_element(output_value.name)
                 for output_key, output_value in
                 predict_signature.outputs.items()
             }
             output = session.run(fetches, feed_dict=feeds)
             self.assertAllEqual((2, 15, 5), output["mean"].shape)
Beispiel #7
0
 def test_one_shot_prediction_head_export(self):
   model_dir = self.get_temp_dir()
   categorical_column = feature_column.categorical_column_with_hash_bucket(
       key="categorical_exogenous_feature", hash_bucket_size=16)
   exogenous_feature_columns = [
       feature_column.numeric_column(
           "2d_exogenous_feature", shape=(2,)),
       feature_column.embedding_column(
           categorical_column=categorical_column, dimension=10)]
   estimator = ts_estimators.TimeSeriesRegressor(
       model=lstm_example._LSTMModel(
           num_features=5, num_units=128,
           exogenous_feature_columns=exogenous_feature_columns),
       optimizer=adam.AdamOptimizer(0.001),
       config=estimator_lib.RunConfig(tf_random_seed=4),
       state_manager=state_management.ChainingStateManager(),
       head_type=ts_head_lib.OneShotPredictionHead,
       model_dir=model_dir)
   train_features = {
       feature_keys.TrainEvalFeatures.TIMES: numpy.arange(
           20, dtype=numpy.int64),
       feature_keys.TrainEvalFeatures.VALUES: numpy.tile(numpy.arange(
           20, dtype=numpy.float32)[:, None], [1, 5]),
       "2d_exogenous_feature": numpy.ones([20, 2]),
       "categorical_exogenous_feature": numpy.array(
           ["strkey"] * 20)[:, None]
   }
   train_input_fn = input_pipeline.RandomWindowInputFn(
       input_pipeline.NumpyReader(train_features), shuffle_seed=2,
       num_threads=1, batch_size=16, window_size=16)
   estimator.train(input_fn=train_input_fn, steps=5)
   input_receiver_fn = estimator.build_raw_serving_input_receiver_fn()
   export_location = estimator.export_savedmodel(self.get_temp_dir(),
                                                 input_receiver_fn)
   graph = ops.Graph()
   with graph.as_default():
     with session_lib.Session() as session:
       signatures = loader.load(
           session, [tag_constants.SERVING], export_location)
       self.assertEqual([feature_keys.SavedModelLabels.PREDICT],
                        list(signatures.signature_def.keys()))
       predict_signature = signatures.signature_def[
           feature_keys.SavedModelLabels.PREDICT]
       six.assertCountEqual(
           self,
           [feature_keys.FilteringFeatures.TIMES,
            feature_keys.FilteringFeatures.VALUES,
            "2d_exogenous_feature",
            "categorical_exogenous_feature"],
           predict_signature.inputs.keys())
       features = {
           feature_keys.TrainEvalFeatures.TIMES: numpy.tile(
               numpy.arange(35, dtype=numpy.int64)[None, :], [2, 1]),
           feature_keys.TrainEvalFeatures.VALUES: numpy.tile(numpy.arange(
               20, dtype=numpy.float32)[None, :, None], [2, 1, 5]),
           "2d_exogenous_feature": numpy.ones([2, 35, 2]),
           "categorical_exogenous_feature": numpy.tile(numpy.array(
               ["strkey"] * 35)[None, :, None], [2, 1, 1])
       }
       feeds = {
           graph.as_graph_element(input_value.name): features[input_key]
           for input_key, input_value in predict_signature.inputs.items()}
       fetches = {output_key: graph.as_graph_element(output_value.name)
                  for output_key, output_value
                  in predict_signature.outputs.items()}
       output = session.run(fetches, feed_dict=feeds)
       self.assertAllEqual((2, 15, 5), output["mean"].shape)
@author: Zhukun Luo
Jiangxi university of finance and economics
'''
#多变量lstm
from __future__ import print_function
import numpy as np
import matplotlib 
from tensorflow.contrib.timeseries.examples.lstm import _LSTMModel
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.contrib.timeseries.python.timeseries import estimators as ts_estimators
from tensorflow.contrib.timeseries.python.timeseries import NumpyReader,CSVReader
csv_file_name='./multivariate_periods.csv'
reader=CSVReader(csv_file_name,column_names=((tf.contrib.timeseries.TrainEvalFeatures.TIMES,)+(tf.contrib.timeseries.TrainEvalFeatures.VALUES,)*5))
train_input_fn=tf.contrib.timeseries.RandomWindowInputFn(reader,batch_size=4,window_size=32)
estimator=ts_estimators.TimeSeriesRegressor(model=_LSTMModel(num_features=5,num_units=128),optimizer=tf.train.AdadeltaOptimizer(0.001))
estimator.train(input_fn=train_input_fn, steps=2000)
evaluation_input_fn=tf.contrib.timeseries.WholeDatasetInputFn(reader)
evaluation=estimator.evaluate(input_fn=evaluation_input_fn, steps=1)
(predictions,)=tuple(estimator.predict(input_fn=tf.contrib.timeseries.predict_continuation_input_fn(evaluation,steps=200)))
observed_times=evaluation['times'][0]
observed=evaluation['observed'][0,:,:]
evaluated_times=evaluation['times'][0]
evaluated=evaluation['mean'][0]
predicted_times=predictions['times']
predicted=predictions['mean']
plt.figure(figsize=(15,5))
plt.axvline(999, linestyle='dotted',linewidth=4,color='r')
observed_lines=plt.plot(observed_times,observed,label='observation',color='k')
evaluated_lines=plt.plot(evaluated_times,evaluated,label='evaluation',color='g')
predicted_lines=plt.plot(predicted_times,predicted,label='prediction',color='r')