[go: nahoru, domu]

Skip to content

Commit

Permalink
TensorFlow: Initial steps towards python3 support, some documentation
Browse files Browse the repository at this point in the history
bug fixes -- reindents to 2 for some of the files to match our internal
requirements.

Thanks to Martin Andrews for the basic_usage.md suggested fix via
Gerrit.

Base CL: 107394029
  • Loading branch information
Vijay Vasudevan committed Nov 9, 2015
1 parent 468ecff commit db0b5da
Show file tree
Hide file tree
Showing 66 changed files with 1,175 additions and 1,127 deletions.
20 changes: 10 additions & 10 deletions tensorflow/g3doc/get_started/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ shows a variable serving as a simple counter. See

```python
# Create a Variable, that will be initialized to the scalar value 0.
var = tf.Variable(0, name="counter")
state = tf.Variable(0, name="counter")

# Create an Op to add one to `var`.
# Create an Op to add one to `state`.

one = tf.constant(1)
new_value = tf.add(state, one)
Expand All @@ -209,14 +209,14 @@ init_op = tf.initialize_all_variables()

# Launch the graph and run the ops.
with tf.Session() as sess:
# Run the 'init' op
sess.run(init_op)
# Print the initial value of 'var'
print sess.run(var)
# Run the op that updates 'var' and print 'var'.
for _ in range(3):
sess.run(update)
print sess.run(var)
# Run the 'init' op
sess.run(init_op)
# Print the initial value of 'state'
print sess.run(state)
# Run the op that updates 'state' and print 'state'.
for _ in range(3):
sess.run(update)
print sess.run(state)

# output:

Expand Down
2 changes: 1 addition & 1 deletion tensorflow/g3doc/get_started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Let's get you up and running with TensorFlow!

But before we even get started, let's give you a sneak peak at what TensorFlow
But before we even get started, let's give you a sneak peek at what TensorFlow
code looks like in the Python API, just so you have a sense of where we're
headed.

Expand Down
3 changes: 2 additions & 1 deletion tensorflow/g3doc/how_tos/adding_an_op/fact_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test that user ops can be used as expected."""
from __future__ import print_function

import tensorflow.python.platform

Expand All @@ -9,7 +10,7 @@ class FactTest(tf.test.TestCase):

def test(self):
with self.test_session():
print tf.user_ops.my_fact().eval()
print(tf.user_ops.my_fact().eval())


if __name__ == '__main__':
Expand Down
95 changes: 48 additions & 47 deletions tensorflow/g3doc/how_tos/reading_data/convert_to_records.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Converts MNIST data to TFRecords file format with Example protos."""
from __future__ import print_function

import os
import tensorflow.python.platform
Expand Down Expand Up @@ -32,56 +33,56 @@ def _bytes_feature(value):


def convert_to(images, labels, name):
num_examples = labels.shape[0]
if images.shape[0] != num_examples:
raise ValueError("Images size %d does not match label size %d." %
(dat.shape[0], num_examples))
rows = images.shape[1]
cols = images.shape[2]
depth = images.shape[3]

filename = os.path.join(FLAGS.directory, name + '.tfrecords')
print 'Writing', filename
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height':_int64_feature(rows),
'width':_int64_feature(cols),
'depth':_int64_feature(depth),
'label':_int64_feature(int(labels[index])),
'image_raw':_bytes_feature(image_raw)}))
writer.write(example.SerializeToString())
num_examples = labels.shape[0]
if images.shape[0] != num_examples:
raise ValueError("Images size %d does not match label size %d." %
(dat.shape[0], num_examples))
rows = images.shape[1]
cols = images.shape[2]
depth = images.shape[3]

filename = os.path.join(FLAGS.directory, name + '.tfrecords')
print('Writing', filename)
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'depth': _int64_feature(depth),
'label': _int64_feature(int(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())


def main(argv):
# Get the data.
train_images_filename = input_data.maybe_download(
TRAIN_IMAGES, FLAGS.directory)
train_labels_filename = input_data.maybe_download(
TRAIN_LABELS, FLAGS.directory)
test_images_filename = input_data.maybe_download(
TEST_IMAGES, FLAGS.directory)
test_labels_filename = input_data.maybe_download(
TEST_LABELS, FLAGS.directory)

# Extract it into numpy arrays.
train_images = input_data.extract_images(train_images_filename)
train_labels = input_data.extract_labels(train_labels_filename)
test_images = input_data.extract_images(test_images_filename)
test_labels = input_data.extract_labels(test_labels_filename)

# Generate a validation set.
validation_images = train_images[:FLAGS.validation_size, :, :, :]
validation_labels = train_labels[:FLAGS.validation_size]
train_images = train_images[FLAGS.validation_size:, :, :, :]
train_labels = train_labels[FLAGS.validation_size:]

# Convert to Examples and write the result to TFRecords.
convert_to(train_images, train_labels, 'train')
convert_to(validation_images, validation_labels, 'validation')
convert_to(test_images, test_labels, 'test')
# Get the data.
train_images_filename = input_data.maybe_download(
TRAIN_IMAGES, FLAGS.directory)
train_labels_filename = input_data.maybe_download(
TRAIN_LABELS, FLAGS.directory)
test_images_filename = input_data.maybe_download(
TEST_IMAGES, FLAGS.directory)
test_labels_filename = input_data.maybe_download(
TEST_LABELS, FLAGS.directory)

# Extract it into numpy arrays.
train_images = input_data.extract_images(train_images_filename)
train_labels = input_data.extract_labels(train_labels_filename)
test_images = input_data.extract_images(test_images_filename)
test_labels = input_data.extract_labels(test_labels_filename)

# Generate a validation set.
validation_images = train_images[:FLAGS.validation_size, :, :, :]
validation_labels = train_labels[:FLAGS.validation_size]
train_images = train_images[FLAGS.validation_size:, :, :, :]
train_labels = train_labels[FLAGS.validation_size:]

# Convert to Examples and write the result to TFRecords.
convert_to(train_images, train_labels, 'train')
convert_to(validation_images, validation_labels, 'validation')
convert_to(test_images, test_labels, 'test')


if __name__ == '__main__':
tf.app.run()
tf.app.run()
189 changes: 94 additions & 95 deletions tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
bazel run -c opt \
<...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded
"""
from __future__ import print_function
import os.path
import time

Expand All @@ -31,104 +32,102 @@


def run_training():
"""Train MNIST for a number of epochs."""
# Get the sets of images and labels for training, validation, and
# test on MNIST.
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)

# Tell TensorFlow that the model will be built into the default Graph.
with tf.Graph().as_default():
with tf.name_scope('input'):
# Input data
input_images = tf.constant(data_sets.train.images)
input_labels = tf.constant(data_sets.train.labels)

image, label = tf.train.slice_input_producer(
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
label = tf.cast(label, tf.int32)
images, labels = tf.train.batch(
[image, label], batch_size=FLAGS.batch_size)

# Build a Graph that computes predictions from the inference model.
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)

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

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

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

# Build the summary operation based on the TF collection of Summaries.
summary_op = tf.merge_all_summaries()

# Create a saver for writing training checkpoints.
saver = tf.train.Saver()

# Create the op for initializing variables.
init_op = tf.initialize_all_variables()

# Create a session for running Ops on the Graph.
sess = tf.Session()

# Run the Op to initialize the variables.
sess.run(init_op)

# Instantiate a SummaryWriter to output summaries and the Graph.
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
graph_def=sess.graph_def)

# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

# And then after everything is built, start the training loop.
try:
step = 0
while not coord.should_stop():
start_time = time.time()

# Run one step of the model.
_, loss_value = sess.run([train_op, loss])

duration = time.time() - start_time

# Write the summaries and print an overview fairly often.
if step % 100 == 0:
# Print status to stdout.
print 'Step %d: loss = %.2f (%.3f sec)' % (step,
loss_value,
duration)
# Update the events file.
summary_str = sess.run(summary_op)
summary_writer.add_summary(summary_str, step)
step += 1

# Save a checkpoint periodically.
if (step + 1) % 1000 == 0:
print 'Saving'
saver.save(sess, FLAGS.train_dir, global_step=step)

step += 1
except tf.errors.OutOfRangeError:
print 'Saving'
saver.save(sess, FLAGS.train_dir, global_step=step)
print 'Done training for %d epochs, %d steps.' % (
FLAGS.num_epochs, step)
finally:
# When done, ask the threads to stop.
coord.request_stop()

# Wait for threads to finish.
coord.join(threads)
sess.close()
"""Train MNIST for a number of epochs."""
# Get the sets of images and labels for training, validation, and
# test on MNIST.
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)

# Tell TensorFlow that the model will be built into the default Graph.
with tf.Graph().as_default():
with tf.name_scope('input'):
# Input data
input_images = tf.constant(data_sets.train.images)
input_labels = tf.constant(data_sets.train.labels)

image, label = tf.train.slice_input_producer(
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
label = tf.cast(label, tf.int32)
images, labels = tf.train.batch(
[image, label], batch_size=FLAGS.batch_size)

# Build a Graph that computes predictions from the inference model.
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)

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

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

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

# Build the summary operation based on the TF collection of Summaries.
summary_op = tf.merge_all_summaries()

# Create a saver for writing training checkpoints.
saver = tf.train.Saver()

# Create the op for initializing variables.
init_op = tf.initialize_all_variables()

# Create a session for running Ops on the Graph.
sess = tf.Session()

# Run the Op to initialize the variables.
sess.run(init_op)

# Instantiate a SummaryWriter to output summaries and the Graph.
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
graph_def=sess.graph_def)

# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

# And then after everything is built, start the training loop.
try:
step = 0
while not coord.should_stop():
start_time = time.time()

# Run one step of the model.
_, loss_value = sess.run([train_op, loss])

duration = time.time() - start_time

# Write the summaries and print an overview fairly often.
if step % 100 == 0:
# Print status to stdout.
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,
duration))
# Update the events file.
summary_str = sess.run(summary_op)
summary_writer.add_summary(summary_str, step)
step += 1

# Save a checkpoint periodically.
if (step + 1) % 1000 == 0:
print('Saving')
saver.save(sess, FLAGS.train_dir, global_step=step)

step += 1
except tf.errors.OutOfRangeError:
print('Saving')
saver.save(sess, FLAGS.train_dir, global_step=step)
print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
finally:
# When done, ask the threads to stop.
coord.request_stop()

# Wait for threads to finish.
coord.join(threads)
sess.close()


def main(_):
run_training()
run_training()


if __name__ == '__main__':
tf.app.run()
tf.app.run()
Loading

0 comments on commit db0b5da

Please sign in to comment.