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