-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
151 lines (118 loc) · 8.13 KB
/
train.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import tensorflow as tf
import numpy as np
flags = tf.app.flags
FLAGS = flags.FLAGS
def train(x_train, y_train, x_val, y_val, x_test, y_test, model, sess, log_file):
# Set random seed
seed = 123
np.random.seed(seed)
tf.set_random_seed(seed)
# batch size calculation
num_instance = x_train.shape[0]
num_batch = num_instance // FLAGS.batch_size
# run required initializations
sess.run(tf.global_variables_initializer())
####################################################################
# TODO: Define Writers #
# Define 3 separate FileWriters for train, val and test sets #
# For more convenience use below log file names as logdir argument #
# Do not forget to add graph to FileWriters #
####################################################################
train_log_file = './logs/' + log_file + '/train/'
val_log_file = './logs/' + log_file + '/val/'
test_log_file = './logs/' + log_file + '/test/'
train_writer = tf.summary.FileWriter(train_log_file,
graph=sess.graph)
val_writer = tf.summary.FileWriter(val_log_file,
graph=sess.graph)
test_writer = tf.summary.FileWriter(test_log_file,
graph=sess.graph)
####################################################################
# END OF YOUR CODE #
####################################################################
# merge all summaries defined in a single summary
merged_summary = tf.summary.merge_all()
for epoch in range(FLAGS.num_epoch):
for batch in range(num_batch):
# select batch range
batch_range = range(FLAGS.batch_size * batch, FLAGS.batch_size * (batch + 1))
train_feed_dict = dict()
#########################################################################
# TODO: Feed Train Dictionary #
# Use update method of dictionary to feed model placeholders #
# Use batch range defined above pointing to the range of current batch #
#########################################################################
train_feed_dict.update({model.placeholders['batch_images']: x_train[batch_range]})
train_feed_dict.update({model.placeholders['batch_labels']: y_train[batch_range]})
#########################################################################
# END OF YOUR CODE #
#########################################################################
# train
sess.run(model.training, feed_dict=train_feed_dict)
######################################################################################
# TODO: Feed Train Dictionary and Run Session #
# Use update method of dictionary to feed model placeholders #
# Feed all train images and labels #
# Run session on merged_summary, loss and accuracy of model #
# Use add_summary method of train_writer to add result of merged_summary evaluation #
# Note to set global step of summary based on epoch number #
######################################################################################
train_feed_dict = dict()
train_feed_dict.update({model.placeholders['batch_images']: x_train})
train_feed_dict.update({model.placeholders['batch_labels']: y_train})
train_summary, train_loss, train_acc = sess.run([merged_summary, model.loss, model.acc],
feed_dict=train_feed_dict)
train_writer.add_summary(train_summary, global_step=epoch + 1)
######################################################################################
# END OF YOUR CODE #
######################################################################################
######################################################################################
# TODO: Feed Validation Dictionary and Run Session #
# Use update method of dictionary to feed model placeholders #
# Feed all validation images and labels #
# Run session just on merged_summary and loss of model #
# Use add_summary method of val_writer to add result of merged_summary evaluation #
# Note to set global step of summary based on epoch number #
######################################################################################
val_feed_dict = dict()
val_feed_dict.update({model.placeholders['batch_images']: x_val})
val_feed_dict.update({model.placeholders['batch_labels']: y_val})
val_summary, val_loss = sess.run([merged_summary, model.loss], feed_dict=val_feed_dict)
val_writer.add_summary(val_summary, global_step=epoch + 1)
######################################################################################
# END OF YOUR CODE #
######################################################################################
######################################################################################
# TODO: Feed Test Dictionary and Run Session #
# Use update method of dictionary to feed model placeholders #
# Feed all test images and labels #
# Run session just on merged_summary #
# Use add_summary method of test_writer to add result of merged_summary evaluation #
# Note to set global step of summary based on epoch number #
######################################################################################
test_feed_dict = dict()
test_feed_dict.update({model.placeholders['batch_images']: x_test})
test_feed_dict.update({model.placeholders['batch_labels']: y_test})
test_summary = sess.run(merged_summary, feed_dict=test_feed_dict)
test_writer.add_summary(test_summary, global_step=epoch + 1)
######################################################################################
# END OF YOUR CODE #
######################################################################################
# print result of each epoch
print('Epoch {}: train loss={:.3f}, train acc={:.3f}'.format(epoch + 1, train_loss, train_acc))
print()
###############################################################
# TODO: Feed Test Dictionary and Run Session #
# Use update method of dictionary to feed model placeholders #
# Feed all test images and labels #
# Run session just on loss and accuracy of model #
###############################################################
test_feed_dict = dict()
test_feed_dict.update({model.placeholders['batch_images']: x_test})
test_feed_dict.update({model.placeholders['batch_labels']: y_test})
test_loss, test_acc = sess.run([model.loss, model.acc], feed_dict=test_feed_dict)
###############################################################
# END OF YOUR CODE #
###############################################################
print('Test: average loss={:.3f}, average accuracy={:.3f}'.format(test_loss, test_acc))
print('-------')