forked from wookayin/tensorflow-talk-debugging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-mnist-summary.py
66 lines (55 loc) · 2.53 KB
/
12-mnist-summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import tensorflow as tf
import tensorflow.contrib.layers as layers
from datetime import datetime
# MNIST input data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
def multilayer_perceptron(x):
fc1 = layers.fully_connected(x, 256, activation_fn=tf.nn.relu, scope='fc1')
tf.histogram_summary('fc1', fc1)
tf.histogram_summary('fc1/sparsity', tf.nn.zero_fraction(fc1))
fc2 = layers.fully_connected(fc1, 256, activation_fn=tf.nn.relu, scope='fc2')
tf.histogram_summary('fc2', fc2)
tf.histogram_summary('fc2/sparsity', tf.nn.zero_fraction(fc2))
out = layers.fully_connected(fc2, 10, activation_fn=None, scope='out')
return out
# build model, loss, and train op
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
pred = multilayer_perceptron(x)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
tf.scalar_summary('loss', loss)
global_step = tf.Variable(0, dtype=tf.int32, trainable=False) # (*)
train_op = tf.train.AdamOptimizer(learning_rate=0.001)\
.minimize(loss, global_step=global_step) # (*)
# histogram summary for all trainable variables (slow?)
for v in tf.trainable_variables():
tf.histogram_summary(v.name, v)
def train(session):
batch_size = 200
session.run(tf.initialize_all_variables())
merged_summary_op = tf.merge_all_summaries() # (*)
summary_writer = tf.train.SummaryWriter('/tmp/mnist', session.graph)
# Training cycle
for epoch in range(10):
epoch_loss = 0.0
batch_steps = mnist.train.num_examples / batch_size
for step in range(batch_steps):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_, c, summary = session.run(
[train_op, loss, merged_summary_op],
feed_dict={x: batch_x, y: batch_y})
summary_writer.add_summary(summary, global_step.eval(session=session))
epoch_loss += c / batch_steps
print "[%s] Epoch %02d, Loss = %.6f" % (datetime.now(), epoch, epoch_loss)
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})
def main():
with tf.Session(config=tf.ConfigProto(
gpu_options=tf.GPUOptions(allow_growth=True),
device_count={'GPU': 1})) as session:
train(session)
if __name__ == '__main__':
main()