-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_into_tensorboard.py
117 lines (90 loc) · 3.41 KB
/
mnist_into_tensorboard.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# encoding: utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# MNIST 손글씨 데이터 읽어들이기
mnist = input_data.read_data_sets("mnist/", one_hot=True)
pixels = 28 * 28
nums = 10
# placeholder 정의
x = tf.placeholder(tf.float32, shape=(None, pixels), name="x") # image data
y_ = tf.placeholder(tf.float32, shape=(None, nums), name="y_") # answer label
# initiate function for weight and bias
def weight_variable(name, shape):
W_init = tf.truncated_normal(shape, stddev=0.1)
W = tf.Variable(W_init, name="W_" + name)
return W
def bias_variable(name, size):
b_init = tf.constant(0.1, shape=[size])
b = tf.Variable(b_init, name="b_" + name)
return b
# function to create convolution layer
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# function to create maximum pooling layer
def max_pool(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
# conv layer 1
with tf.name_scope('conv1') as scope:
W_conv1 = weight_variable('conv1', [5, 5, 1, 32])
b_conv1 = bias_variable('conv1', 32)
x_image = tf.reshape(x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
# pooling layer 1
with tf.name_scope('pool1') as scope:
h_pool1 = max_pool(h_conv1)
# conv layer 2
with tf.name_scope('conv2') as scope:
W_conv2 = weight_variable('conv2', [5, 5, 32, 64])
b_conv2 = bias_variable('conv2', 64)
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# pooling layer 2
with tf.name_scope('pool2') as scope:
h_pool2 = max_pool(h_conv2)
# fully connected layer
with tf.name_scope('fully_connected') as scope:
n = 7 * 7 * 64
W_fc = weight_variable('fc', [n, 1024])
b_fc = bias_variable('fc', 1024)
h_pool2_flat = tf.reshape(h_pool2, [-1, n])
h_fc = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc) + b_fc)
# keep from dropout
with tf.name_scope('dropout') as scope:
keep_prob = tf.placeholder(tf.float32)
h_fc_drop = tf.nn.dropout(h_fc, keep_prob)
# output layer
with tf.name_scope('readout') as scope:
W_fc2 = weight_variable('fc2', [1024, 10])
b_fc2 = bias_variable('fc2', 10)
y_conv = tf.nn.softmax(tf.matmul(h_fc_drop, W_fc2) + b_fc2)
# training model
with tf.name_scope('loss') as scope:
cross_entoropy = -tf.reduce_sum(y_ * tf.log(y_conv))
with tf.name_scope('training') as scope:
optimizer = tf.train.AdamOptimizer(1e-4)
train_step = optimizer.minimize(cross_entoropy)
# testing model
with tf.name_scope('predict') as scope:
predict_step = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy_step = tf.reduce_mean(tf.cast(predict_step, tf.float32))
# set feed_dict
def set_feed(images, labels, prob):
return {x: images, y_: labels, keep_prob: prob}
# start session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# prepare TensorBoard
tw = tf.summary.FileWriter('log_dir', graph=sess.graph)
# create feed for test
test_fd = set_feed(mnist.test.images, mnist.test.labels, 1)
# start training
for step in range(10000):
batch = mnist.train.next_batch(50)
fd = set_feed(batch[0], batch[1], 0.5)
_, loss = sess.run([train_step, cross_entoropy], feed_dict=fd)
if step % 100 == 0:
acc = sess.run(accuracy_step, feed_dict=test_fd)
print('step=', step, 'loss=', loss, 'acc=', acc)
# print final result
acc = sess.run(accuracy_step, feed_dict=test_fd)
print('accuracy=', acc)