Tensorflow – A working MNIST Example notebook for starters

Key Features:

  • Download training and testing data
  • Pass the data frame to tensorflow
  • Build 3 layer DNN with 10, 20, 10 units
  • Fit model
  • Evaluate accuracy
  • Classify two new flower samples

Notebook:

In [1]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np

tf.logging.set_verbosity(tf.logging.INFO)
In [2]:
import csv
import tempfile
import urllib
import urllib2

#Location for training and test data set
#http://download.tensorflow.org/data/iris_training.csv
#http://download.tensorflow.org/data/iris_test.csv

#url = 'http://download.tensorflow.org/data/iris_training.csv'
#response = urllib2.urlopen(url)
#IRIS_TRAIN = csv.reader(response)

#url = 'http://download.tensorflow.org/data/iris_test.csv'
#response = urllib2.urlopen(url)
#IRIS_TEST = csv.reader(response)
In [3]:
# TRAINING 
train_file = tempfile.NamedTemporaryFile(delete=False)
urllib.urlretrieve("http://download.tensorflow.org/data/iris_training.csv", train_file.name)
train_file_name = train_file.name
train_file.close()
print("Training data is downloaded to %s" % train_file_name)
    
Training data is downloaded to /tmp/tmpMORAXc
In [4]:
# TEST DATA
test_file = tempfile.NamedTemporaryFile(delete=False)
urllib.urlretrieve("http://download.tensorflow.org/data/iris_test.csv", test_file.name)
test_file_name = test_file.name
test_file.close()
print("Training data is downloaded to %s" % test_file_name)
Training data is downloaded to /tmp/tmpJEW5rv
In [5]:
# Data sets
#IRIS_TRAINING = "iris_training.csv"
#IRIS_TEST = "iris_test.csv"
IRIS_TRAINING = train_file_name
IRIS_TEST = test_file_name
In [7]:
# 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)
In [10]:
# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
In [11]:
feature_columns
Out[11]:
[_RealValuedColumn(column_name='', dimension=4, default_value=None, dtype=tf.float32, normalizer=None)]
In [12]:
# 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")
WARNING:tensorflow:Change warning: default value of `enable_centered_bias` will change after 2016-10-09. It will be disabled by default.Instructions for keeping existing behaviour:
Explicitly set `enable_centered_bias` to 'True' if you want to keep existing behaviour.
WARNING:tensorflow:Using default config.
INFO:tensorflow:Using config: {'task': 0, 'save_summary_steps': 100, 'keep_checkpoint_max': 5, '_is_chief': True, 'save_checkpoints_secs': 600, 'evaluation_master': '', 'tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, 'master': '', 'keep_checkpoint_every_n_hours': 10000, '_job_name': None, 'cluster_spec': None, 'tf_random_seed': None, 'num_ps_replicas': 0}
In [13]:
# Fit model.
classifier.fit(x=training_set.data,
               y=training_set.target,
               steps=2000)
INFO:tensorflow:Setting feature info to TensorSignature(dtype=tf.float32, shape=TensorShape([Dimension(None), Dimension(4)]), is_sparse=False)
INFO:tensorflow:Setting targets info to TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(None)]), is_sparse=False)
INFO:tensorflow:Transforming feature_column _RealValuedColumn(column_name='', dimension=4, default_value=None, dtype=tf.float32, normalizer=None)
INFO:tensorflow:Create CheckpointSaverHook
INFO:tensorflow:loss = 1.30827, step = 1
INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:loss = 0.23881, step = 101
INFO:tensorflow:loss = 0.0918469, step = 201
INFO:tensorflow:loss = 0.0948484, step = 301
INFO:tensorflow:loss = 0.0614423, step = 401
INFO:tensorflow:loss = 0.0585238, step = 501
INFO:tensorflow:loss = 0.0556661, step = 601
INFO:tensorflow:loss = 0.0546077, step = 701
INFO:tensorflow:loss = 0.0524153, step = 801
INFO:tensorflow:loss = 0.0511613, step = 901
INFO:tensorflow:loss = 0.0499391, step = 1001
INFO:tensorflow:loss = 0.0488964, step = 1101
INFO:tensorflow:loss = 0.0479813, step = 1201
INFO:tensorflow:loss = 0.0471655, step = 1301
INFO:tensorflow:loss = 0.046428, step = 1401
INFO:tensorflow:loss = 0.0456749, step = 1501
INFO:tensorflow:loss = 0.0450335, step = 1601
INFO:tensorflow:loss = 0.0444158, step = 1701
INFO:tensorflow:loss = 0.0437809, step = 1801
INFO:tensorflow:loss = 0.0432727, step = 1901
INFO:tensorflow:Saving checkpoints for 2000 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:Loss for final step: 0.0428384.
Out[13]:
Estimator(params={'enable_centered_bias': True, 'activation_fn': , 'weight_column_name': None, 'hidden_units': [10, 20, 10], 'feature_columns': [_RealValuedColumn(column_name='', dimension=4, default_value=None, dtype=tf.float32, normalizer=None)], 'n_classes': 3, 'optimizer': 'Adagrad', 'dropout': None, 'gradient_clip_norm': None, 'num_ps_replicas': 0})
In [14]:
# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=test_set.data,
                                     y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
WARNING:tensorflow:Given features: Tensor("input:0", shape=(?, 4), dtype=float32), required signatures: TensorSignature(dtype=tf.float32, shape=TensorShape([Dimension(None), Dimension(4)]), is_sparse=False).
WARNING:tensorflow:Given targets: Tensor("output:0", shape=(?,), dtype=int64), required signatures: TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(None)]), is_sparse=False).
INFO:tensorflow:Transforming feature_column _RealValuedColumn(column_name='', dimension=4, default_value=None, dtype=tf.float32, normalizer=None)
INFO:tensorflow:Restored model from /tmp/iris_model
INFO:tensorflow:Eval steps [0,inf) for training step 2000.
INFO:tensorflow:Input iterator is exhausted.
INFO:tensorflow:Saving evaluation summary for 2000 step: loss = 0.0670678, accuracy = 0.966667
Accuracy: 0.966667
In [15]:
# Classify two new flower samples.
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
INFO:tensorflow:Transforming feature_column _RealValuedColumn(column_name='', dimension=4, default_value=None, dtype=tf.float32, normalizer=None)
INFO:tensorflow:Loading model from checkpoint: /tmp/iris_model/model.ckpt-2000-?????-of-00001.
Predictions: [array([1]), array([2])]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s