Created
September 28, 2017 17:46
-
-
Save yufengg/7dd4170cc730ce418a1dcd141532ec6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Copyright 2017 Google Inc.\n", | |
"#\n", | |
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n", | |
"# you may not use this file except in compliance with the License.\n", | |
"# You may obtain a copy of the License at\n", | |
"#\n", | |
"# http://www.apache.org/licenses/LICENSE-2.0\n", | |
"#\n", | |
"# Unless required by applicable law or agreed to in writing, software\n", | |
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n", | |
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | |
"# See the License for the specific language governing permissions and\n", | |
"# limitations under the License." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.3.0\n", | |
"INFO:tensorflow:Using default config.\n", | |
"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}\n", | |
"INFO:tensorflow:Create CheckpointSaverHook.\n", | |
"INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model/model.ckpt.\n", | |
"INFO:tensorflow:loss = 174.354, step = 1\n", | |
"INFO:tensorflow:global_step/sec: 551.517\n", | |
"INFO:tensorflow:loss = 11.9108, step = 101 (0.185 sec)\n", | |
"INFO:tensorflow:global_step/sec: 566.986\n", | |
"INFO:tensorflow:loss = 11.0975, step = 201 (0.176 sec)\n", | |
"INFO:tensorflow:global_step/sec: 831.443\n", | |
"INFO:tensorflow:loss = 7.06128, step = 301 (0.117 sec)\n", | |
"INFO:tensorflow:global_step/sec: 786.033\n", | |
"INFO:tensorflow:loss = 6.75643, step = 401 (0.127 sec)\n", | |
"INFO:tensorflow:global_step/sec: 878.983\n", | |
"INFO:tensorflow:loss = 4.96682, step = 501 (0.114 sec)\n", | |
"INFO:tensorflow:global_step/sec: 900.933\n", | |
"INFO:tensorflow:loss = 4.99079, step = 601 (0.111 sec)\n", | |
"INFO:tensorflow:global_step/sec: 899.166\n", | |
"INFO:tensorflow:loss = 4.42463, step = 701 (0.112 sec)\n", | |
"INFO:tensorflow:global_step/sec: 888.867\n", | |
"INFO:tensorflow:loss = 6.84585, step = 801 (0.112 sec)\n", | |
"INFO:tensorflow:global_step/sec: 897.738\n", | |
"INFO:tensorflow:loss = 4.07034, step = 901 (0.113 sec)\n", | |
"INFO:tensorflow:Saving checkpoints for 1000 into /tmp/iris_model/model.ckpt.\n", | |
"INFO:tensorflow:Loss for final step: 3.99844.\n", | |
"fit done\n", | |
"INFO:tensorflow:Starting evaluation at 2017-09-28-17:45:46\n", | |
"INFO:tensorflow:Restoring parameters from /tmp/iris_model/model.ckpt-1000\n", | |
"INFO:tensorflow:Evaluation [1/100]\n", | |
"INFO:tensorflow:Evaluation [2/100]\n", | |
"INFO:tensorflow:Evaluation [3/100]\n", | |
"INFO:tensorflow:Evaluation [4/100]\n", | |
"INFO:tensorflow:Evaluation [5/100]\n", | |
"INFO:tensorflow:Evaluation [6/100]\n", | |
"INFO:tensorflow:Evaluation [7/100]\n", | |
"INFO:tensorflow:Evaluation [8/100]\n", | |
"INFO:tensorflow:Evaluation [9/100]\n", | |
"INFO:tensorflow:Evaluation [10/100]\n", | |
"INFO:tensorflow:Evaluation [11/100]\n", | |
"INFO:tensorflow:Evaluation [12/100]\n", | |
"INFO:tensorflow:Evaluation [13/100]\n", | |
"INFO:tensorflow:Evaluation [14/100]\n", | |
"INFO:tensorflow:Evaluation [15/100]\n", | |
"INFO:tensorflow:Evaluation [16/100]\n", | |
"INFO:tensorflow:Evaluation [17/100]\n", | |
"INFO:tensorflow:Evaluation [18/100]\n", | |
"INFO:tensorflow:Evaluation [19/100]\n", | |
"INFO:tensorflow:Evaluation [20/100]\n", | |
"INFO:tensorflow:Evaluation [21/100]\n", | |
"INFO:tensorflow:Evaluation [22/100]\n", | |
"INFO:tensorflow:Evaluation [23/100]\n", | |
"INFO:tensorflow:Evaluation [24/100]\n", | |
"INFO:tensorflow:Evaluation [25/100]\n", | |
"INFO:tensorflow:Evaluation [26/100]\n", | |
"INFO:tensorflow:Evaluation [27/100]\n", | |
"INFO:tensorflow:Evaluation [28/100]\n", | |
"INFO:tensorflow:Evaluation [29/100]\n", | |
"INFO:tensorflow:Evaluation [30/100]\n", | |
"INFO:tensorflow:Evaluation [31/100]\n", | |
"INFO:tensorflow:Evaluation [32/100]\n", | |
"INFO:tensorflow:Evaluation [33/100]\n", | |
"INFO:tensorflow:Evaluation [34/100]\n", | |
"INFO:tensorflow:Evaluation [35/100]\n", | |
"INFO:tensorflow:Evaluation [36/100]\n", | |
"INFO:tensorflow:Evaluation [37/100]\n", | |
"INFO:tensorflow:Evaluation [38/100]\n", | |
"INFO:tensorflow:Evaluation [39/100]\n", | |
"INFO:tensorflow:Evaluation [40/100]\n", | |
"INFO:tensorflow:Evaluation [41/100]\n", | |
"INFO:tensorflow:Evaluation [42/100]\n", | |
"INFO:tensorflow:Evaluation [43/100]\n", | |
"INFO:tensorflow:Evaluation [44/100]\n", | |
"INFO:tensorflow:Evaluation [45/100]\n", | |
"INFO:tensorflow:Evaluation [46/100]\n", | |
"INFO:tensorflow:Evaluation [47/100]\n", | |
"INFO:tensorflow:Evaluation [48/100]\n", | |
"INFO:tensorflow:Evaluation [49/100]\n", | |
"INFO:tensorflow:Evaluation [50/100]\n", | |
"INFO:tensorflow:Evaluation [51/100]\n", | |
"INFO:tensorflow:Evaluation [52/100]\n", | |
"INFO:tensorflow:Evaluation [53/100]\n", | |
"INFO:tensorflow:Evaluation [54/100]\n", | |
"INFO:tensorflow:Evaluation [55/100]\n", | |
"INFO:tensorflow:Evaluation [56/100]\n", | |
"INFO:tensorflow:Evaluation [57/100]\n", | |
"INFO:tensorflow:Evaluation [58/100]\n", | |
"INFO:tensorflow:Evaluation [59/100]\n", | |
"INFO:tensorflow:Evaluation [60/100]\n", | |
"INFO:tensorflow:Evaluation [61/100]\n", | |
"INFO:tensorflow:Evaluation [62/100]\n", | |
"INFO:tensorflow:Evaluation [63/100]\n", | |
"INFO:tensorflow:Evaluation [64/100]\n", | |
"INFO:tensorflow:Evaluation [65/100]\n", | |
"INFO:tensorflow:Evaluation [66/100]\n", | |
"INFO:tensorflow:Evaluation [67/100]\n", | |
"INFO:tensorflow:Evaluation [68/100]\n", | |
"INFO:tensorflow:Evaluation [69/100]\n", | |
"INFO:tensorflow:Evaluation [70/100]\n", | |
"INFO:tensorflow:Evaluation [71/100]\n", | |
"INFO:tensorflow:Evaluation [72/100]\n", | |
"INFO:tensorflow:Evaluation [73/100]\n", | |
"INFO:tensorflow:Evaluation [74/100]\n", | |
"INFO:tensorflow:Evaluation [75/100]\n", | |
"INFO:tensorflow:Evaluation [76/100]\n", | |
"INFO:tensorflow:Evaluation [77/100]\n", | |
"INFO:tensorflow:Evaluation [78/100]\n", | |
"INFO:tensorflow:Evaluation [79/100]\n", | |
"INFO:tensorflow:Evaluation [80/100]\n", | |
"INFO:tensorflow:Evaluation [81/100]\n", | |
"INFO:tensorflow:Evaluation [82/100]\n", | |
"INFO:tensorflow:Evaluation [83/100]\n", | |
"INFO:tensorflow:Evaluation [84/100]\n", | |
"INFO:tensorflow:Evaluation [85/100]\n", | |
"INFO:tensorflow:Evaluation [86/100]\n", | |
"INFO:tensorflow:Evaluation [87/100]\n", | |
"INFO:tensorflow:Evaluation [88/100]\n", | |
"INFO:tensorflow:Evaluation [89/100]\n", | |
"INFO:tensorflow:Evaluation [90/100]\n", | |
"INFO:tensorflow:Evaluation [91/100]\n", | |
"INFO:tensorflow:Evaluation [92/100]\n", | |
"INFO:tensorflow:Evaluation [93/100]\n", | |
"INFO:tensorflow:Evaluation [94/100]\n", | |
"INFO:tensorflow:Evaluation [95/100]\n", | |
"INFO:tensorflow:Evaluation [96/100]\n", | |
"INFO:tensorflow:Evaluation [97/100]\n", | |
"INFO:tensorflow:Evaluation [98/100]\n", | |
"INFO:tensorflow:Evaluation [99/100]\n", | |
"INFO:tensorflow:Evaluation [100/100]\n", | |
"INFO:tensorflow:Finished evaluation at 2017-09-28-17:45:46\n", | |
"INFO:tensorflow:Saving dict for global step 1000: accuracy = 0.966667, average_loss = 0.0652359, global_step = 1000, loss = 1.95708\n", | |
"\n", | |
"Accuracy: 0.966667\n", | |
"INFO:tensorflow:Restoring parameters from /tmp/iris_model/model.ckpt-1000\n", | |
"INFO:tensorflow:Assets added to graph.\n", | |
"INFO:tensorflow:No assets to write.\n", | |
"INFO:tensorflow:SavedModel written to: /tmp/iris_model/export/1506620748/saved_model.pb\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'/tmp/iris_model/export/1506620748'" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import tensorflow as tf\n", | |
"import numpy as np\n", | |
"\n", | |
"print(tf.__version__)\n", | |
"\n", | |
"from tensorflow.contrib.learn.python.learn.datasets import base\n", | |
"\n", | |
"# Data files\n", | |
"IRIS_TRAINING = \"iris_training.csv\"\n", | |
"IRIS_TEST = \"iris_test.csv\"\n", | |
"\n", | |
"# Load datasets.\n", | |
"training_set = base.load_csv_with_header(filename=IRIS_TRAINING,\n", | |
" features_dtype=np.float32,\n", | |
" target_dtype=np.int)\n", | |
"test_set = base.load_csv_with_header(filename=IRIS_TEST,\n", | |
" features_dtype=np.float32,\n", | |
" target_dtype=np.int)\n", | |
"\n", | |
"# Specify that all features have real-value data\n", | |
"feature_name = \"flower_features\"\n", | |
"feature_columns = [tf.feature_column.numeric_column(feature_name, \n", | |
" shape=[4])]\n", | |
"classifier = tf.estimator.DNNClassifier(\n", | |
" feature_columns=feature_columns,\n", | |
" n_classes=3,\n", | |
" model_dir=\"/tmp/iris_model\",\n", | |
" hidden_units=[100, 70, 50, 25])\n", | |
"\n", | |
"def input_fn(dataset):\n", | |
" def _fn():\n", | |
" features = {feature_name: tf.constant(dataset.data)}\n", | |
" label = tf.constant(dataset.target)\n", | |
" return features, label\n", | |
" return _fn\n", | |
"\n", | |
"# Fit model.\n", | |
"classifier.train(input_fn=input_fn(training_set),\n", | |
" steps=1000)\n", | |
"print('fit done')\n", | |
"\n", | |
"# Evaluate accuracy.\n", | |
"accuracy_score = classifier.evaluate(input_fn=input_fn(test_set), \n", | |
" steps=100)[\"accuracy\"]\n", | |
"print('\\nAccuracy: {0:f}'.format(accuracy_score))\n", | |
"\n", | |
"# Export the model for serving\n", | |
"feature_spec = {'flower_features': tf.FixedLenFeature(shape=[4], dtype=np.float32)}\n", | |
"\n", | |
"serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)\n", | |
"\n", | |
"classifier.export_savedmodel(export_dir_base='/tmp/iris_model' + '/export', \n", | |
" serving_input_receiver_fn=serving_fn)\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment