# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
All the code we'll look at is in the next cell. We will step through each step after.
import tensorflow as tf
import numpy as np
print(tf.__version__)
from tensorflow.contrib.learn.python.learn.datasets import base
# Data files
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
# Load datasets.
training_set = base.load_csv_with_header(filename=IRIS_TRAINING,
features_dtype=np.float32,
target_dtype=np.int)
test_set = base.load_csv_with_header(filename=IRIS_TEST,
features_dtype=np.float32,
target_dtype=np.int)
# Specify that all features have real-value data
feature_name = "flower_features"
feature_columns = [tf.feature_column.numeric_column(feature_name,
shape=[4])]
classifier = tf.estimator.LinearClassifier(
feature_columns=feature_columns,
n_classes=3,
model_dir="/tmp/iris_model")
def input_fn(dataset):
def _fn():
features = {feature_name: tf.constant(dataset.data)}
label = tf.constant(dataset.target)
return features, label
return _fn
# Fit model.
classifier.train(input_fn=input_fn(training_set),
steps=1000)
print('fit done')
# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=input_fn(test_set),
steps=100)["accuracy"]
print('\nAccuracy: {0:f}'.format(accuracy_score))
# Export the model for serving
feature_spec = {'flower_features': tf.FixedLenFeature(shape=[4], dtype=np.float32)}
serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
classifier.export_savedmodel(export_dir_base='/tmp/iris_model' + '/export',
serving_input_receiver_fn=serving_fn)
import tensorflow as tf
import numpy as np
print(tf.__version__)
1.3.0
From https://en.wikipedia.org/wiki/Iris_flower_data_set
3 types of Iris Flowers:
* Iris Setosa * Iris Versicolour * Iris Virginicafrom tensorflow.contrib.learn.python.learn.datasets import base
# Data files
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
# Load datasets.
training_set = base.load_csv_with_header(filename=IRIS_TRAINING,
features_dtype=np.float32,
target_dtype=np.int)
test_set = base.load_csv_with_header(filename=IRIS_TEST,
features_dtype=np.float32,
target_dtype=np.int)
print(training_set.data)
print(training_set.target)
[[ 6.4000001 2.79999995 5.5999999 2.20000005] [ 5. 2.29999995 3.29999995 1. ] [ 4.9000001 2.5 4.5 1.70000005] [ 4.9000001 3.0999999 1.5 0.1 ] [ 5.69999981 3.79999995 1.70000005 0.30000001] [ 4.4000001 3.20000005 1.29999995 0.2 ] [ 5.4000001 3.4000001 1.5 0.40000001] [ 6.9000001 3.0999999 5.0999999 2.29999995] [ 6.69999981 3.0999999 4.4000001 1.39999998] [ 5.0999999 3.70000005 1.5 0.40000001] [ 5.19999981 2.70000005 3.9000001 1.39999998] [ 6.9000001 3.0999999 4.9000001 1.5 ] [ 5.80000019 4. 1.20000005 0.2 ] [ 5.4000001 3.9000001 1.70000005 0.40000001] [ 7.69999981 3.79999995 6.69999981 2.20000005] [ 6.30000019 3.29999995 4.69999981 1.60000002] [ 6.80000019 3.20000005 5.9000001 2.29999995] [ 7.5999999 3. 6.5999999 2.0999999 ] [ 6.4000001 3.20000005 5.30000019 2.29999995] [ 5.69999981 4.4000001 1.5 0.40000001] [ 6.69999981 3.29999995 5.69999981 2.0999999 ] [ 6.4000001 2.79999995 5.5999999 2.0999999 ] [ 5.4000001 3.9000001 1.29999995 0.40000001] [ 6.0999999 2.5999999 5.5999999 1.39999998] [ 7.19999981 3. 5.80000019 1.60000002] [ 5.19999981 3.5 1.5 0.2 ] [ 5.80000019 2.5999999 4. 1.20000005] [ 5.9000001 3. 5.0999999 1.79999995] [ 5.4000001 3. 4.5 1.5 ] [ 6.69999981 3. 5. 1.70000005] [ 6.30000019 2.29999995 4.4000001 1.29999995] [ 5.0999999 2.5 3. 1.10000002] [ 6.4000001 3.20000005 4.5 1.5 ] [ 6.80000019 3. 5.5 2.0999999 ] [ 6.19999981 2.79999995 4.80000019 1.79999995] [ 6.9000001 3.20000005 5.69999981 2.29999995] [ 6.5 3.20000005 5.0999999 2. ] [ 5.80000019 2.79999995 5.0999999 2.4000001 ] [ 5.0999999 3.79999995 1.5 0.30000001] [ 4.80000019 3. 1.39999998 0.30000001] [ 7.9000001 3.79999995 6.4000001 2. ] [ 5.80000019 2.70000005 5.0999999 1.89999998] [ 6.69999981 3. 5.19999981 2.29999995] [ 5.0999999 3.79999995 1.89999998 0.40000001] [ 4.69999981 3.20000005 1.60000002 0.2 ] [ 6. 2.20000005 5. 1.5 ] [ 4.80000019 3.4000001 1.60000002 0.2 ] [ 7.69999981 2.5999999 6.9000001 2.29999995] [ 4.5999999 3.5999999 1. 0.2 ] [ 7.19999981 3.20000005 6. 1.79999995] [ 5. 3.29999995 1.39999998 0.2 ] [ 6.5999999 3. 4.4000001 1.39999998] [ 6.0999999 2.79999995 4. 1.29999995] [ 5. 3.20000005 1.20000005 0.2 ] [ 7. 3.20000005 4.69999981 1.39999998] [ 6. 3. 4.80000019 1.79999995] [ 7.4000001 2.79999995 6.0999999 1.89999998] [ 5.80000019 2.70000005 5.0999999 1.89999998] [ 6.19999981 3.4000001 5.4000001 2.29999995] [ 5. 2. 3.5 1. ] [ 5.5999999 2.5 3.9000001 1.10000002] [ 6.69999981 3.0999999 5.5999999 2.4000001 ] [ 6.30000019 2.5 5. 1.89999998] [ 6.4000001 3.0999999 5.5 1.79999995] [ 6.19999981 2.20000005 4.5 1.5 ] [ 7.30000019 2.9000001 6.30000019 1.79999995] [ 4.4000001 3. 1.29999995 0.2 ] [ 7.19999981 3.5999999 6.0999999 2.5 ] [ 6.5 3. 5.5 1.79999995] [ 5. 3.4000001 1.5 0.2 ] [ 4.69999981 3.20000005 1.29999995 0.2 ] [ 6.5999999 2.9000001 4.5999999 1.29999995] [ 5.5 3.5 1.29999995 0.2 ] [ 7.69999981 3. 6.0999999 2.29999995] [ 6.0999999 3. 4.9000001 1.79999995] [ 4.9000001 3.0999999 1.5 0.1 ] [ 5.5 2.4000001 3.79999995 1.10000002] [ 5.69999981 2.9000001 4.19999981 1.29999995] [ 6. 2.9000001 4.5 1.5 ] [ 6.4000001 2.70000005 5.30000019 1.89999998] [ 5.4000001 3.70000005 1.5 0.2 ] [ 6.0999999 2.9000001 4.69999981 1.39999998] [ 6.5 2.79999995 4.5999999 1.5 ] [ 5.5999999 2.70000005 4.19999981 1.29999995] [ 6.30000019 3.4000001 5.5999999 2.4000001 ] [ 4.9000001 3.0999999 1.5 0.1 ] [ 6.80000019 2.79999995 4.80000019 1.39999998] [ 5.69999981 2.79999995 4.5 1.29999995] [ 6. 2.70000005 5.0999999 1.60000002] [ 5. 3.5 1.29999995 0.30000001] [ 6.5 3. 5.19999981 2. ] [ 6.0999999 2.79999995 4.69999981 1.20000005] [ 5.0999999 3.5 1.39999998 0.30000001] [ 4.5999999 3.0999999 1.5 0.2 ] [ 6.5 3. 5.80000019 2.20000005] [ 4.5999999 3.4000001 1.39999998 0.30000001] [ 4.5999999 3.20000005 1.39999998 0.2 ] [ 7.69999981 2.79999995 6.69999981 2. ] [ 5.9000001 3.20000005 4.80000019 1.79999995] [ 5.0999999 3.79999995 1.60000002 0.2 ] [ 4.9000001 3. 1.39999998 0.2 ] [ 4.9000001 2.4000001 3.29999995 1. ] [ 4.5 2.29999995 1.29999995 0.30000001] [ 5.80000019 2.70000005 4.0999999 1. ] [ 5. 3.4000001 1.60000002 0.40000001] [ 5.19999981 3.4000001 1.39999998 0.2 ] [ 5.30000019 3.70000005 1.5 0.2 ] [ 5. 3.5999999 1.39999998 0.2 ] [ 5.5999999 2.9000001 3.5999999 1.29999995] [ 4.80000019 3.0999999 1.60000002 0.2 ] [ 6.30000019 2.70000005 4.9000001 1.79999995] [ 5.69999981 2.79999995 4.0999999 1.29999995] [ 5. 3. 1.60000002 0.2 ] [ 6.30000019 3.29999995 6. 2.5 ] [ 5. 3.5 1.60000002 0.60000002] [ 5.5 2.5999999 4.4000001 1.20000005] [ 5.69999981 3. 4.19999981 1.20000005] [ 4.4000001 2.9000001 1.39999998 0.2 ] [ 4.80000019 3. 1.39999998 0.1 ] [ 5.5 2.4000001 3.70000005 1. ]] [2 1 2 0 0 0 0 2 1 0 1 1 0 0 2 1 2 2 2 0 2 2 0 2 2 0 1 2 1 1 1 1 1 2 2 2 2 2 0 0 2 2 2 0 0 2 0 2 0 2 0 1 1 0 1 2 2 2 2 1 1 2 2 2 1 2 0 2 2 0 0 1 0 2 2 0 1 1 1 2 0 1 1 1 2 0 1 1 1 0 2 1 0 0 2 0 0 2 1 0 0 1 0 1 0 0 0 0 1 0 2 1 0 2 0 1 1 0 0 1]
# Specify that all features have real-value data
feature_name = "flower_features"
feature_columns = [tf.feature_column.numeric_column(feature_name,
shape=[4])]
classifier = tf.estimator.LinearClassifier(
feature_columns=feature_columns,
n_classes=3,
model_dir="/tmp/iris_model")
INFO:tensorflow:Using default config. INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_tf_random_seed': 1, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_save_checkpoints_steps': None, '_model_dir': '/tmp/iris_model', '_save_summary_steps': 100}
def input_fn(dataset):
def _fn():
features = {feature_name: tf.constant(dataset.data)}
label = tf.constant(dataset.target)
return features, label
return _fn
print(input_fn(training_set)())
# raw data -> input function -> feature columns -> model
({'flower_features': <tf.Tensor 'Const:0' shape=(120, 4) dtype=float32>}, <tf.Tensor 'Const_1:0' shape=(120,) dtype=int64>)
# Fit model.
classifier.train(input_fn=input_fn(training_set),
steps=1000)
print('fit done')
INFO:tensorflow:Create CheckpointSaverHook. INFO:tensorflow:Restoring parameters from /tmp/iris_model/model.ckpt-3000 INFO:tensorflow:Saving checkpoints for 3001 into /tmp/iris_model/model.ckpt. INFO:tensorflow:loss = 8.50027, step = 3001 INFO:tensorflow:global_step/sec: 827.439 INFO:tensorflow:loss = 8.40806, step = 3101 (0.123 sec) INFO:tensorflow:global_step/sec: 868.825 INFO:tensorflow:loss = 8.32063, step = 3201 (0.116 sec) INFO:tensorflow:global_step/sec: 959.112 INFO:tensorflow:loss = 8.23757, step = 3301 (0.104 sec) INFO:tensorflow:global_step/sec: 844.444 INFO:tensorflow:loss = 8.15855, step = 3401 (0.118 sec) INFO:tensorflow:global_step/sec: 847.278 INFO:tensorflow:loss = 8.08324, step = 3501 (0.118 sec) INFO:tensorflow:global_step/sec: 825.594 INFO:tensorflow:loss = 8.01139, step = 3601 (0.120 sec) INFO:tensorflow:global_step/sec: 882.98 INFO:tensorflow:loss = 7.94273, step = 3701 (0.114 sec) INFO:tensorflow:global_step/sec: 941.876 INFO:tensorflow:loss = 7.87704, step = 3801 (0.106 sec) INFO:tensorflow:global_step/sec: 889.862 INFO:tensorflow:loss = 7.81412, step = 3901 (0.112 sec) INFO:tensorflow:Saving checkpoints for 4000 into /tmp/iris_model/model.ckpt. INFO:tensorflow:Loss for final step: 7.75437. fit done
## Evaluation
# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=input_fn(test_set),
steps=100)["accuracy"]
print('\nAccuracy: {0:f}'.format(accuracy_score))
INFO:tensorflow:Starting evaluation at 2017-09-14-15:16:40 INFO:tensorflow:Restoring parameters from /tmp/iris_model/model.ckpt-4000 INFO:tensorflow:Evaluation [1/100] INFO:tensorflow:Evaluation [2/100] INFO:tensorflow:Evaluation [3/100] INFO:tensorflow:Evaluation [4/100] INFO:tensorflow:Evaluation [5/100] INFO:tensorflow:Evaluation [6/100] INFO:tensorflow:Evaluation [7/100] INFO:tensorflow:Evaluation [8/100] INFO:tensorflow:Evaluation [9/100] INFO:tensorflow:Evaluation [10/100] INFO:tensorflow:Evaluation [11/100] INFO:tensorflow:Evaluation [12/100] INFO:tensorflow:Evaluation [13/100] INFO:tensorflow:Evaluation [14/100] INFO:tensorflow:Evaluation [15/100] INFO:tensorflow:Evaluation [16/100] INFO:tensorflow:Evaluation [17/100] INFO:tensorflow:Evaluation [18/100] INFO:tensorflow:Evaluation [19/100] INFO:tensorflow:Evaluation [20/100] INFO:tensorflow:Evaluation [21/100] INFO:tensorflow:Evaluation [22/100] INFO:tensorflow:Evaluation [23/100] INFO:tensorflow:Evaluation [24/100] INFO:tensorflow:Evaluation [25/100] INFO:tensorflow:Evaluation [26/100] INFO:tensorflow:Evaluation [27/100] INFO:tensorflow:Evaluation [28/100] INFO:tensorflow:Evaluation [29/100] INFO:tensorflow:Evaluation [30/100] INFO:tensorflow:Evaluation [31/100] INFO:tensorflow:Evaluation [32/100] INFO:tensorflow:Evaluation [33/100] INFO:tensorflow:Evaluation [34/100] INFO:tensorflow:Evaluation [35/100] INFO:tensorflow:Evaluation [36/100] INFO:tensorflow:Evaluation [37/100] INFO:tensorflow:Evaluation [38/100] INFO:tensorflow:Evaluation [39/100] INFO:tensorflow:Evaluation [40/100] INFO:tensorflow:Evaluation [41/100] INFO:tensorflow:Evaluation [42/100] INFO:tensorflow:Evaluation [43/100] INFO:tensorflow:Evaluation [44/100] INFO:tensorflow:Evaluation [45/100] INFO:tensorflow:Evaluation [46/100] INFO:tensorflow:Evaluation [47/100] INFO:tensorflow:Evaluation [48/100] INFO:tensorflow:Evaluation [49/100] INFO:tensorflow:Evaluation [50/100] INFO:tensorflow:Evaluation [51/100] INFO:tensorflow:Evaluation [52/100] INFO:tensorflow:Evaluation [53/100] INFO:tensorflow:Evaluation [54/100] INFO:tensorflow:Evaluation [55/100] INFO:tensorflow:Evaluation [56/100] INFO:tensorflow:Evaluation [57/100] INFO:tensorflow:Evaluation [58/100] INFO:tensorflow:Evaluation [59/100] INFO:tensorflow:Evaluation [60/100] INFO:tensorflow:Evaluation [61/100] INFO:tensorflow:Evaluation [62/100] INFO:tensorflow:Evaluation [63/100] INFO:tensorflow:Evaluation [64/100] INFO:tensorflow:Evaluation [65/100] INFO:tensorflow:Evaluation [66/100] INFO:tensorflow:Evaluation [67/100] INFO:tensorflow:Evaluation [68/100] INFO:tensorflow:Evaluation [69/100] INFO:tensorflow:Evaluation [70/100] INFO:tensorflow:Evaluation [71/100] INFO:tensorflow:Evaluation [72/100] INFO:tensorflow:Evaluation [73/100] INFO:tensorflow:Evaluation [74/100] INFO:tensorflow:Evaluation [75/100] INFO:tensorflow:Evaluation [76/100] INFO:tensorflow:Evaluation [77/100] INFO:tensorflow:Evaluation [78/100] INFO:tensorflow:Evaluation [79/100] INFO:tensorflow:Evaluation [80/100] INFO:tensorflow:Evaluation [81/100] INFO:tensorflow:Evaluation [82/100] INFO:tensorflow:Evaluation [83/100] INFO:tensorflow:Evaluation [84/100] INFO:tensorflow:Evaluation [85/100] INFO:tensorflow:Evaluation [86/100] INFO:tensorflow:Evaluation [87/100] INFO:tensorflow:Evaluation [88/100] INFO:tensorflow:Evaluation [89/100] INFO:tensorflow:Evaluation [90/100] INFO:tensorflow:Evaluation [91/100] INFO:tensorflow:Evaluation [92/100] INFO:tensorflow:Evaluation [93/100] INFO:tensorflow:Evaluation [94/100] INFO:tensorflow:Evaluation [95/100] INFO:tensorflow:Evaluation [96/100] INFO:tensorflow:Evaluation [97/100] INFO:tensorflow:Evaluation [98/100] INFO:tensorflow:Evaluation [99/100] INFO:tensorflow:Evaluation [100/100] INFO:tensorflow:Finished evaluation at 2017-09-14-15:16:41 INFO:tensorflow:Saving dict for global step 4000: accuracy = 0.966667, average_loss = 0.0706094, global_step = 4000, loss = 2.11828 Accuracy: 0.966667
training_data = load_csv_with_header()
def input_fn(dataset)
feature_columns = [tf.feature_column.numeric_column(feature_name, shape=[4])]
classifier = tf.estimator.LinearClassifier()
classifier.train()
classifier.evaluate()
feature_spec = {'flower_features': tf.FixedLenFeature(shape=[4], dtype=np.float32)}
serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
classifier.export_savedmodel(export_dir_base='/tmp/iris_model' + '/export',
serving_input_receiver_fn=serving_fn)
INFO:tensorflow:Restoring parameters from /tmp/iris_model/model.ckpt-4000 INFO:tensorflow:Assets added to graph. INFO:tensorflow:No assets to write. INFO:tensorflow:SavedModel written to: /tmp/iris_model/export/1505402201/saved_model.pb
'/tmp/iris_model/export/1505402201'