Beispiel #1
0
def main(_):
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
        "Exactly one of --config_name or --config_json is required.")
    config = (models.get_model_config(FLAGS.model, FLAGS.config_name) if
              FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
    config = configdict.ConfigDict(config)

    # Create the estimator.
    estimator = estimator_util.create_estimator(model_class,
                                                config.hparams,
                                                model_dir=FLAGS.model_dir)

    # Read and process the input features.
    features = _process_tce(config.inputs.features)

    # Create an input function.
    def input_fn():
        return {
            "time_series_features":
            tf.estimator.inputs.numpy_input_fn(features,
                                               batch_size=1,
                                               shuffle=False,
                                               queue_capacity=1)()
        }

    # Generate the predictions.
    for predictions in estimator.predict(input_fn):
        assert len(predictions) == 1
        print("Prediction:", predictions[0])
Beispiel #2
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)
Beispiel #3
0
    def setUp(self):
        super(BuildDatasetTest, self).setUp()

        # The test dataset contains 10 tensorflow.Example protocol buffers. The i-th
        # Example contains the following features:
        #   global_view = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
        #   local_view = [0.0, 1.0, 2.0, 3.0]
        #   aux_feature = 100 + i
        #   label_str = "PC" if i % 3 == 0 else "AFP" if i % 3 == 1 else "NTP"
        self._file_pattern = _TEST_TFRECORD_FILE

        self._input_config = configdict.ConfigDict({
            "features": {
                "global_view": {
                    "is_time_series": True,
                    "length": 8
                },
                "local_view": {
                    "is_time_series": True,
                    "length": 4
                },
                "aux_feature": {
                    "is_time_series": False,
                    "length": 1
                }
            }
        })
Beispiel #4
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)
Beispiel #5
0
def main(_):
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
        "Exactly one of --config_name or --config_json is required.")
    config = (
        models.get_model_config(FLAGS.model, FLAGS.config_name)
        if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))

    config = configdict.ConfigDict(config)

    # Create the estimator.
    estimator = estimator_util.create_estimator(
        model_class, config.hparams, model_dir=FLAGS.model_dir)

    # Create an input function that reads the evaluation dataset.
    input_fn = estimator_util.create_input_fn(
        file_pattern=FLAGS.eval_files,
        input_config=config.inputs,
        mode=tf.estimator.ModeKeys.EVAL)

    # Run evaluation. This will log the result to stderr and also write a summary
    # file in the model_dir.
    estimator_util.evaluate(estimator, input_fn, eval_name=FLAGS.eval_name)
Beispiel #6
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")
Beispiel #7
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()
Beispiel #8
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)
Beispiel #9
0
def main(_):
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
        "Exactly one of --config_name or --config_json is required.")
    config = (
        models.get_model_config(FLAGS.model, FLAGS.config_name)
        if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))

    config = configdict.ConfigDict(config)
    config_util.log_and_save_config(config, FLAGS.model_dir)

    # Create the estimator.
    run_config = tf.estimator.RunConfig(keep_checkpoint_max=1)
    estimator = estimator_util.create_estimator(model_class, config.hparams,
                                                run_config, FLAGS.model_dir)

    # Create an input function that reads the training dataset. We iterate through
    # the dataset once at a time if we are alternating with evaluation, otherwise
    # we iterate infinitely.
    train_input_fn = estimator_util.create_input_fn(
        file_pattern=FLAGS.train_files,
        input_config=config.inputs,
        mode=tf.estimator.ModeKeys.TRAIN,
        shuffle_values_buffer=FLAGS.shuffle_buffer_size,
        repeat=1 if FLAGS.eval_files else None)

    if not FLAGS.eval_files:
        estimator.train(train_input_fn, max_steps=FLAGS.train_steps)
    else:
        eval_input_fn = estimator_util.create_input_fn(
            file_pattern=FLAGS.eval_files,
            input_config=config.inputs,
            mode=tf.estimator.ModeKeys.EVAL)

        for _ in estimator_util.continuous_train_and_eval(
                estimator=estimator,
                train_input_fn=train_input_fn,
                eval_input_fn=eval_input_fn,
                train_steps=FLAGS.train_steps):
            # continuous_train_and_eval() yields evaluation metrics after each
            # training epoch. We don't do anything here.
            pass
Beispiel #10
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)
Beispiel #11
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)
Beispiel #12
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)