Пример #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)
Пример #2
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)
Пример #3
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")
Пример #4
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()
Пример #5
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)