-
Notifications
You must be signed in to change notification settings - Fork 4
/
code-07-IncreasedBatch.py
63 lines (45 loc) · 1.99 KB
/
code-07-IncreasedBatch.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
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
def weight_variable(shape):
"""Generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""Generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
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])
# Placeholder that will be fed the correct labels.
y_ = tf.placeholder(tf.float32, [None, 10])
# Define weight and bias.
W = weight_variable([784, 10])
b = bias_variable([10])
# Here we define our model which utilizes the softmax regression.
y = tf.nn.softmax(tf.matmul(x, W) + b)
# Define our loss.
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
# Define our optimizer.
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# Define accuracy.
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
# Launch session.
sess = tf.InteractiveSession()
# Initialize variables.
tf.global_variables_initializer().run()
# 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}))
if __name__ == '__main__':
main()