-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmnist_train.py
More file actions
59 lines (46 loc) · 1.45 KB
/
Copy pathmnist_train.py
File metadata and controls
59 lines (46 loc) · 1.45 KB
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
import tensorflow as tf
from datasets import mnist
from model import lenet, load_batch
slim = tf.contrib.slim
flags = tf.app.flags
flags.DEFINE_string('data_dir', '/tmp/mnist',
'Directory with the mnist data.')
flags.DEFINE_integer('batch_size', 5, 'Batch size.')
flags.DEFINE_integer('num_batches', None,
'Num of batches to train (epochs).')
flags.DEFINE_string('log_dir', './log/train',
'Directory with the log data.')
FLAGS = flags.FLAGS
def main(args):
# load the dataset
dataset = mnist.get_split('train', FLAGS.data_dir)
# load batch of dataset
images, labels = load_batch(
dataset,
FLAGS.batch_size,
is_training=True)
# run the image through the model
predictions = lenet(images)
# get the cross-entropy loss
one_hot_labels = slim.one_hot_encoding(
labels,
dataset.num_classes)
slim.losses.softmax_cross_entropy(
predictions,
one_hot_labels)
total_loss = slim.losses.get_total_loss()
tf.summary.scalar('loss', total_loss)
# use RMSProp to optimize
optimizer = tf.train.RMSPropOptimizer(0.001, 0.9)
# create train op
train_op = slim.learning.create_train_op(
total_loss,
optimizer,
summarize_gradients=True)
# run training
slim.learning.train(
train_op,
FLAGS.log_dir,
save_summaries_secs=20)
if __name__ == '__main__':
tf.app.run()