In [8]:
from __future__ import absolute_import
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.
  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="/tmp/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("\nTest Accuracy: {0:f}\n".format(accuracy_score))

  # Classify two new flower samples.
  def new_samples():
    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:    {}\n"
      .format(predictions))

if __name__ == "__main__":
    main()


INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_num_ps_replicas': 0, '_keep_checkpoint_max': 5, '_tf_random_seed': None, '_task_type': None, '_environment': 'local', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x6982ef30>, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': ''}
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:loss = 1.93453, step = 1
INFO:tensorflow:global_step/sec: 115.28
INFO:tensorflow:loss = 1.09608, step = 101
INFO:tensorflow:global_step/sec: 128.192
INFO:tensorflow:loss = 1.09607, step = 201
INFO:tensorflow:global_step/sec: 128.54
INFO:tensorflow:loss = 1.09607, step = 301
INFO:tensorflow:global_step/sec: 132.66
INFO:tensorflow:loss = 1.09607, step = 401
INFO:tensorflow:global_step/sec: 129.689
INFO:tensorflow:loss = 1.09607, step = 501
INFO:tensorflow:global_step/sec: 127.194
INFO:tensorflow:loss = 1.09607, step = 601
INFO:tensorflow:global_step/sec: 127.367
INFO:tensorflow:loss = 1.09607, step = 701
INFO:tensorflow:global_step/sec: 127.626
INFO:tensorflow:loss = 1.09607, step = 801
INFO:tensorflow:global_step/sec: 124.069
INFO:tensorflow:loss = 1.09607, step = 901
INFO:tensorflow:global_step/sec: 129.075
INFO:tensorflow:loss = 1.09607, step = 1001
INFO:tensorflow:global_step/sec: 127.563
INFO:tensorflow:loss = 1.09607, step = 1101
INFO:tensorflow:global_step/sec: 128.849
INFO:tensorflow:loss = 1.09607, step = 1201
INFO:tensorflow:global_step/sec: 124.76
INFO:tensorflow:loss = 1.09607, step = 1301
INFO:tensorflow:global_step/sec: 128.621
INFO:tensorflow:loss = 1.09607, step = 1401
INFO:tensorflow:global_step/sec: 131.069
INFO:tensorflow:loss = 1.09607, step = 1501
INFO:tensorflow:global_step/sec: 125.608
INFO:tensorflow:loss = 1.09607, step = 1601
INFO:tensorflow:global_step/sec: 118.155
INFO:tensorflow:loss = 1.09607, step = 1701
INFO:tensorflow:global_step/sec: 128.63
INFO:tensorflow:loss = 1.09607, step = 1801
INFO:tensorflow:global_step/sec: 129.359
INFO:tensorflow:loss = 1.09607, step = 1901
INFO:tensorflow:Saving checkpoints for 2000 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:Loss for final step: 1.09607.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-04-04-23:19:19
INFO:tensorflow:Evaluation [1/1]
INFO:tensorflow:Finished evaluation at 2017-04-04-23:19:23
INFO:tensorflow:Saving dict for global step 2000: accuracy = 0.266667, auc = 0.399998, global_step = 2000, loss = 1.12176
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.

Test Accuracy: 0.266667

New Samples, Class Predictions:    [0, 0]


In [ ]: