diff options
author | Shanqing Cai <cais@google.com> | 2018-05-03 13:30:12 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-05-03 13:45:45 -0700 |
commit | 7529268d692c1c888f93924e6ca5e10fd3183b80 (patch) | |
tree | 5235f1433397c4fa0a7ad979bd95ba2b65f3d491 /tensorflow/python/debug | |
parent | 5a64e609d0eb94244067f5d7514605863c9f37c3 (diff) |
tfdbg + tflearn: replace deprecated classes and methods in example & docs
* `tf.contrib.learn.Experiment` is deprecated. Remove it from debug_tflearn_iris.py.
* Use `tf.estimator.DNNClassifier`, instead of the older one from `tf.contrib.learn`.
* Use `train()`, instead of `fit()` of Estimators.
* `Estimator.predict()` supports hooks. Add example lines for that.
PiperOrigin-RevId: 195301913
Diffstat (limited to 'tensorflow/python/debug')
-rw-r--r-- | tensorflow/python/debug/BUILD | 1 | ||||
-rw-r--r-- | tensorflow/python/debug/examples/debug_tflearn_iris.py | 83 |
2 files changed, 38 insertions, 46 deletions
diff --git a/tensorflow/python/debug/BUILD b/tensorflow/python/debug/BUILD index b5760df1ed..183994ddaa 100644 --- a/tensorflow/python/debug/BUILD +++ b/tensorflow/python/debug/BUILD @@ -449,7 +449,6 @@ py_binary( deps = [ ":debug_py", "//tensorflow:tensorflow_py", - "//third_party/py/numpy", "@six_archive//:six", ], ) diff --git a/tensorflow/python/debug/examples/debug_tflearn_iris.py b/tensorflow/python/debug/examples/debug_tflearn_iris.py index 4f4666ee4f..00090b21fe 100644 --- a/tensorflow/python/debug/examples/debug_tflearn_iris.py +++ b/tensorflow/python/debug/examples/debug_tflearn_iris.py @@ -22,11 +22,9 @@ import os import sys import tempfile -import numpy as np from six.moves import urllib import tensorflow as tf -from tensorflow.contrib.learn.python.learn import experiment from tensorflow.contrib.learn.python.learn.datasets import base from tensorflow.python import debug as tf_debug @@ -82,28 +80,34 @@ def iris_input_fn(): def main(_): # Load datasets. if FLAGS.fake_data: - training_set = tf.contrib.learn.datasets.base.Dataset( - np.random.random([120, 4]), - np.random.random_integers(3, size=[120]) - 1) - test_set = tf.contrib.learn.datasets.base.Dataset( - np.random.random([30, 4]), - np.random.random_integers(3, size=[30]) - 1) + def training_input_fn(): + return ({"features": tf.random_normal([128, 4])}, + tf.random_uniform([128], minval=0, maxval=3, dtype=tf.int32)) + def test_input_fn(): + return ({"features": tf.random_normal([32, 4])}, + tf.random_uniform([32], minval=0, maxval=3, dtype=tf.int32)) + feature_columns = [ + tf.feature_column.numeric_column("features", shape=(4,))] else: training_data_path, test_data_path = maybe_download_data(FLAGS.data_dir) - training_set = tf.contrib.learn.datasets.base.load_csv_with_header( - filename=training_data_path, - target_dtype=np.int, - features_dtype=np.float32) - test_set = tf.contrib.learn.datasets.base.load_csv_with_header( - filename=test_data_path, target_dtype=np.int, features_dtype=np.float32) - - # Specify that all features have real-value data - feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)] + column_names = [ + "sepal_length", "sepal_width", "petal_length", "petal_width", "label"] + batch_size = 32 + def training_input_fn(): + return tf.contrib.data.make_csv_dataset( + [training_data_path], batch_size, + column_names=column_names, label_name="label") + def test_input_fn(): + return tf.contrib.data.make_csv_dataset( + [test_data_path], batch_size, + column_names=column_names, label_name="label") + feature_columns = [tf.feature_column.numeric_column(feature) + for feature in column_names[:-1]] # Build 3 layer DNN with 10, 20, 10 units respectively. model_dir = FLAGS.model_dir or tempfile.mkdtemp(prefix="debug_tflearn_iris_") - classifier = tf.contrib.learn.DNNClassifier( + classifier = tf.estimator.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3, @@ -121,32 +125,23 @@ def main(_): debug_hook = tf_debug.TensorBoardDebugHook(FLAGS.tensorboard_debug_address) hooks = [debug_hook] - if not FLAGS.use_experiment: - # Fit model. - classifier.fit(x=training_set.data, - y=training_set.target, + # Train model, using tfdbg hook. + classifier.train(training_input_fn, steps=FLAGS.train_steps, - monitors=hooks) + hooks=hooks) - # Evaluate accuracy. - accuracy_score = classifier.evaluate(x=test_set.data, - y=test_set.target, - hooks=hooks)["accuracy"] - else: - ex = experiment.Experiment(classifier, - train_input_fn=iris_input_fn, - eval_input_fn=iris_input_fn, - train_steps=FLAGS.train_steps, - eval_delay_secs=0, - eval_steps=1, - train_monitors=hooks, - eval_hooks=hooks) - ex.train() - accuracy_score = ex.evaluate()["accuracy"] + # Evaluate accuracy, using tfdbg hook. + accuracy_score = classifier.evaluate(test_input_fn, + steps=FLAGS.eval_steps, + hooks=hooks)["accuracy"] print("After training %d steps, Accuracy = %f" % (FLAGS.train_steps, accuracy_score)) + # Make predictions, using tfdbg hook. + predict_results = classifier.predict(test_input_fn, hooks=hooks) + print("A prediction result: %s" % predict_results.next()) + if __name__ == "__main__": parser = argparse.ArgumentParser() @@ -165,14 +160,12 @@ if __name__ == "__main__": "--train_steps", type=int, default=10, - help="Number of steps to run trainer.") + help="Number of steps to run training for.") parser.add_argument( - "--use_experiment", - type="bool", - nargs="?", - const=True, - default=False, - help="Use tf.contrib.learn Experiment to run training and evaluation") + "--eval_steps", + type=int, + default=1, + help="Number of steps to run evaluation foir.") parser.add_argument( "--ui_type", type=str, |