aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2017-06-23 14:44:08 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-06-23 14:47:29 -0700
commitb00dbe39b8571eec41e9da0a83e8ad264ac5386f (patch)
tree68eb65e089b76ca0c2060dda5fba549f6376296b
parentdee19ca4dd0510499b7da9ebb97c92910638b4f2 (diff)
Updates some examples in examples/learn.
PiperOrigin-RevId: 159996397
-rw-r--r--tensorflow/examples/learn/boston.py34
-rw-r--r--tensorflow/examples/learn/iris.py31
2 files changed, 45 insertions, 20 deletions
diff --git a/tensorflow/examples/learn/boston.py b/tensorflow/examples/learn/boston.py
index 7a7024e001..c9ce508dfd 100644
--- a/tensorflow/examples/learn/boston.py
+++ b/tensorflow/examples/learn/boston.py
@@ -17,6 +17,7 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
+import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection
@@ -39,22 +40,31 @@ def main(unused_argv):
x_train = scaler.fit_transform(x_train)
# Build 2 layer fully connected DNN with 10, 10 units respectively.
- feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
- x_train)
- regressor = tf.contrib.learn.DNNRegressor(
+ feature_columns = [
+ tf.feature_column.numeric_column('x', shape=np.array(x_train).shape[1:])]
+ regressor = tf.estimator.DNNRegressor(
feature_columns=feature_columns, hidden_units=[10, 10])
- # Fit
- regressor.fit(x_train, y_train, steps=5000, batch_size=1)
-
- # Transform
+ # Train.
+ train_input_fn = tf.estimator.inputs.numpy_input_fn(
+ x={'x': x_train}, y=y_train, batch_size=1, num_epochs=None, shuffle=True)
+ regressor.train(input_fn=train_input_fn, steps=2000)
+
+ # Predict.
x_transformed = scaler.transform(x_test)
-
- # Predict and score
- y_predicted = list(regressor.predict(x_transformed, as_iterable=True))
- score = metrics.mean_squared_error(y_predicted, y_test)
+ test_input_fn = tf.estimator.inputs.numpy_input_fn(
+ x={'x': x_transformed}, y=y_test, num_epochs=1, shuffle=False)
+ predictions = regressor.predict(input_fn=test_input_fn)
+ y_predicted = np.array(list(p['predictions'] for p in predictions))
+ y_predicted = y_predicted.reshape(np.array(y_test).shape)
+
+ # Score with sklearn.
+ score_sklearn = metrics.mean_squared_error(y_predicted, y_test)
+ print('MSE (sklearn): {0:f}'.format(score_sklearn))
- print('MSE: {0:f}'.format(score))
+ # Score with tensorflow.
+ scores = regressor.evaluate(input_fn=test_input_fn)
+ print('MSE (tensorflow): {0:f}'.format(scores['average_loss']))
if __name__ == '__main__':
diff --git a/tensorflow/examples/learn/iris.py b/tensorflow/examples/learn/iris.py
index ec2aa9b573..2ec490b7a2 100644
--- a/tensorflow/examples/learn/iris.py
+++ b/tensorflow/examples/learn/iris.py
@@ -17,6 +17,7 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
+import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection
@@ -31,16 +32,30 @@ def main(unused_argv):
iris.data, iris.target, test_size=0.2, random_state=42)
# Build 3 layer DNN with 10, 20, 10 units respectively.
- feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
- x_train)
- classifier = tf.contrib.learn.DNNClassifier(
+ feature_columns = [
+ tf.feature_column.numeric_column('x', shape=np.array(x_train).shape[1:])]
+ classifier = tf.estimator.DNNClassifier(
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
- # Fit and predict.
- classifier.fit(x_train, y_train, steps=200)
- predictions = list(classifier.predict(x_test, as_iterable=True))
- score = metrics.accuracy_score(y_test, predictions)
- print('Accuracy: {0:f}'.format(score))
+ # Train.
+ train_input_fn = tf.estimator.inputs.numpy_input_fn(
+ x={'x': x_train}, y=y_train, num_epochs=None, shuffle=True)
+ classifier.train(input_fn=train_input_fn, steps=200)
+
+ # Predict.
+ test_input_fn = tf.estimator.inputs.numpy_input_fn(
+ x={'x': x_test}, y=y_test, num_epochs=1, shuffle=False)
+ predictions = classifier.predict(input_fn=test_input_fn)
+ y_predicted = np.array(list(p['class_ids'] for p in predictions))
+ y_predicted = y_predicted.reshape(np.array(y_test).shape)
+
+ # Score with sklearn.
+ score = metrics.accuracy_score(y_test, y_predicted)
+ print('Accuracy (sklearn): {0:f}'.format(score))
+
+ # Score with tensorflow.
+ scores = classifier.evaluate(input_fn=test_input_fn)
+ print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
if __name__ == '__main__':