| 601 | |
| 602 | |
| 603 | def train(sess_config, |
| 604 | input_hooks, |
| 605 | model, |
| 606 | data_init_op, |
| 607 | steps, |
| 608 | checkpoint_dir, |
| 609 | tf_config=None, |
| 610 | server=None): |
| 611 | model.is_training = True |
| 612 | hooks = [] |
| 613 | hooks.extend(input_hooks) |
| 614 | |
| 615 | scaffold = tf.train.Scaffold( |
| 616 | local_init_op=tf.group(tf.local_variables_initializer(), data_init_op), |
| 617 | saver=tf.train.Saver(max_to_keep=args.keep_checkpoint_max, sharded=True)) |
| 618 | |
| 619 | stop_hook = tf.train.StopAtStepHook(last_step=steps) |
| 620 | log_hook = tf.train.LoggingTensorHook( |
| 621 | { |
| 622 | 'steps': model.global_step, |
| 623 | 'loss': model.loss |
| 624 | }, every_n_iter=100) |
| 625 | hooks.append(stop_hook) |
| 626 | hooks.append(log_hook) |
| 627 | if args.timeline > 0: |
| 628 | hooks.append( |
| 629 | tf.train.ProfilerHook(save_steps=args.timeline, |
| 630 | output_dir=checkpoint_dir)) |
| 631 | save_steps = args.save_steps if args.save_steps or args.no_eval else steps |
| 632 | ''' |
| 633 | Incremental_Checkpoint |
| 634 | Please add `save_incremental_checkpoint_secs` in 'tf.train.MonitoredTrainingSession' |
| 635 | it's default to None, Incremental_save checkpoint time in seconds can be set |
| 636 | to use incremental checkpoint function, like `tf.train.MonitoredTrainingSession( |
| 637 | save_incremental_checkpoint_secs=args.incremental_ckpt)` |
| 638 | ''' |
| 639 | if args.incremental_ckpt and not args.tf: |
| 640 | print("Incremental_Checkpoint is not really enabled.") |
| 641 | print("Please see the comments in the code.") |
| 642 | sys.exit() |
| 643 | |
| 644 | with tf.train.MonitoredTrainingSession( |
| 645 | master=server.target if server else '', |
| 646 | is_chief=tf_config['is_chief'] if tf_config else True, |
| 647 | hooks=hooks, |
| 648 | scaffold=scaffold, |
| 649 | checkpoint_dir=checkpoint_dir, |
| 650 | save_checkpoint_steps=save_steps, |
| 651 | summary_dir=checkpoint_dir, |
| 652 | save_summaries_steps=args.save_steps, |
| 653 | config=sess_config) as sess: |
| 654 | while not sess.should_stop(): |
| 655 | sess.run([model.loss, model.train_op]) |
| 656 | print("Training completed.") |
| 657 | |
| 658 | |
| 659 | def eval(sess_config, input_hooks, model, data_init_op, steps, checkpoint_dir): |