Skip to content

Commit

Permalink
bit more refactoring to bring all routines together. still not there …
Browse files Browse the repository at this point in the history
…yet.
  • Loading branch information
lene committed Feb 17, 2016
1 parent 762d0d2 commit fe4dbba
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ __pycache__

# if tensorflow source is downloaded in this directory, ignore it
tensorflow

# ignore autogenerated summary data files
data-*
2 changes: 1 addition & 1 deletion ACKNOWLEDGEMENTS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ nn-wtf was started using code examples from the TensorFlow library by Google Inc
TensorFlow homepage: http://www.tensorflow.org
TensorFlow on GitHub: https://github.com/tensorflow/tensorflow

TensorFlow is distributed underr the Apache License V2.0. See the file LICENSE.md for details.
TensorFlow is distributed under the Apache License V2.0. See the file LICENSE.md for details.
14 changes: 6 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@
flags.DEFINE_integer('max_steps', 2000, 'Number of steps to run trainer.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.')



flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data for unit testing.')


def run_training():
Expand All @@ -49,7 +44,10 @@ def run_training():

# Tell TensorFlow that the model will be built into the default Graph.
with tf.Graph().as_default():
graph = MNISTGraph(FLAGS)
graph = MNISTGraph(
learning_rate=FLAGS.learning_rate, max_steps=FLAGS.max_steps, hidden1=FLAGS.hidden1,
hidden2=FLAGS.hidden2, batch_size=FLAGS.batch_size, train_dir=FLAGS.train_dir
)
graph.run_training_graph(data_sets)


Expand Down
36 changes: 0 additions & 36 deletions mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,6 @@
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE


def inference(images, hidden1_units, hidden2_units):
"""Build the MNIST model up to where it may be used for inference.
Args:
images: Images placeholder, from inputs().
hidden1_units: Size of the first hidden layer.
hidden2_units: Size of the second hidden layer.
Returns:
softmax_linear: Output tensor with the computed logits.
"""
hidden1 = add_layer('hidden1', IMAGE_PIXELS, hidden1_units, images, tf.nn.relu)
hidden2 = add_layer('hidden2', hidden1_units, hidden2_units, hidden1, tf.nn.relu)
logits = add_layer('softmax_linear', hidden2_units, NUM_CLASSES, hidden2)
return logits


def add_layer(layer_name, in_units_size, out_units_size, input_layer, function=lambda x: x):
with tf.name_scope(layer_name):
weights = initialize_weights(in_units_size, out_units_size)
biases = initialize_biases(out_units_size)
new_layer = function(tf.matmul(input_layer, weights) + biases)
return new_layer


def initialize_weights(in_units_size, out_units_size):
return tf.Variable(
tf.truncated_normal([in_units_size, out_units_size], stddev=1.0 / math.sqrt(float(in_units_size))),
name='weights'
)


def initialize_biases(out_units_size):
return tf.Variable(tf.zeros([out_units_size]), name='biases')


def loss(logits, labels):
"""Calculates the loss from the logits and the labels.
Expand Down
123 changes: 92 additions & 31 deletions mnist_graph.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
import time
import math

import tensorflow as tf

import mnist
from mnist import NUM_CLASSES

__author__ = 'Lene Preuss <lp@sinnwerkstatt.com>'


class MNISTGraph:

def __init__(self, flags):
self.flags = flags
def __init__(
self,
learning_rate=0.01, max_steps=2000, hidden1=128, hidden2=32,
batch_size=100, train_dir='data'
):
# self.flags = flags
self.learning_rate = learning_rate
self.max_steps = max_steps
self.hidden1 = hidden1
self.hidden2 = hidden2
self.batch_size = batch_size
self.train_dir = train_dir
self.fake_data = False

self._build_graph()
self._setup_summaries()

def _build_graph(self):
# Generate placeholders for the images and labels.
self.images_placeholder, self.labels_placeholder = placeholder_inputs(self.flags.batch_size)
# Build a Graph that computes predictions from the inference model.
self.logits = mnist.inference(self.images_placeholder, self.flags.hidden1, self.flags.hidden2)
# Add to the Graph the Ops for loss calculation.
self.loss = mnist.loss(self.logits, self.labels_placeholder)
# Add to the Graph the Ops that calculate and apply gradients.
self.train_op = mnist.training(self.loss, self.flags.learning_rate)
# Add the Op to compare the logits to the labels during evaluation.
self.eval_correct = mnist.evaluation(self.logits, self.labels_placeholder)

def _setup_summaries(self):
# Build the summary operation based on the TF collection of Summaries.
self.summary_op = tf.merge_all_summaries()
# Create a saver for writing training checkpoints.
self.saver = tf.train.Saver()
self.summary_writer = None

def run_training_graph(self, data_sets):
session = self.initialize_session()

# And then after everything is built, start the training loop.
for step in range(self.flags.max_steps):
for step in range(self.max_steps):
start_time = time.time()

# Fill a feed dictionary with the actual set of images and labels for this particular
Expand All @@ -57,8 +52,8 @@ def run_training_graph(self, data_sets):
self.write_summary(duration, feed_dict, loss_value, session, step)

# Save a checkpoint and evaluate the model periodically.
if (step + 1) % 1000 == 0 or (step + 1) == self.flags.max_steps:
self.saver.save(session, self.flags.train_dir, global_step=step)
if (step + 1) % 1000 == 0 or (step + 1) == self.max_steps:
self.saver.save(session, self.train_dir, global_step=step)
self.print_evaluations(data_sets, session)

def print_evaluations(self, data_sets, session):
Expand All @@ -79,7 +74,7 @@ def initialize_session(self):
init = tf.initialize_all_variables()
sess.run(init)
# Instantiate a SummaryWriter to output summaries and the Graph.
self.summary_writer = tf.train.SummaryWriter(self.flags.train_dir, graph_def=sess.graph_def)
self.summary_writer = tf.train.SummaryWriter(self.train_dir, graph_def=sess.graph_def)
return sess

def fill_feed_dict(self, data_set):
Expand All @@ -98,7 +93,7 @@ def fill_feed_dict(self, data_set):
feed_dict: The feed dictionary mapping from placeholders to values.
"""
# Create the feed_dict for the placeholders filled with the next `batch size ` examples.
images_feed, labels_feed = data_set.next_batch(self.flags.batch_size, self.flags.fake_data)
images_feed, labels_feed = data_set.next_batch(self.batch_size, self.fake_data)
feed_dict = {
self.images_placeholder: images_feed,
self.labels_placeholder: labels_feed,
Expand All @@ -121,15 +116,42 @@ def do_eval(self, session, data_set):
input_data.read_data_sets().
"""
true_count = 0 # Counts the number of correct predictions.
steps_per_epoch = data_set.num_examples // self.flags.batch_size
num_examples = steps_per_epoch * self.flags.batch_size
steps_per_epoch = data_set.num_examples // self.batch_size
num_examples = steps_per_epoch * self.batch_size
for _ in range(steps_per_epoch):
feed_dict = self.fill_feed_dict(data_set)
true_count += session.run(self.eval_correct, feed_dict=feed_dict)
precision = true_count / num_examples
print(' Num examples: %d Num correct: %d Precision @ 1: %0.04f' %
(num_examples, true_count, precision))

def _build_graph(self):
# Generate placeholders for the images and labels.
self.images_placeholder, self.labels_placeholder = placeholder_inputs(self.batch_size)

# Build a Graph that computes predictions from the inference model.
self.logits = build_neural_network(
self.images_placeholder,
(self.hidden1, self.hidden2),
NUM_CLASSES
)

# Add to the Graph the Ops for loss calculation.
self.loss = mnist.loss(self.logits, self.labels_placeholder)

# Add to the Graph the Ops that calculate and apply gradients.
self.train_op = mnist.training(self.loss, self.learning_rate)

# Add the Op to compare the logits to the labels during evaluation.
self.eval_correct = mnist.evaluation(self.logits, self.labels_placeholder)

def _setup_summaries(self):
# Build the summary operation based on the TF collection of Summaries.
self.summary_op = tf.merge_all_summaries()
# Create a saver for writing training checkpoints.
self.saver = tf.train.Saver()
self.summary_writer = None


def placeholder_inputs(batch_size):
"""Generate placeholder variables to represent the input tensors.
Expand All @@ -144,11 +166,50 @@ def placeholder_inputs(batch_size):
images_placeholder: Images placeholder.
labels_placeholder: Labels placeholder.
"""
# Note that the shapes of the placeholders match the shapes of the full
# image and label tensors, except the first dimension is now batch_size
# rather than the full size of the train or test data sets.
# Note that the shapes of the placeholders match the shapes of the full image and label
# tensors, except the first dimension is now batch_size rather than the full size of
# the train or test data sets.
images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
mnist.IMAGE_PIXELS))
labels_placeholder = tf.placeholder(tf.int32, shape=batch_size)
return images_placeholder, labels_placeholder


def build_neural_network(images, hidden_layer_sizes, output_size):
"""Build the MNIST model up to where it may be used for inference.
Args:
images: Images placeholder, from inputs().
hidden1_units: Size of the first hidden layer.
hidden2_units: Size of the second hidden layer.
Returns:
softmax_linear: Output tensor with the computed logits.
"""
input_size = int(images.get_shape()[1])
# hidden1 = add_layer('hidden1', IMAGE_PIXELS, hidden_layer_sizes[0], images, tf.nn.relu)
hidden1 = add_layer('hidden1', input_size, hidden_layer_sizes[0], images, tf.nn.relu)
hidden2 = add_layer('hidden2', hidden_layer_sizes[0], hidden_layer_sizes[1], hidden1, tf.nn.relu)
logits = add_layer('softmax_linear', hidden_layer_sizes[1], output_size, hidden2)
return logits


def add_layer(layer_name, in_units_size, out_units_size, input_layer, function=lambda x: x):
with tf.name_scope(layer_name):
weights = initialize_weights(in_units_size, out_units_size)
biases = initialize_biases(out_units_size)
new_layer = function(tf.matmul(input_layer, weights) + biases)
return new_layer


def initialize_weights(in_units_size, out_units_size):
return tf.Variable(
tf.truncated_normal([in_units_size, out_units_size], stddev=1.0 / math.sqrt(float(in_units_size))),
name='weights'
)


def initialize_biases(out_units_size):
return tf.Variable(tf.zeros([out_units_size]), name='biases')


0 comments on commit fe4dbba

Please sign in to comment.