diff options
author | 2017-06-16 08:36:01 -0700 | |
---|---|---|
committer | 2017-06-16 08:39:44 -0700 | |
commit | 571424a44f0e03b744fbc156382b2522311f6c3a (patch) | |
tree | 03644d7cc24a5be05af94bf25f9bbaf7e80aef99 /tensorflow/examples/tutorials | |
parent | 452aa0dc7d66c5661b3da7c8fbdff92e0df33590 (diff) |
Updates input.fn doc and corresponding boston.py tutorial.
PiperOrigin-RevId: 159230680
Diffstat (limited to 'tensorflow/examples/tutorials')
-rw-r--r-- | tensorflow/examples/tutorials/input_fn/boston.py | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/tensorflow/examples/tutorials/input_fn/boston.py b/tensorflow/examples/tutorials/input_fn/boston.py index c7fb7e2316..34f350e9ac 100644 --- a/tensorflow/examples/tutorials/input_fn/boston.py +++ b/tensorflow/examples/tutorials/input_fn/boston.py @@ -31,10 +31,12 @@ FEATURES = ["crim", "zn", "indus", "nox", "rm", LABEL = "medv" -def input_fn(data_set): - feature_cols = {k: tf.constant(data_set[k].values) for k in FEATURES} - labels = tf.constant(data_set[LABEL].values) - return feature_cols, labels +def get_input_fn(data_set, num_epochs=None, shuffle=True): + return tf.estimator.inputs.pandas_input_fn( + x=pd.DataFrame({k: data_set[k].values for k in FEATURES}), + y=pd.Series(data_set[LABEL].values), + num_epochs=num_epochs, + shuffle=shuffle) def main(unused_argv): @@ -49,26 +51,28 @@ def main(unused_argv): skiprows=1, names=COLUMNS) # Feature cols - feature_cols = [tf.contrib.layers.real_valued_column(k) - for k in FEATURES] + feature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES] # Build 2 layer fully connected DNN with 10, 10 units respectively. - regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols, - hidden_units=[10, 10], - model_dir="/tmp/boston_model") + regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols, + hidden_units=[10, 10], + model_dir="/tmp/boston_model") - # Fit - regressor.fit(input_fn=lambda: input_fn(training_set), steps=5000) + # Train + regressor.train(input_fn=get_input_fn(training_set), steps=5000) - # Score accuracy - ev = regressor.evaluate(input_fn=lambda: input_fn(test_set), steps=1) + # Evaluate loss over one epoch of test_set. + ev = regressor.evaluate( + input_fn=get_input_fn(test_set, num_epochs=1, shuffle=False)) loss_score = ev["loss"] print("Loss: {0:f}".format(loss_score)) - # Print out predictions - y = regressor.predict(input_fn=lambda: input_fn(prediction_set)) - # .predict() returns an iterator; convert to a list and print predictions - predictions = list(itertools.islice(y, 6)) + # Print out predictions over a slice of prediction_set. + y = regressor.predict( + input_fn=get_input_fn(prediction_set, num_epochs=1, shuffle=False)) + # .predict() returns an iterator of dicts; convert to a list and print + # predictions + predictions = list(p["predictions"] for p in itertools.islice(y, 6)) print("Predictions: {}".format(str(predictions))) if __name__ == "__main__": |