Skip to content

Commit db0b5da

Browse files
author
Vijay Vasudevan
committed
TensorFlow: Initial steps towards python3 support, some documentation
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
1 parent 468ecff commit db0b5da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1175
-1127
lines changed

tensorflow/g3doc/get_started/basic_usage.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ shows a variable serving as a simple counter. See
194194

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

199-
# Create an Op to add one to `var`.
199+
# Create an Op to add one to `state`.
200200

201201
one = tf.constant(1)
202202
new_value = tf.add(state, one)
@@ -209,14 +209,14 @@ init_op = tf.initialize_all_variables()
209209

210210
# Launch the graph and run the ops.
211211
with tf.Session() as sess:
212-
# Run the 'init' op
213-
sess.run(init_op)
214-
# Print the initial value of 'var'
215-
print sess.run(var)
216-
# Run the op that updates 'var' and print 'var'.
217-
for _ in range(3):
218-
sess.run(update)
219-
print sess.run(var)
212+
# Run the 'init' op
213+
sess.run(init_op)
214+
# Print the initial value of 'state'
215+
print sess.run(state)
216+
# Run the op that updates 'state' and print 'state'.
217+
for _ in range(3):
218+
sess.run(update)
219+
print sess.run(state)
220220

221221
# output:
222222

tensorflow/g3doc/get_started/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Let's get you up and running with TensorFlow!
44

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

tensorflow/g3doc/how_tos/adding_an_op/fact_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test that user ops can be used as expected."""
2+
from __future__ import print_function
23

34
import tensorflow.python.platform
45

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

1011
def test(self):
1112
with self.test_session():
12-
print tf.user_ops.my_fact().eval()
13+
print(tf.user_ops.my_fact().eval())
1314

1415

1516
if __name__ == '__main__':
Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Converts MNIST data to TFRecords file format with Example protos."""
2+
from __future__ import print_function
23

34
import os
45
import tensorflow.python.platform
@@ -32,56 +33,56 @@ def _bytes_feature(value):
3233

3334

3435
def convert_to(images, labels, name):
35-
num_examples = labels.shape[0]
36-
if images.shape[0] != num_examples:
37-
raise ValueError("Images size %d does not match label size %d." %
38-
(dat.shape[0], num_examples))
39-
rows = images.shape[1]
40-
cols = images.shape[2]
41-
depth = images.shape[3]
42-
43-
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
44-
print 'Writing', filename
45-
writer = tf.python_io.TFRecordWriter(filename)
46-
for index in range(num_examples):
47-
image_raw = images[index].tostring()
48-
example = tf.train.Example(features=tf.train.Features(feature={
49-
'height':_int64_feature(rows),
50-
'width':_int64_feature(cols),
51-
'depth':_int64_feature(depth),
52-
'label':_int64_feature(int(labels[index])),
53-
'image_raw':_bytes_feature(image_raw)}))
54-
writer.write(example.SerializeToString())
36+
num_examples = labels.shape[0]
37+
if images.shape[0] != num_examples:
38+
raise ValueError("Images size %d does not match label size %d." %
39+
(dat.shape[0], num_examples))
40+
rows = images.shape[1]
41+
cols = images.shape[2]
42+
depth = images.shape[3]
43+
44+
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
45+
print('Writing', filename)
46+
writer = tf.python_io.TFRecordWriter(filename)
47+
for index in range(num_examples):
48+
image_raw = images[index].tostring()
49+
example = tf.train.Example(features=tf.train.Features(feature={
50+
'height': _int64_feature(rows),
51+
'width': _int64_feature(cols),
52+
'depth': _int64_feature(depth),
53+
'label': _int64_feature(int(labels[index])),
54+
'image_raw': _bytes_feature(image_raw)}))
55+
writer.write(example.SerializeToString())
5556

5657

5758
def main(argv):
58-
# Get the data.
59-
train_images_filename = input_data.maybe_download(
60-
TRAIN_IMAGES, FLAGS.directory)
61-
train_labels_filename = input_data.maybe_download(
62-
TRAIN_LABELS, FLAGS.directory)
63-
test_images_filename = input_data.maybe_download(
64-
TEST_IMAGES, FLAGS.directory)
65-
test_labels_filename = input_data.maybe_download(
66-
TEST_LABELS, FLAGS.directory)
67-
68-
# Extract it into numpy arrays.
69-
train_images = input_data.extract_images(train_images_filename)
70-
train_labels = input_data.extract_labels(train_labels_filename)
71-
test_images = input_data.extract_images(test_images_filename)
72-
test_labels = input_data.extract_labels(test_labels_filename)
73-
74-
# Generate a validation set.
75-
validation_images = train_images[:FLAGS.validation_size, :, :, :]
76-
validation_labels = train_labels[:FLAGS.validation_size]
77-
train_images = train_images[FLAGS.validation_size:, :, :, :]
78-
train_labels = train_labels[FLAGS.validation_size:]
79-
80-
# Convert to Examples and write the result to TFRecords.
81-
convert_to(train_images, train_labels, 'train')
82-
convert_to(validation_images, validation_labels, 'validation')
83-
convert_to(test_images, test_labels, 'test')
59+
# Get the data.
60+
train_images_filename = input_data.maybe_download(
61+
TRAIN_IMAGES, FLAGS.directory)
62+
train_labels_filename = input_data.maybe_download(
63+
TRAIN_LABELS, FLAGS.directory)
64+
test_images_filename = input_data.maybe_download(
65+
TEST_IMAGES, FLAGS.directory)
66+
test_labels_filename = input_data.maybe_download(
67+
TEST_LABELS, FLAGS.directory)
68+
69+
# Extract it into numpy arrays.
70+
train_images = input_data.extract_images(train_images_filename)
71+
train_labels = input_data.extract_labels(train_labels_filename)
72+
test_images = input_data.extract_images(test_images_filename)
73+
test_labels = input_data.extract_labels(test_labels_filename)
74+
75+
# Generate a validation set.
76+
validation_images = train_images[:FLAGS.validation_size, :, :, :]
77+
validation_labels = train_labels[:FLAGS.validation_size]
78+
train_images = train_images[FLAGS.validation_size:, :, :, :]
79+
train_labels = train_labels[FLAGS.validation_size:]
80+
81+
# Convert to Examples and write the result to TFRecords.
82+
convert_to(train_images, train_labels, 'train')
83+
convert_to(validation_images, validation_labels, 'validation')
84+
convert_to(test_images, test_labels, 'test')
8485

8586

8687
if __name__ == '__main__':
87-
tf.app.run()
88+
tf.app.run()

tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py

Lines changed: 94 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
bazel run -c opt \
66
<...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded
77
"""
8+
from __future__ import print_function
89
import os.path
910
import time
1011

@@ -31,104 +32,102 @@
3132

3233

3334
def run_training():
34-
"""Train MNIST for a number of epochs."""
35-
# Get the sets of images and labels for training, validation, and
36-
# test on MNIST.
37-
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)
38-
39-
# Tell TensorFlow that the model will be built into the default Graph.
40-
with tf.Graph().as_default():
41-
with tf.name_scope('input'):
42-
# Input data
43-
input_images = tf.constant(data_sets.train.images)
44-
input_labels = tf.constant(data_sets.train.labels)
45-
46-
image, label = tf.train.slice_input_producer(
47-
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
48-
label = tf.cast(label, tf.int32)
49-
images, labels = tf.train.batch(
50-
[image, label], batch_size=FLAGS.batch_size)
51-
52-
# Build a Graph that computes predictions from the inference model.
53-
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)
54-
55-
# Add to the Graph the Ops for loss calculation.
56-
loss = mnist.loss(logits, labels)
57-
58-
# Add to the Graph the Ops that calculate and apply gradients.
59-
train_op = mnist.training(loss, FLAGS.learning_rate)
60-
61-
# Add the Op to compare the logits to the labels during evaluation.
62-
eval_correct = mnist.evaluation(logits, labels)
63-
64-
# Build the summary operation based on the TF collection of Summaries.
65-
summary_op = tf.merge_all_summaries()
66-
67-
# Create a saver for writing training checkpoints.
68-
saver = tf.train.Saver()
69-
70-
# Create the op for initializing variables.
71-
init_op = tf.initialize_all_variables()
72-
73-
# Create a session for running Ops on the Graph.
74-
sess = tf.Session()
75-
76-
# Run the Op to initialize the variables.
77-
sess.run(init_op)
78-
79-
# Instantiate a SummaryWriter to output summaries and the Graph.
80-
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
81-
graph_def=sess.graph_def)
82-
83-
# Start input enqueue threads.
84-
coord = tf.train.Coordinator()
85-
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
86-
87-
# And then after everything is built, start the training loop.
88-
try:
89-
step = 0
90-
while not coord.should_stop():
91-
start_time = time.time()
92-
93-
# Run one step of the model.
94-
_, loss_value = sess.run([train_op, loss])
95-
96-
duration = time.time() - start_time
97-
98-
# Write the summaries and print an overview fairly often.
99-
if step % 100 == 0:
100-
# Print status to stdout.
101-
print 'Step %d: loss = %.2f (%.3f sec)' % (step,
102-
loss_value,
103-
duration)
104-
# Update the events file.
105-
summary_str = sess.run(summary_op)
106-
summary_writer.add_summary(summary_str, step)
107-
step += 1
108-
109-
# Save a checkpoint periodically.
110-
if (step + 1) % 1000 == 0:
111-
print 'Saving'
112-
saver.save(sess, FLAGS.train_dir, global_step=step)
113-
114-
step += 1
115-
except tf.errors.OutOfRangeError:
116-
print 'Saving'
117-
saver.save(sess, FLAGS.train_dir, global_step=step)
118-
print 'Done training for %d epochs, %d steps.' % (
119-
FLAGS.num_epochs, step)
120-
finally:
121-
# When done, ask the threads to stop.
122-
coord.request_stop()
123-
124-
# Wait for threads to finish.
125-
coord.join(threads)
126-
sess.close()
35+
"""Train MNIST for a number of epochs."""
36+
# Get the sets of images and labels for training, validation, and
37+
# test on MNIST.
38+
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)
39+
40+
# Tell TensorFlow that the model will be built into the default Graph.
41+
with tf.Graph().as_default():
42+
with tf.name_scope('input'):
43+
# Input data
44+
input_images = tf.constant(data_sets.train.images)
45+
input_labels = tf.constant(data_sets.train.labels)
46+
47+
image, label = tf.train.slice_input_producer(
48+
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
49+
label = tf.cast(label, tf.int32)
50+
images, labels = tf.train.batch(
51+
[image, label], batch_size=FLAGS.batch_size)
52+
53+
# Build a Graph that computes predictions from the inference model.
54+
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)
55+
56+
# Add to the Graph the Ops for loss calculation.
57+
loss = mnist.loss(logits, labels)
58+
59+
# Add to the Graph the Ops that calculate and apply gradients.
60+
train_op = mnist.training(loss, FLAGS.learning_rate)
61+
62+
# Add the Op to compare the logits to the labels during evaluation.
63+
eval_correct = mnist.evaluation(logits, labels)
64+
65+
# Build the summary operation based on the TF collection of Summaries.
66+
summary_op = tf.merge_all_summaries()
67+
68+
# Create a saver for writing training checkpoints.
69+
saver = tf.train.Saver()
70+
71+
# Create the op for initializing variables.
72+
init_op = tf.initialize_all_variables()
73+
74+
# Create a session for running Ops on the Graph.
75+
sess = tf.Session()
76+
77+
# Run the Op to initialize the variables.
78+
sess.run(init_op)
79+
80+
# Instantiate a SummaryWriter to output summaries and the Graph.
81+
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
82+
graph_def=sess.graph_def)
83+
84+
# Start input enqueue threads.
85+
coord = tf.train.Coordinator()
86+
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
87+
88+
# And then after everything is built, start the training loop.
89+
try:
90+
step = 0
91+
while not coord.should_stop():
92+
start_time = time.time()
93+
94+
# Run one step of the model.
95+
_, loss_value = sess.run([train_op, loss])
96+
97+
duration = time.time() - start_time
98+
99+
# Write the summaries and print an overview fairly often.
100+
if step % 100 == 0:
101+
# Print status to stdout.
102+
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,
103+
duration))
104+
# Update the events file.
105+
summary_str = sess.run(summary_op)
106+
summary_writer.add_summary(summary_str, step)
107+
step += 1
108+
109+
# Save a checkpoint periodically.
110+
if (step + 1) % 1000 == 0:
111+
print('Saving')
112+
saver.save(sess, FLAGS.train_dir, global_step=step)
113+
114+
step += 1
115+
except tf.errors.OutOfRangeError:
116+
print('Saving')
117+
saver.save(sess, FLAGS.train_dir, global_step=step)
118+
print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
119+
finally:
120+
# When done, ask the threads to stop.
121+
coord.request_stop()
122+
123+
# Wait for threads to finish.
124+
coord.join(threads)
125+
sess.close()
127126

128127

129128
def main(_):
130-
run_training()
129+
run_training()
131130

132131

133132
if __name__ == '__main__':
134-
tf.app.run()
133+
tf.app.run()

0 commit comments

Comments
 (0)