| 765 | |
| 766 | |
| 767 | def train(sess_config, |
| 768 | input_hooks, |
| 769 | model, |
| 770 | data_init_op, |
| 771 | steps, |
| 772 | checkpoint_dir, |
| 773 | tf_config=None, |
| 774 | server=None): |
| 775 | model.is_training = True |
| 776 | hooks = [] |
| 777 | hooks.extend(input_hooks) |
| 778 | |
| 779 | scaffold = tf.train.Scaffold( |
| 780 | local_init_op=tf.group(tf.local_variables_initializer(), data_init_op), |
| 781 | saver=tf.train.Saver(max_to_keep=args.keep_checkpoint_max, sharded=True)) |
| 782 | |
| 783 | stop_hook = tf.train.StopAtStepHook(last_step=steps) |
| 784 | log_hook = tf.train.LoggingTensorHook( |
| 785 | { |
| 786 | 'steps': model.global_step, |
| 787 | 'loss': model.loss |
| 788 | }, every_n_iter=100) |
| 789 | hooks.append(stop_hook) |
| 790 | hooks.append(log_hook) |
| 791 | if args.timeline > 0: |
| 792 | hooks.append( |
| 793 | tf.train.ProfilerHook(save_steps=args.timeline, |
| 794 | output_dir=checkpoint_dir)) |
| 795 | save_steps = args.save_steps if args.save_steps or args.no_eval else steps |
| 796 | ''' |
| 797 | Incremental_Checkpoint |
| 798 | Please add `save_incremental_checkpoint_secs` in 'tf.train.MonitoredTrainingSession' |
| 799 | it's default to None, Incremental_save checkpoint time in seconds can be set |
| 800 | to use incremental checkpoint function, like `tf.train.MonitoredTrainingSession( |
| 801 | save_incremental_checkpoint_secs=args.incremental_ckpt)` |
| 802 | ''' |
| 803 | if args.incremental_ckpt and not args.tf: |
| 804 | print("Incremental_Checkpoint is not really enabled.") |
| 805 | print("Please see the comments in the code.") |
| 806 | sys.exit() |
| 807 | |
| 808 | with tf.train.MonitoredTrainingSession( |
| 809 | master=server.target if server else '', |
| 810 | is_chief=tf_config['is_chief'] if tf_config else True, |
| 811 | hooks=hooks, |
| 812 | scaffold=scaffold, |
| 813 | checkpoint_dir=checkpoint_dir, |
| 814 | save_checkpoint_steps=save_steps, |
| 815 | summary_dir=checkpoint_dir, |
| 816 | save_summaries_steps=args.save_steps, |
| 817 | config=sess_config) as sess: |
| 818 | while not sess.should_stop(): |
| 819 | sess.run([model.loss, model.train_op]) |
| 820 | print("Training completed.") |
| 821 | |
| 822 | |
| 823 | def eval(sess_config, input_hooks, model, data_init_op, steps, checkpoint_dir): |