Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96145
License: OTHER
1
""" Using convolutional net on MNIST dataset of handwritten digits
2
MNIST dataset: http://yann.lecun.com/exdb/mnist/
3
CS 20: "TensorFlow for Deep Learning Research"
4
cs20.stanford.edu
5
Chip Huyen ([email protected])
6
Lecture 07
7
"""
8
import os
9
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
10
import time
11
12
import tensorflow as tf
13
14
import utils
15
16
def conv_relu(inputs, filters, k_size, stride, padding, scope_name):
17
'''
18
A method that does convolution + relu on inputs
19
'''
20
#############################
21
########## TO DO ############
22
#############################
23
return None
24
25
def maxpool(inputs, ksize, stride, padding='VALID', scope_name='pool'):
26
'''A method that does max pooling on inputs'''
27
#############################
28
########## TO DO ############
29
#############################
30
return None
31
32
def fully_connected(inputs, out_dim, scope_name='fc'):
33
'''
34
A fully connected linear layer on inputs
35
'''
36
#############################
37
########## TO DO ############
38
#############################
39
return None
40
41
class ConvNet(object):
42
def __init__(self):
43
self.lr = 0.001
44
self.batch_size = 128
45
self.keep_prob = tf.constant(0.75)
46
self.gstep = tf.Variable(0, dtype=tf.int32,
47
trainable=False, name='global_step')
48
self.n_classes = 10
49
self.skip_step = 20
50
self.n_test = 10000
51
52
def get_data(self):
53
with tf.name_scope('data'):
54
train_data, test_data = utils.get_mnist_dataset(self.batch_size)
55
iterator = tf.data.Iterator.from_structure(train_data.output_types,
56
train_data.output_shapes)
57
img, self.label = iterator.get_next()
58
self.img = tf.reshape(img, shape=[-1, 28, 28, 1])
59
# reshape the image to make it work with tf.nn.conv2d
60
61
self.train_init = iterator.make_initializer(train_data) # initializer for train_data
62
self.test_init = iterator.make_initializer(test_data) # initializer for train_data
63
64
def inference(self):
65
'''
66
Build the model according to the description we've shown in class
67
'''
68
#############################
69
########## TO DO ############
70
#############################
71
self.logits = None
72
73
def loss(self):
74
'''
75
define loss function
76
use softmax cross entropy with logits as the loss function
77
tf.nn.softmax_cross_entropy_with_logits
78
softmax is applied internally
79
don't forget to compute mean cross all sample in a batch
80
'''
81
#############################
82
########## TO DO ############
83
#############################
84
self.loss = None
85
86
def optimize(self):
87
'''
88
Define training op
89
using Adam Gradient Descent to minimize cost
90
Don't forget to use global step
91
'''
92
#############################
93
########## TO DO ############
94
#############################
95
self.opt = None
96
97
def summary(self):
98
'''
99
Create summaries to write on TensorBoard
100
Remember to track both training loss and test accuracy
101
'''
102
#############################
103
########## TO DO ############
104
#############################
105
self.summary_op = None
106
107
def eval(self):
108
'''
109
Count the number of right predictions in a batch
110
'''
111
with tf.name_scope('predict'):
112
preds = tf.nn.softmax(self.logits)
113
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(self.label, 1))
114
self.accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
115
116
def build(self):
117
'''
118
Build the computation graph
119
'''
120
self.get_data()
121
self.inference()
122
self.loss()
123
self.optimize()
124
self.eval()
125
self.summary()
126
127
def train_one_epoch(self, sess, saver, init, writer, epoch, step):
128
start_time = time.time()
129
sess.run(init)
130
total_loss = 0
131
n_batches = 0
132
try:
133
while True:
134
_, l, summaries = sess.run([self.opt, self.loss, self.summary_op])
135
writer.add_summary(summaries, global_step=step)
136
if (step + 1) % self.skip_step == 0:
137
print('Loss at step {0}: {1}'.format(step, l))
138
step += 1
139
total_loss += l
140
n_batches += 1
141
except tf.errors.OutOfRangeError:
142
pass
143
saver.save(sess, 'checkpoints/convnet_starter/mnist-convnet', step)
144
print('Average loss at epoch {0}: {1}'.format(epoch, total_loss/n_batches))
145
print('Took: {0} seconds'.format(time.time() - start_time))
146
return step
147
148
def eval_once(self, sess, init, writer, epoch, step):
149
start_time = time.time()
150
sess.run(init)
151
total_correct_preds = 0
152
try:
153
while True:
154
accuracy_batch, summaries = sess.run([self.accuracy, self.summary_op])
155
writer.add_summary(summaries, global_step=step)
156
total_correct_preds += accuracy_batch
157
except tf.errors.OutOfRangeError:
158
pass
159
160
print('Accuracy at epoch {0}: {1} '.format(epoch, total_correct_preds/self.n_test))
161
print('Took: {0} seconds'.format(time.time() - start_time))
162
163
def train(self, n_epochs):
164
'''
165
The train function alternates between training one epoch and evaluating
166
'''
167
utils.safe_mkdir('checkpoints')
168
utils.safe_mkdir('checkpoints/convnet_starter')
169
writer = tf.summary.FileWriter('./graphs/convnet_starter', tf.get_default_graph())
170
171
with tf.Session() as sess:
172
sess.run(tf.global_variables_initializer())
173
saver = tf.train.Saver()
174
ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/convnet_starter/checkpoint'))
175
if ckpt and ckpt.model_checkpoint_path:
176
saver.restore(sess, ckpt.model_checkpoint_path)
177
178
step = self.gstep.eval()
179
180
for epoch in range(n_epochs):
181
step = self.train_one_epoch(sess, saver, self.train_init, writer, epoch, step)
182
self.eval_once(sess, self.test_init, writer, epoch, step)
183
writer.close()
184
185
if __name__ == '__main__':
186
model = ConvNet()
187
model.build()
188
model.train(n_epochs=15)
189