-
Notifications
You must be signed in to change notification settings - Fork 4
/
code-09-NameScopes.py
74 lines (54 loc) · 2.46 KB
/
code-09-NameScopes.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
67
68
69
70
71
72
73
74
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
LOGDIR = './tensorflow_logs/mnist_deep'
def weight_variable(shape):
"""Generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial, name='weight')
def bias_variable(shape):
"""Generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial, name='bias')
def main():
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Placeholder that will be fed image data.
x = tf.placeholder(tf.float32, [None, 784], name='x')
# Placeholder that will be fed the correct labels.
y_ = tf.placeholder(tf.float32, [None, 10], name='labels')
# Define weight and bias.
W = weight_variable([784, 10])
b = bias_variable([10])
# Here we define our model which utilizes the softmax regression.
with tf.name_scope('softmax'):
y = tf.nn.softmax(tf.matmul(x, W) + b, name='y')
# Define our loss.
with tf.name_scope('loss'):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]), name='cross_entropy')
# Define our optimizer.
with tf.name_scope('optimizer'):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy, name='train_step')
# Define accuracy.
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
correct_prediction = tf.cast(correct_prediction, tf.float32, name='correct_prediction')
accuracy = tf.reduce_mean(correct_prediction, name='accuracy')
# Launch session.
sess = tf.InteractiveSession()
# Initialize variables.
tf.global_variables_initializer().run()
# Create summary writer
writer = tf.summary.FileWriter(LOGDIR, sess.graph)
# Do the training.
for i in range(1100):
batch = mnist.train.next_batch(100)
if i % 100 == 0:
train_accuracy = sess.run(accuracy, feed_dict={x:batch[0], y_: batch[1]})
print("Step %d, Training Accuracy %g" % (i, float(train_accuracy)))
sess.run(train_step, feed_dict={x: batch[0], y_: batch[1]})
# See how model did.
print("Test Accuracy %g" % sess.run(accuracy, feed_dict={x: mnist.test.images,
y_: mnist.test.labels}))
# Close summary writer
writer.close()
if __name__ == '__main__':
main()