MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / train_and_export

Function train_and_export

tensorflow/examples/tf2_showcase/mnist.py:184–234  ·  view source on GitHub ↗

Run MNIST training and eval loop in eager mode. Args: flags_obj: An object containing parsed flag values.

(flags_obj)

Source from the content-addressed store, hash-verified

182
183
184def train_and_export(flags_obj):
185 """Run MNIST training and eval loop in eager mode.
186
187 Args:
188 flags_obj: An object containing parsed flag values.
189 """
190 # Load the datasets
191 train_ds, test_ds = mnist_datasets()
192 train_ds = train_ds.shuffle(60000).batch(flags_obj.batch_size)
193 test_ds = test_ds.batch(flags_obj.batch_size)
194
195 # Create the model and optimizer
196 model = create_model()
197 optimizer = tf.train.MomentumOptimizer(
198 flags_obj.learning_rate, flags_obj.momentum)
199
200 # See summaries with `tensorboard --logdir=<model_dir>`
201 train_dir = os.path.join(flags_obj.model_dir, 'summaries', 'train')
202 test_dir = os.path.join(flags_obj.model_dir, 'summaries', 'eval')
203 summary_writer = tf.contrib.summary.create_file_writer(
204 train_dir, flush_millis=10000)
205 test_summary_writer = tf.contrib.summary.create_file_writer(
206 test_dir, flush_millis=10000, name='test')
207
208 # Create and restore checkpoint (if one exists on the path)
209 checkpoint_dir = os.path.join(flags_obj.model_dir, 'checkpoints')
210 checkpoint_prefix = os.path.join(checkpoint_dir, 'ckpt')
211 step_counter = tf.train.get_or_create_global_step()
212 checkpoint = tf.train.Checkpoint(
213 model=model, optimizer=optimizer, step_counter=step_counter)
214 # Restore variables on creation if a checkpoint exists.
215 checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
216
217 # Train and evaluate for a set number of epochs.
218 for _ in range(flags_obj.train_epochs):
219 start = time.time()
220 with summary_writer.as_default():
221 train(model, optimizer, train_ds, step_counter,
222 flags_obj.log_interval, num_steps=1)
223 end = time.time()
224 print('\nTrain time for epoch #%d (%d total steps): %f' %
225 (checkpoint.save_counter.numpy() + 1,
226 step_counter.numpy(),
227 end - start))
228 with test_summary_writer.as_default():
229 test(model, test_ds)
230 checkpoint.save(checkpoint_prefix)
231
232 # TODO(brianklee): Enable this functionality after @allenl implements this.
233 # export_path = os.path.join(flags_obj.model_dir, 'export')
234 # tf.saved_model.save(export_path, model)
235
236
237def import_and_eval(flags_obj):

Callers 1

mainFunction · 0.85

Calls 14

restoreMethod · 0.95
saveMethod · 0.95
latest_checkpointMethod · 0.80
timeMethod · 0.80
mnist_datasetsFunction · 0.70
create_modelFunction · 0.70
trainFunction · 0.70
testFunction · 0.70
rangeFunction · 0.50
batchMethod · 0.45
shuffleMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected