Demo entry 6534173

recommend

   

Submitted by vito on Jun 01, 2017 at 07:50
Language: Python. Code size: 3.4 kB.

class DistributionWDL(BaseEstimator):
    def run(self, target, cluster_spec, task_id, model_dir
        , config_file_path, batch_size, model_params):
        tf.logging.set_verbosity(tf.logging.INFO)
        print("model_params:%s" % model_params)

        # 1. build Classifier
        fg = FeatureGenerator(config_file_path)
        feature_map = fg.feature_map()
        print("feature map = %s" % feature_map)
        classifier = DNNLinearCombinedClassifier(
            dnn_hidden_units=deep_net,
            config=_build_run_config(target, cluster_spec, task_id),
            linear_feature_columns=None if len(fg.wide()) == 0 else fg.wide(),
            dnn_feature_columns=None if len(fg.deep()) == 0 else fg.deep(),
            model_dir=model_dir,
            linear_optimizer=tf.train.FtrlOptimizer(
                learning_rate=learning_rate,
                learning_rate_power=learning_rate_power,
                initial_accumulator_value=initial_accumulator_value,
                l1_regularization_strength=l1_regularization_strength,
                l2_regularization_strength=l2_regularization_strength
            ),
            input_layer_min_slice_size=2048 << 20,
            hidden_layer_min_slice_size=1024 << 20
        )
        print("task:%s" % task)

        if task == "train":
            # 2. train with monitor
            train_monitor = ValidationMonitor(
                input_fn=lambda: self.default_input_fn(
                    file_path=config_file_path,
                    batch_size=batch_size
                ),
                every_n_steps=validation_per_steps,
                eval_steps=validation_max_steps,
                metrics={
                    "accuracy":
                        MetricSpec(
                            metric_fn=tf.contrib.metrics.streaming_accuracy,
                            prediction_key=PredictionKey.CLASSES)
                },
                name="[Train Monitor]"
            )

            test_monitor = ValidationMonitor(
                input_fn=lambda: self.default_input_fn(
                    file_path=config_file_path,
                    batch_size=batch_size
                ),
                every_n_steps=validation_per_steps,
                eval_steps=validation_max_steps,
                metrics={
                    "accuracy":
                        MetricSpec(
                            metric_fn=tf.contrib.metrics.streaming_accuracy,
                            prediction_key=PredictionKey.CLASSES)
                },
                name="[Test Monitor]"
            )

            classifier.fit(
                input_fn=lambda: self.default_input_fn(
                    file_path=config_file_path,
                    batch_size=batch_size
                ),
                steps=training_max_steps,
                monitors=[train_monitor, test_monitor]
            )
        elif task == "predict":
            # 3. predict
            predictions = classifier.predict_proba(
                input_fn=lambda: self.default_input_fn(
                    file_path=config_file_path,
                    batch_size=batch_size
                ),
                as_iterable=True
            )
        print("End Tensorflow!")
        return classifier._graph

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).