-
Notifications
You must be signed in to change notification settings - Fork 0
/
digitRecog_with_NN.py
65 lines (49 loc) · 2.25 KB
/
digitRecog_with_NN.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
import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
with tf.name_scope('Input'):
x = tf.placeholder(tf.float32, [None, 784], name='x_input')
y_real = tf.placeholder(tf.float32, [None, 10], name='y_input')
with tf.name_scope('Weights'):
Weights = {
'h1': tf.Variable(tf.random_normal([784, 256]), name='W_1'),
'h2': tf.Variable(tf.random_normal([256, 256]), name='W_2'),
'out': tf.Variable(tf.random_normal([256, 10]), name='W_3'),
}
with tf.name_scope('biases'):
biases = {
'h1': tf.Variable(tf.random_normal([256]), name='b_1'),
'h2': tf.Variable(tf.random_normal([256]), name='b_2'),
'out': tf.Variable(tf.random_normal([10]), name='b_3'),
}
with tf.name_scope('Layer1'):
layer1_raw = tf.add(tf.matmul(x, Weights['h1']), biases['h1'])
layer1 = tf.nn.relu(layer1_raw, name='output_layer1')
tf.histogram_summary('W_1', Weights['h1'])
tf.histogram_summary('b_1', biases['h1'])
with tf.name_scope('Layer2'):
layer2_raw = tf.add(tf.matmul(layer1, Weights['h2']), biases['h2'])
layer2 = tf.nn.relu(layer2_raw, name='output_layer1')
tf.histogram_summary('W_2', Weights['h2'])
tf.histogram_summary('b_2', biases['h2'])
with tf.name_scope('Layer3'):
output = tf.add(tf.matmul(layer2, Weights['out']), biases['out'], name='output_final')
tf.histogram_summary('W_3', Weights['out'])
tf.histogram_summary('b_3', biases['out'])
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, y_real))
tf.scalar_summary('loss', loss)
train = tf.train.AdamOptimizer(0.01).minimize(loss)
init = tf.initialize_all_variables()
with tf.Session() as sess:
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter('test/', sess.graph)
sess.run(init)
for i in range(1, 1000):
x_batch, y_batch = mnist.train.next_batch(100)
sess.run(train, feed_dict={x: x_batch, y_real: y_batch})
result = sess.run(merged, feed_dict={x: x_batch, y_real: y_batch})
# writer.add_summary(result, i)
acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y_real, 1), tf.argmax(output, 1)), tf.float32))
print 'step: ', i, ' accuracy: ', acc.eval({x: mnist.test.images, y_real: mnist.test.labels})
# operation time ~ 3 hours, accuracy ~94.5%