Skip to content

Commit

Permalink
removed requirement to define placeholders externally
Browse files Browse the repository at this point in the history
  • Loading branch information
lene committed Feb 27, 2016
1 parent 11fb186 commit 71b5546
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 62 deletions.
30 changes: 3 additions & 27 deletions nn_wtf/mnist_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ def __init__(
self.train_dir = ensure_is_dir(train_dir)

self.step = 0
images_placeholder, labels_placeholder = placeholder_inputs(self.batch_size)

super().__init__(images_placeholder.get_shape()[1], self.hidden, NUM_CLASSES)
super().__init__(IMAGE_PIXELS, self.hidden, NUM_CLASSES)

self.build_neural_network(images_placeholder)
self.build_neural_network()

self.build_train_ops(labels_placeholder, self.learning_rate)
self.build_train_ops(self.learning_rate)

self._setup_summaries()

Expand Down Expand Up @@ -102,26 +101,3 @@ def ensure_is_dir(train_dir_string):
train_dir_string += '/'
return train_dir_string


def placeholder_inputs(batch_size):
"""Generate placeholder variables to represent the input tensors.
These placeholders are used as inputs by the rest of the model building
code and will be fed from the downloaded data in the .run() loop, below.
Args:
batch_size: The batch size will be baked into both placeholders.
Returns:
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.
# images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS))
images_placeholder = tf.placeholder(tf.float32, shape=(None, IMAGE_PIXELS), name='images')
# labels_placeholder = tf.placeholder(tf.int32, shape=batch_size)
labels_placeholder = tf.placeholder(tf.int32, shape=(None,), name='labels')
return images_placeholder, labels_placeholder

25 changes: 8 additions & 17 deletions nn_wtf/neural_network_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ def __init__(self, input_size, layer_sizes, output_size):
self.num_hidden_layers = len(self.layer_sizes)-1
self.layers = []
self.predictor = None
self.input_placeholder = None
self.labels_placeholder = None
self.input_placeholder = tf.placeholder(tf.float32, shape=(None, self.input_size), name='input')
self.labels_placeholder = tf.placeholder(tf.int32, shape=(None,), name='labels')


def _set_layer_sizes(self, layer_sizes):
layer_sizes = tuple(filter(None, layer_sizes))
if layer_sizes[-1] < self.output_size:
raise ValueError('Last layer size must be greater or equal output size')
return (self.input_size,) + layer_sizes

def build_neural_network(self, input_placeholder):
def build_neural_network(self):
"""Builds a neural network with the given layers and output size.
Args:
Expand All @@ -41,16 +42,9 @@ def build_neural_network(self, input_placeholder):
logits: Output tensor with the computed logits.
"""

assert isinstance(input_placeholder, tf.Tensor), 'input placeholder not a tf.Tensor'
assert self.input_size == int(input_placeholder.get_shape()[1]), \
'declared input size {} not matching input placehoder shape {}'.format(
self.input_size, int(input_placeholder.get_shape()[1])
)
assert self.layers == [], 'build_neural_network() has been called before'

self.input_placeholder = input_placeholder

self.layers.append(input_placeholder)
self.layers.append(self.input_placeholder)
for i in range(1, self.num_hidden_layers+1):
self.layers.append(
self._add_layer(
Expand All @@ -64,21 +58,18 @@ def build_neural_network(self, input_placeholder):

return logits

def build_train_ops(self, labels_placeholder, learning_rate):
def build_train_ops(self, learning_rate):

assert isinstance(labels_placeholder, tf.Tensor), 'labels placeholder not a tf.Tensor'
assert len(self.layers) > 0, 'build_neural_network() needs to be called first'

self.labels_placeholder = labels_placeholder

# Add to the Graph the Ops for loss calculation.
self.loss_op = self.loss(self.layers[-1], labels_placeholder)
self.loss_op = self.loss(self.layers[-1], self.labels_placeholder)

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

# Add the Op to compare the logits to the labels during evaluation.
self.eval_correct_op = self.evaluation(self.layers[-1], labels_placeholder)
self.eval_correct_op = self.evaluation(self.layers[-1], self.labels_placeholder)

def set_session(self, session=None):
if session is None:
Expand Down
22 changes: 5 additions & 17 deletions nn_wtf/tests/neural_network_graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,19 @@ def test_init_fails_if_last_layer_smaller_than_output_size(self):

def test_build_neural_network_runs(self):
graph = self._create_minimal_graph()
input_placeholder = create_minimal_input_placeholder()

graph.build_neural_network(input_placeholder)
graph.build_neural_network()

def test_build_neural_network_runs_only_once(self):
graph = self._create_minimal_graph()
input_placeholder = create_minimal_input_placeholder()

graph.build_neural_network(input_placeholder)
graph.build_neural_network()

with self.assertRaises(AssertionError):
graph.build_neural_network(input_placeholder)

def test_build_neural_network_fails_on_bad_input_size(self):
graph = self._create_minimal_graph()
input_placeholder = tf.placeholder(tf.float32, shape=(MINIMAL_BATCH_SIZE, MINIMAL_INPUT_SIZE+1))

with self.assertRaises(AssertionError):
graph.build_neural_network(input_placeholder)
graph.build_neural_network()

def test_build_neural_network_output(self):
graph = self._create_minimal_graph()

output = graph.build_neural_network(create_minimal_input_placeholder())
output = graph.build_neural_network()

self.assertIsInstance(output, tf.Tensor)
self.assertEqual(2, output.get_shape().ndims)
Expand All @@ -68,10 +57,9 @@ def test_build_neural_network_output_with_middle_layer_none(self):

def _check_num_hidden_layers_for_input_is(self, definition, expected_size):
graph = NeuralNetworkGraph(MINIMAL_INPUT_SIZE, definition, MINIMAL_OUTPUT_SIZE)
graph.build_neural_network(create_minimal_input_placeholder())
graph.build_neural_network()
self.assertEqual(expected_size, graph.num_hidden_layers)

def _create_minimal_graph(self):
return NeuralNetworkGraph(MINIMAL_INPUT_SIZE, MINIMAL_LAYER_GEOMETRY, MINIMAL_OUTPUT_SIZE)


2 changes: 1 addition & 1 deletion nn_wtf/tests/predictor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PredictorTest(unittest.TestCase):

def test_simple_case(self):
graph = NeuralNetworkGraph(MINIMAL_INPUT_SIZE, MINIMAL_LAYER_GEOMETRY, MINIMAL_OUTPUT_SIZE)
output = graph.build_neural_network(create_minimal_input_placeholder())
output = graph.build_neural_network()
train_data = [[0, 0], [1, 1]]
train_labels = [0, 1]

0 comments on commit 71b5546

Please sign in to comment.