示例#1
0
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate hidden layers.
    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.pre_logits_concat)
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate hidden layers.
    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.pre_logits_concat)
示例#3
0
  def testZeroHiddenLayers(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1
    config.hparams.num_pre_logits_hidden_layers = 0

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    logits_w = testing.get_variable_by_name("logits/kernel")
    self.assertShapeEquals((21, 1), logits_w)
  def testZeroHiddenLayers(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1
    config.hparams.num_pre_logits_hidden_layers = 0

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    logits_w = testing.get_variable_by_name("logits/kernel")
    self.assertShapeEquals((21, 1), logits_w)
示例#5
0
  def testTwoTimeSeriesFeatures(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 20,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 5,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    hidden_spec = {
        "time_feature_1": {
            "rnn_num_layers": 2,
            "rnn_num_units": 16,
            "rnn_memory_cells": "lstm",
            "rnn_activation": "tanh",
            "rnn_dropout": 0.0,
            "rnn_direction": "bi"
        },
        "time_feature_2": {
            "rnn_num_layers": 1,
            "rnn_num_units": 4,
            "rnn_memory_cells": "lstm",
            "rnn_activation": "tanh",
            "rnn_dropout": 0.0,
            "rnn_direction": "bi"
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)
    
    # Build model
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_rnn_model.AstroRNNModel(features, labels, config.hparams,
                                          tf.estimator.ModeKeys.TRAIN)
    model.build()
    
    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
示例#6
0
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        }
    }
    hidden_spec = {
        "time_feature_1": {
            "num_local_layers": 2,
            "local_layer_size": 20,
            "translation_delta": 2,
            "pooling_type": "max",
            "dropout_rate": 0.5,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_fc_model.AstroFCModel(features, labels, config.hparams,
                                        tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    conv = testing.get_variable_by_name("time_feature_1_hidden/conv1d/kernel")
    self.assertShapeEquals((10, 1, 20), conv)

    fc_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/fully_connected_1/weights")
    self.assertShapeEquals((20, 20), fc_1)

    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertShapeEquals((None, 20),
                           model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_hidden_layers["time_feature_1"],
                  model.pre_logits_concat)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        }
    }
    hidden_spec = {
        "time_feature_1": {
            "num_local_layers": 2,
            "local_layer_size": 20,
            "translation_delta": 2,
            "pooling_type": "max",
            "dropout_rate": 0.5,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_fc_model.AstroFCModel(features, labels, config.hparams,
                                        tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    conv = testing.get_variable_by_name("time_feature_1_hidden/conv1d/kernel")
    self.assertShapeEquals((10, 1, 20), conv)

    fc_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/fully_connected_1/weights")
    self.assertShapeEquals((20, 20), fc_1)

    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertShapeEquals((None, 20),
                           model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_hidden_layers["time_feature_1"],
                  model.pre_logits_concat)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
  def testInvalidModeRaisesError(self):
    # Build config.
    config = configdict.ConfigDict(configurations.base())

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    with self.assertRaises(ValueError):
      _ = astro_model.AstroModel(features, labels, config.hparams, "training")
示例#9
0
  def testInvalidModeRaisesError(self):
    # Build config.
    config = configdict.ConfigDict(configurations.base())

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    with self.assertRaises(ValueError):
      _ = astro_model.AstroModel(features, labels, config.hparams, "training")
示例#10
0
    def testEvalMode(self):
        # Build config.
        feature_spec = {
            "time_feature_1": {
                "length": 10,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 10,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
        }
        config = configurations.base()
        config["inputs"]["features"] = feature_spec
        config = configdict.ConfigDict(config)
        config.hparams.output_dim = 1

        # Build model.
        features = input_ops.build_feature_placeholders(config.inputs.features)
        labels = input_ops.build_labels_placeholder()
        model = astro_model.AstroModel(features, labels, config.hparams,
                                       tf.estimator.ModeKeys.TRAIN)
        model.build()

        # Validate Tensor shapes.
        self.assertShapeEquals((None, 21), model.pre_logits_concat)
        self.assertShapeEquals((None, 1), model.logits)
        self.assertShapeEquals((None, 1), model.predictions)
        self.assertShapeEquals((None, ), model.batch_losses)
        self.assertShapeEquals((), model.total_loss)

        # Execute the TensorFlow graph.
        scaffold = tf.train.Scaffold()
        scaffold.finalize()
        with self.session() as sess:
            sess.run([scaffold.init_op, scaffold.local_init_op])
            step = sess.run(model.global_step)
            self.assertEqual(0, step)

            # Fetch total loss.
            features = testing.fake_features(feature_spec, batch_size=16)
            labels = testing.fake_labels(config.hparams.output_dim,
                                         batch_size=16)
            feed_dict = input_ops.prepare_feed_dict(model, features, labels)
            total_loss = sess.run(model.total_loss, feed_dict=feed_dict)
            self.assertShapeEquals((), total_loss)
  def testZeroFeaturesRaisesError(self):
    # Build config.
    config = configurations.base()
    config["inputs"]["features"] = {}
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    with self.assertRaises(ValueError):
      # Raises ValueError because at least one feature is required.
      model.build()
示例#12
0
  def testZeroFeaturesRaisesError(self):
    # Build config.
    config = configurations.base()
    config["inputs"]["features"] = {}
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    with self.assertRaises(ValueError):
      # Raises ValueError because at least one feature is required.
      model.build()
示例#13
0
  def testEvalMode(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    self.assertShapeEquals((None, 1), model.logits)
    self.assertShapeEquals((None, 1), model.predictions)
    self.assertShapeEquals((None,), model.batch_losses)
    self.assertShapeEquals((), model.total_loss)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch total loss.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      total_loss = sess.run(model.total_loss, feed_dict=feed_dict)
      self.assertShapeEquals((), total_loss)
示例#14
0
    def testTwoHiddenLayers(self):
        # Build config.
        feature_spec = {
            "time_feature_1": {
                "length": 10,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 10,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
        }
        config = configurations.base()
        config["inputs"]["features"] = feature_spec
        config = configdict.ConfigDict(config)
        config.hparams.output_dim = 1
        config.hparams.num_pre_logits_hidden_layers = 2
        config.hparams.pre_logits_hidden_layer_size = 5

        # Build model.
        features = input_ops.build_feature_placeholders(config.inputs.features)
        labels = input_ops.build_labels_placeholder()
        model = astro_model.AstroModel(features, labels, config.hparams,
                                       tf.estimator.ModeKeys.TRAIN)
        model.build()

        # TODO(shallue): TensorFlow 2.0 doesn't have global variable collections.
        # If we want to keep testing variable shapes in 2.0, we must keep track of
        # the individual Keras Layer objects in the model class.
        variables = {v.op.name: v for v in tf.global_variables()}

        # Validate Tensor shapes.
        self.assertShapeEquals((None, 21), model.pre_logits_concat)
        fc1 = variables["pre_logits_hidden/fully_connected_1/kernel"]
        self.assertShapeEquals((21, 5), fc1)
        fc2 = variables["pre_logits_hidden/fully_connected_2/kernel"]
        self.assertShapeEquals((5, 5), fc2)
        logits_w = variables["logits/kernel"]
        self.assertShapeEquals((5, 1), logits_w)
    def testTwoTimeSeriesFeatures(self):
        # Build config.
        feature_spec = {
            "time_feature_1": {
                "length": 20,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 5,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
        }
        hidden_spec = {
            "time_feature_1": {
                "num_local_layers": 1,
                "local_layer_size": 20,
                "translation_delta": 1,
                "pooling_type": "max",
                "dropout_rate": 0.5,
            },
            "time_feature_2": {
                "num_local_layers": 2,
                "local_layer_size": 7,
                "translation_delta": 0,
                "dropout_rate": 0,
            }
        }
        config = configurations.base()
        config["inputs"]["features"] = feature_spec
        config["hparams"]["time_series_hidden"] = hidden_spec
        config = configdict.ConfigDict(config)

        # Build model.
        features = input_ops.build_feature_placeholders(config.inputs.features)
        labels = input_ops.build_labels_placeholder()
        model = astro_fc_model.AstroFCModel(features, labels, config.hparams,
                                            tf.estimator.ModeKeys.TRAIN)
        model.build()

        # TODO(shallue): TensorFlow 2.0 doesn't have global variable collections.
        # If we want to keep testing variable shapes in 2.0, we must keep track of
        # the individual Keras Layer objects in the model class.
        variables = {v.op.name: v for v in tf.global_variables()}

        # Validate Tensor shapes.
        conv = variables["time_feature_1_hidden/conv1d/kernel"]
        self.assertShapeEquals((18, 1, 20), conv)

        fc_1 = variables["time_feature_2_hidden/fully_connected_1/kernel"]
        self.assertShapeEquals((5, 7), fc_1)

        fc_2 = variables["time_feature_2_hidden/fully_connected_2/kernel"]
        self.assertShapeEquals((7, 7), fc_2)

        self.assertItemsEqual(["time_feature_1", "time_feature_2"],
                              model.time_series_hidden_layers.keys())
        self.assertShapeEquals(
            (None, 20), model.time_series_hidden_layers["time_feature_1"])
        self.assertShapeEquals(
            (None, 7), model.time_series_hidden_layers["time_feature_2"])
        self.assertItemsEqual(["aux_feature_1"],
                              model.aux_hidden_layers.keys())
        self.assertIs(model.aux_features["aux_feature_1"],
                      model.aux_hidden_layers["aux_feature_1"])
        self.assertShapeEquals((None, 28), model.pre_logits_concat)

        # Execute the TensorFlow graph.
        scaffold = tf.train.Scaffold()
        scaffold.finalize()
        with self.session() as sess:
            sess.run([scaffold.init_op, scaffold.local_init_op])
            step = sess.run(model.global_step)
            self.assertEqual(0, step)

            # Fetch predictions.
            features = testing.fake_features(feature_spec, batch_size=16)
            labels = testing.fake_labels(config.hparams.output_dim,
                                         batch_size=16)
            feed_dict = input_ops.prepare_feed_dict(model, features, labels)
            predictions = sess.run(model.predictions, feed_dict=feed_dict)
            self.assertShapeEquals((16, 1), predictions)
  def testTwoTimeSeriesFeatures(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 20,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 5,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    hidden_spec = {
        "time_feature_1": {
            "cnn_num_blocks": 2,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 4,
            "cnn_block_filter_factor": 1.5,
            "cnn_kernel_size": 3,
            "convolution_padding": "same",
            "pool_size": 2,
            "pool_strides": 2,
        },
        "time_feature_2": {
            "cnn_num_blocks": 1,
            "cnn_block_size": 1,
            "cnn_initial_num_filters": 5,
            "cnn_block_filter_factor": 1,
            "cnn_kernel_size": 2,
            "convolution_padding": "same",
            "pool_size": 0,
            "pool_strides": 0,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_cnn_model.AstroCNNModel(features, labels, config.hparams,
                                          tf.estimator.ModeKeys.TRAIN)
    model.build()

    # TODO(shallue): TensorFlow 2.0 doesn't have global variable collections.
    # If we want to keep testing variable shapes in 2.0, we must keep track of
    # the individual Keras Layer objects in the model class.
    variables = {v.op.name: v for v in tf.global_variables()}

    # Validate Tensor shapes.
    feature_1_block_1_conv_1 = variables[
        "time_feature_1_hidden/block_1/conv_1/kernel"]
    self.assertShapeEquals((3, 1, 4), feature_1_block_1_conv_1)

    feature_1_block_1_conv_2 = variables[
        "time_feature_1_hidden/block_1/conv_2/kernel"]
    self.assertShapeEquals((3, 4, 4), feature_1_block_1_conv_2)

    feature_1_block_2_conv_1 = variables[
        "time_feature_1_hidden/block_2/conv_1/kernel"]
    self.assertShapeEquals((3, 4, 6), feature_1_block_2_conv_1)

    feature_1_block_2_conv_2 = variables[
        "time_feature_1_hidden/block_2/conv_2/kernel"]
    self.assertShapeEquals((3, 6, 6), feature_1_block_2_conv_2)

    feature_2_block_1_conv_1 = variables[
        "time_feature_2_hidden/block_1/conv_1/kernel"]
    self.assertShapeEquals((2, 1, 5), feature_2_block_1_conv_1)

    self.assertItemsEqual(["time_feature_1", "time_feature_2"],
                          model.time_series_hidden_layers.keys())
    self.assertShapeEquals((None, 30),
                           model.time_series_hidden_layers["time_feature_1"])
    self.assertShapeEquals((None, 25),
                           model.time_series_hidden_layers["time_feature_2"])
    self.assertItemsEqual(["aux_feature_1"], model.aux_hidden_layers.keys())
    self.assertIs(model.aux_features["aux_feature_1"],
                  model.aux_hidden_layers["aux_feature_1"])
    self.assertShapeEquals((None, 56), model.pre_logits_concat)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
示例#17
0
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 20,
            "is_time_series": True,
        }
    }
    hidden_spec = {
        "time_feature_1": {
            "cnn_num_blocks": 2,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 4,
            "cnn_block_filter_factor": 1.5,
            "cnn_kernel_size": 3,
            "convolution_padding": "same",
            "pool_size": 2,
            "pool_strides": 2,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_cnn_model.AstroCNNModel(features, labels, config.hparams,
                                          tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    block_1_conv_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_1/conv_1/kernel")
    self.assertShapeEquals((3, 1, 4), block_1_conv_1)

    block_1_conv_2 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_1/conv_2/kernel")
    self.assertShapeEquals((3, 4, 4), block_1_conv_2)

    block_2_conv_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_2/conv_1/kernel")
    self.assertShapeEquals((3, 4, 6), block_2_conv_1)

    block_2_conv_2 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_2/conv_2/kernel")
    self.assertShapeEquals((3, 6, 6), block_2_conv_2)

    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertShapeEquals((None, 30),
                           model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_hidden_layers["time_feature_1"],
                  model.pre_logits_concat)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
示例#18
0
  def testBuildFeaturePlaceholders(self):
    # One time series feature.
    config = configdict.ConfigDict({
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        }
    })
    expected_shapes = {
        "time_series_features": {
            "time_feature_1": [None, 14],
        },
        "aux_features": {}
    }
    features = input_ops.build_feature_placeholders(config)
    self.assertFeatureShapesEqual(expected_shapes, features)

    # Two time series features.
    config = configdict.ConfigDict({
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 5,
            "is_time_series": True,
        }
    })
    expected_shapes = {
        "time_series_features": {
            "time_feature_1": [None, 14],
            "time_feature_2": [None, 5],
        },
        "aux_features": {}
    }
    features = input_ops.build_feature_placeholders(config)
    self.assertFeatureShapesEqual(expected_shapes, features)

    # One aux feature.
    config = configdict.ConfigDict({
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        }
    })
    expected_shapes = {
        "time_series_features": {
            "time_feature_1": [None, 14],
        },
        "aux_features": {
            "aux_feature_1": [None, 1]
        }
    }
    features = input_ops.build_feature_placeholders(config)
    self.assertFeatureShapesEqual(expected_shapes, features)

    # Two aux features.
    config = configdict.ConfigDict({
        "time_feature_1": {
            "length": 14,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
        "aux_feature_2": {
            "length": 6,
            "is_time_series": False,
        },
    })
    expected_shapes = {
        "time_series_features": {
            "time_feature_1": [None, 14],
        },
        "aux_features": {
            "aux_feature_1": [None, 1],
            "aux_feature_2": [None, 6]
        }
    }
    features = input_ops.build_feature_placeholders(config)
    self.assertFeatureShapesEqual(expected_shapes, features)
示例#19
0
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 20,
            "is_time_series": True,
        }
    }
    hidden_spec = {
        "time_feature_1": {
            "cnn_num_blocks": 2,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 4,
            "cnn_block_filter_factor": 1.5,
            "cnn_kernel_size": 3,
            "convolution_padding": "same",
            "pool_size": 2,
            "pool_strides": 2,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config["hparams"]["time_series_hidden"] = hidden_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_cnn_model.AstroCNNModel(features, labels, config.hparams,
                                          tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    block_1_conv_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_1/conv_1/kernel")
    self.assertShapeEquals((3, 1, 4), block_1_conv_1)

    block_1_conv_2 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_1/conv_2/kernel")
    self.assertShapeEquals((3, 4, 4), block_1_conv_2)

    block_2_conv_1 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_2/conv_1/kernel")
    self.assertShapeEquals((3, 4, 6), block_2_conv_1)

    block_2_conv_2 = testing.get_variable_by_name(
        "time_feature_1_hidden/block_2/conv_2/kernel")
    self.assertShapeEquals((3, 6, 6), block_2_conv_2)

    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertShapeEquals((None, 30),
                           model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_hidden_layers["time_feature_1"],
                  model.pre_logits_concat)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch predictions.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      predictions = sess.run(model.predictions, feed_dict=feed_dict)
      self.assertShapeEquals((16, 1), predictions)
示例#20
0
    def testBuildFeaturePlaceholders(self):
        # One time series feature.
        config = configdict.ConfigDict(
            {"time_feature_1": {
                "length": 14,
                "is_time_series": True,
            }})
        expected_shapes = {
            "time_series_features": {
                "time_feature_1": [None, 14],
            },
            "aux_features": {}
        }
        features = input_ops.build_feature_placeholders(config)
        self.assertFeatureShapesEqual(expected_shapes, features)

        # Two time series features.
        config = configdict.ConfigDict({
            "time_feature_1": {
                "length": 14,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 5,
                "is_time_series": True,
            }
        })
        expected_shapes = {
            "time_series_features": {
                "time_feature_1": [None, 14],
                "time_feature_2": [None, 5],
            },
            "aux_features": {}
        }
        features = input_ops.build_feature_placeholders(config)
        self.assertFeatureShapesEqual(expected_shapes, features)

        # One aux feature.
        config = configdict.ConfigDict({
            "time_feature_1": {
                "length": 14,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            }
        })
        expected_shapes = {
            "time_series_features": {
                "time_feature_1": [None, 14],
            },
            "aux_features": {
                "aux_feature_1": [None, 1]
            }
        }
        features = input_ops.build_feature_placeholders(config)
        self.assertFeatureShapesEqual(expected_shapes, features)

        # Two aux features.
        config = configdict.ConfigDict({
            "time_feature_1": {
                "length": 14,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
            "aux_feature_2": {
                "length": 6,
                "is_time_series": False,
            },
        })
        expected_shapes = {
            "time_series_features": {
                "time_feature_1": [None, 14],
            },
            "aux_features": {
                "aux_feature_1": [None, 1],
                "aux_feature_2": [None, 6]
            }
        }
        features = input_ops.build_feature_placeholders(config)
        self.assertFeatureShapesEqual(expected_shapes, features)