Exemplo n.º 1
0
  def __init__(self, model, state_manager=None, optimizer=None, model_dir=None,
               config=None):
    """Initialize the Estimator.

    Args:
      model: The time series model to wrap (inheriting from TimeSeriesModel).
      state_manager: The state manager to use, or (by default)
          PassthroughStateManager if none is needed.
      optimizer: The optimization algorithm to use when training, inheriting
          from tf.train.Optimizer. Defaults to Adam with step size 0.02.
      model_dir: See `Estimator`.
      config: See `Estimator`.
    """
    input_statistics_generator = math_utils.InputStatisticsFromMiniBatch(
        dtype=model.dtype, num_features=model.num_features)
    if state_manager is None:
      state_manager = state_management.PassthroughStateManager()
    if optimizer is None:
      optimizer = train.AdamOptimizer(0.02)
    self._model = model
    model_fn = ts_head_lib.time_series_regression_head(
        model, state_manager, optimizer,
        input_statistics_generator=input_statistics_generator).create_estimator_spec
    super(TimeSeriesRegressor, self).__init__(
        model_fn=model_fn,
        model_dir=model_dir,
        config=config)
Exemplo n.º 2
0
 def test_metrics_consistent(self):
     # Tests that the identity metrics used to report in-sample predictions match
     # the behavior of standard metrics.
     g = ops.Graph()
     with g.as_default():
         features = {
             feature_keys.TrainEvalFeatures.TIMES:
             array_ops.zeros((1, 1)),
             feature_keys.TrainEvalFeatures.VALUES:
             array_ops.zeros((1, 1, 1)),
             "ticker":
             array_ops.reshape(
                 math_ops.cast(variables.Variable(
                     name="ticker",
                     initial_value=0,
                     dtype=dtypes.int64,
                     collections=[ops.GraphKeys.LOCAL_VARIABLES
                                  ]).count_up_to(10),
                               dtype=dtypes.float32), (1, 1, 1))
         }
         model_fn = ts_head_lib.time_series_regression_head(
             model=_TickerModel(),
             state_manager=state_management.PassthroughStateManager(),
             optimizer=train.GradientDescentOptimizer(
                 0.001)).create_estimator_spec
         outputs = model_fn(features=features,
                            labels=None,
                            mode=estimator_lib.ModeKeys.EVAL)
         metric_update_ops = [
             metric[1] for metric in outputs.eval_metric_ops.values()
         ]
         loss_mean, loss_update = metrics.mean(outputs.loss)
         metric_update_ops.append(loss_update)
         with self.test_session() as sess:
             coordinator = coordinator_lib.Coordinator()
             queue_runner_impl.start_queue_runners(sess, coord=coordinator)
             variables.local_variables_initializer().run()
             sess.run(metric_update_ops)
             loss_evaled, metric_evaled, nested_metric_evaled = sess.run(
                 (loss_mean, outputs.eval_metric_ops["ticker"][0],
                  outputs.eval_metric_ops[
                      feature_keys.FilteringResults.STATE_TUPLE][0][0]))
             # The custom model_utils metrics for in-sample predictions should be in
             # sync with the Estimator's mean metric for model loss.
             self.assertAllClose(0., loss_evaled)
             self.assertAllClose((((0., ), ), ), metric_evaled)
             self.assertAllClose((((0., ), ), ), nested_metric_evaled)
             coordinator.request_stop()
             coordinator.join()
Exemplo n.º 3
0
 def test_metrics_consistent(self):
   # Tests that the identity metrics used to report in-sample predictions match
   # the behavior of standard metrics.
   g = ops.Graph()
   with g.as_default():
     features = {
         feature_keys.TrainEvalFeatures.TIMES:
             array_ops.zeros((1, 1)),
         feature_keys.TrainEvalFeatures.VALUES:
             array_ops.zeros((1, 1, 1)),
         "ticker":
             array_ops.reshape(
                 math_ops.cast(
                     variables.Variable(
                         name="ticker",
                         initial_value=0,
                         dtype=dtypes.int64,
                         collections=[ops.GraphKeys.LOCAL_VARIABLES])
                     .count_up_to(10),
                     dtype=dtypes.float32), (1, 1, 1))
     }
     model_fn = ts_head_lib.time_series_regression_head(
         model=_TickerModel(),
         state_manager=state_management.PassthroughStateManager(),
         optimizer=train.GradientDescentOptimizer(0.001)).create_estimator_spec
     outputs = model_fn(
         features=features, labels=None, mode=estimator_lib.ModeKeys.EVAL)
     metric_update_ops = [
         metric[1] for metric in outputs.eval_metric_ops.values()]
     loss_mean, loss_update = metrics.mean(outputs.loss)
     metric_update_ops.append(loss_update)
     with self.test_session() as sess:
       coordinator = coordinator_lib.Coordinator()
       queue_runner_impl.start_queue_runners(sess, coord=coordinator)
       variables.local_variables_initializer().run()
       sess.run(metric_update_ops)
       loss_evaled, metric_evaled, nested_metric_evaled = sess.run(
           (loss_mean, outputs.eval_metric_ops["ticker"][0],
            outputs.eval_metric_ops[feature_keys.FilteringResults.STATE_TUPLE][
                0][0]))
       # The custom model_utils metrics for in-sample predictions should be in
       # sync with the Estimator's mean metric for model loss.
       self.assertAllClose(0., loss_evaled)
       self.assertAllClose((((0.,),),), metric_evaled)
       self.assertAllClose((((0.,),),), nested_metric_evaled)
       coordinator.request_stop()
       coordinator.join()
Exemplo n.º 4
0
def _stub_model_fn():
  return ts_head_lib.time_series_regression_head(
      model=_StubModel(),
      state_manager=state_management.PassthroughStateManager(),
      optimizer=train.AdamOptimizer(0.001)).create_estimator_spec
Exemplo n.º 5
0
def _stub_model_fn():
    return ts_head_lib.time_series_regression_head(
        model=_StubModel(),
        state_manager=state_management.PassthroughStateManager(),
        optimizer=train.AdamOptimizer(0.001)).create_estimator_spec