Home | History | Annotate | Download | only in learn
      1 #  Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 #  Licensed under the Apache License, Version 2.0 (the "License");
      4 #  you may not use this file except in compliance with the License.
      5 #  You may obtain a copy of the License at
      6 #
      7 #   http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 #  Unless required by applicable law or agreed to in writing, software
     10 #  distributed under the License is distributed on an "AS IS" BASIS,
     11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 #  See the License for the specific language governing permissions and
     13 #  limitations under the License.
     14 
     15 """Example of DNNClassifier for Iris plant dataset, with run config."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 import numpy as np
     22 from sklearn import datasets
     23 from sklearn import metrics
     24 from sklearn import model_selection
     25 import tensorflow as tf
     26 
     27 
     28 X_FEATURE = 'x'  # Name of the input feature.
     29 
     30 
     31 def main(unused_argv):
     32   # Load dataset.
     33   iris = datasets.load_iris()
     34   x_train, x_test, y_train, y_test = model_selection.train_test_split(
     35       iris.data, iris.target, test_size=0.2, random_state=42)
     36 
     37   # You can define you configurations by providing a RunConfig object to
     38   # estimator to control session configurations, e.g. tf_random_seed.
     39   run_config = tf.estimator.RunConfig().replace(tf_random_seed=1)
     40 
     41   # Build 3 layer DNN with 10, 20, 10 units respectively.
     42   feature_columns = [
     43       tf.feature_column.numeric_column(
     44           X_FEATURE, shape=np.array(x_train).shape[1:])]
     45   classifier = tf.estimator.DNNClassifier(
     46       feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3,
     47       config=run_config)
     48 
     49   # Train.
     50   train_input_fn = tf.estimator.inputs.numpy_input_fn(
     51       x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True)
     52   classifier.train(input_fn=train_input_fn, steps=200)
     53 
     54   # Predict.
     55   test_input_fn = tf.estimator.inputs.numpy_input_fn(
     56       x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
     57   predictions = classifier.predict(input_fn=test_input_fn)
     58   y_predicted = np.array(list(p['class_ids'] for p in predictions))
     59   y_predicted = y_predicted.reshape(np.array(y_test).shape)
     60 
     61   # Score with sklearn.
     62   score = metrics.accuracy_score(y_test, y_predicted)
     63   print('Accuracy (sklearn): {0:f}'.format(score))
     64 
     65   # Score with tensorflow.
     66   scores = classifier.evaluate(input_fn=test_input_fn)
     67   print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
     68 
     69 
     70 if __name__ == '__main__':
     71   tf.app.run()
     72