安装tensorflow 1.5版本之后,运行简单的iris分类。
参考官网: https://www.tensorflow.org/get_started/premade_estimators
iris分类,根据sepals and petals.的量化值进行分类, 总共有3类,Iris setosa (by Radomil, CC BY-SA 3.0), Iris versicolor (by Dlanglois, CC BY-SA 3.0), and Iris virginica (by Frank Mayfield, CC BY-SA 2.0).
训练集
120,4,setosa,versicolor,virginica
6.4,2.8,5.6,2.2,2
5.0,2.3,3.3,1.0,1
4.9,2.5,4.5,1.7,2
4.9,3.1,1.5,0.1,0
5.7,3.8,1.7,0.3,0
4.4,3.2,1.3,0.2,0
5.4,3.4,1.5,0.4,0
6.9,3.1,5.1,2.3,2
5.4,3.9,1.7,0.4,0
7.7,3.8,6.7,2.2,2
6.3,3.3,4.7,1.6,1
6.8,3.2,5.9,2.3,2
7.6,3.0,6.6,2.1,2
6.4,3.2,5.3,2.3,2
5.7,4.4,1.5,0.4,0
测试集
30,4,setosa,versicolor,virginica
5.9,3.0,4.2,1.5,1
6.9,3.1,5.4,2.1,2
5.1,3.3,1.7,0.5,0
6.0,3.4,4.5,1.6,1
5.5,2.5,4.0,1.3,1
6.2,2.9,4.3,1.3,1
5.5,4.2,1.4,0.2,0
6.3,2.8,5.1,1.5,2
5.6,3.0,4.1,1.3,1
6.7,2.5,5.8,1.8,2
7.1,3.0,5.9,2.1,2
4.3,3.0,1.1,0.1,0
5.6,2.8,4.9,2.0,2
5.5,2.3,4.0,1.3,1
6.0,2.2,4.0,1.0,1
5.1,3.5,1.4,0.2,0
5.7,2.6,3.5,1.0,1
4.8,3.4,1.9,0.2,0
5.1,3.4,1.5,0.2,0
5.7,2.5,5.0,2.0,2
5.4,3.4,1.7,0.2,0
from __future__ import division from __future__ import print_function import os import urllib import numpy as np import tensorflow as tf # Data sets IRIS_TRAINING = "iris_training.csv" IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv" IRIS_TEST = "iris_test.csv" IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv" def main(): # If the training and test sets aren't stored locally, download them. if not os.path.exists(IRIS_TRAINING): raw = urllib.urlopen(IRIS_TRAINING_URL).read() with open(IRIS_TRAINING, "w") as f: f.write(raw) if not os.path.exists(IRIS_TEST): raw = urllib.urlopen(IRIS_TEST_URL).read() with open(IRIS_TEST, "w") as f: f.write(raw) # Load datasets. featuresColumns, targe training_set = tf.contrib.learn.datasets.base.load_csv_with_header( filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32) test_set = tf.contrib.learn.datasets.base.load_csv_with_header( filename=IRIS_TEST, target_dtype=np.int, features_dtype=np.float32) # Specify that all features have real-value data feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)] # Build 3 layer DNN with 10, 20, 10 units respectively. classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10, 20, 10],n_classes=3,model_dir="./iris_model") # Define the training inputs def get_train_inputs(): x = tf.constant(training_set.data) y = tf.constant(training_set.target) return x, y # Fit model. classifier.fit(input_fn=get_train_inputs, steps=2000) # Define the test inputs def get_test_inputs(): x = tf.constant(test_set.data) y = tf.constant(test_set.target) return x, y # Evaluate accuracy. accuracy_score = classifier.evaluate(input_fn=get_test_inputs, steps=1)["accuracy"] print(" Test Accuracy: {0:f} ".format(accuracy_score)) # Classify two new flower samples. def new_samples(): # 2 * 4 return np.array( [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=np.float32) predictions = list(classifier.predict(input_fn=new_samples)) print( "New Samples, Class Predictions: {} " .format(predictions)) if __name__ == "__main__": main() ~ ~
from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import os import sys import tempfile import numpy as np from six.moves import urllib import tensorflow as tf from tensorflow.contrib.learn.python.learn import experiment from tensorflow.contrib.learn.python.learn.datasets import base from tensorflow.python import debug as tf_debug # URLs to download data sets from, if necessary. IRIS_TRAINING_DATA_URL = "https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/monitors/iris_training.csv" IRIS_TEST_DATA_URL = "https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/monitors/iris_test.csv" def maybe_download_data(data_dir): """Download data sets if necessary. Args: data_dir: Path to where data should be downloaded. Returns: Paths to the training and test data files. """ if not os.path.isdir(data_dir): os.makedirs(data_dir) training_data_path = os.path.join(data_dir, os.path.basename(IRIS_TRAINING_DATA_URL)) if not os.path.isfile(training_data_path): train_file = open(training_data_path, "wt") urllib.request.urlretrieve(IRIS_TRAINING_DATA_URL, train_file.name) train_file.close() print("Training data are downloaded to %s" % train_file.name) test_data_path = os.path.join(data_dir, os.path.basename(IRIS_TEST_DATA_URL)) if not os.path.isfile(test_data_path): test_file = open(test_data_path, "wt") urllib.request.urlretrieve(IRIS_TEST_DATA_URL, test_file.name) test_file.close() print("Test data are downloaded to %s" % test_file.name) return training_data_path, test_data_path _IRIS_INPUT_DIM = 4 def iris_input_fn(): iris = base.load_iris() #nsamples*nfeats features = tf.reshape(tf.constant(iris.data), [-1, _IRIS_INPUT_DIM]) #nsamples* labels = tf.reshape(tf.constant(iris.target), [-1]) return features, labels def main(_): # Load datasets. if FLAGS.fake_data: training_set = tf.contrib.learn.datasets.base.Dataset( np.random.random([120, 4]), np.random.random_integers(3, size=[120]) - 1) test_set = tf.contrib.learn.datasets.base.Dataset( np.random.random([30, 4]), np.random.random_integers(3, size=[30]) - 1) else: training_data_path, test_data_path = maybe_download_data(FLAGS.data_dir) training_set = tf.contrib.learn.datasets.base.load_csv_with_header( filename=training_data_path, target_dtype=np.int, features_dtype=np.float32) test_set = tf.contrib.learn.datasets.base.load_csv_with_header( filename=test_data_path, target_dtype=np.int, features_dtype=np.float32) # Specify that all features have real-value data feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)] # Build 3 layer DNN with 10, 20, 10 units respectively. model_dir = FLAGS.model_dir or tempfile.mkdtemp(prefix="debug_tflearn_iris_") classifier = tf.contrib.learn.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3, model_dir=model_dir) hooks = None if FLAGS.debug: debug_hook = tf_debug.LocalCLIDebugHook(ui_type=FLAGS.ui_type, dump_root=FLAGS.dump_root) hooks = [debug_hook] if not FLAGS.use_experiment: # Fit model. classifier.fit(x=training_set.data, y=training_set.target, steps=FLAGS.train_steps, monitors=hooks) # Evaluate accuracy. accuracy_score = classifier.evaluate(x=test_set.data, y=test_set.target, hooks=hooks)["accuracy"] else: ex = experiment.Experiment(classifier, train_input_fn=iris_input_fn, eval_input_fn=iris_input_fn, train_steps=FLAGS.train_steps, eval_delay_secs=0, eval_steps=1, train_monitors=hooks, eval_hooks=hooks) ex.train() accuracy_score = ex.evaluate()["accuracy"] print("After training %d steps, Accuracy = %f" % (FLAGS.train_steps, accuracy_score)) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.register("type", "bool", lambda v: v.lower() == "true") parser.add_argument( "--data_dir", type=str, default="/tmp/iris_data", help="Directory to save the training and test data in.") parser.add_argument( "--model_dir", type=str, default="", help="Directory to save the trained model in.") parser.add_argument( "--train_steps", type=int, default=10, help="Number of steps to run trainer.") parser.add_argument( "--use_experiment", type="bool", nargs="?", const=True, default=False, help="Use tf.contrib.learn Experiment to run training and evaluation") parser.add_argument( "--ui_type", type=str, default="curses", help="Command-line user interface type (curses | readline)") parser.add_argument( "--fake_data", type="bool", nargs="?", const=True, default=False, help="Use fake MNIST data for unit testing") parser.add_argument( "--debug", type="bool", nargs="?", const=True, default=False, help="Use debugger to track down bad values during training") parser.add_argument( "--dump_root", type=str, default="", help="Optional custom root directory for temporary debug dump data") FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) ~