| 467 | |
| 468 | |
| 469 | def train(sess_config, |
| 470 | input_hooks, |
| 471 | model, |
| 472 | data_init_op, |
| 473 | steps, |
| 474 | checkpoint_dir, |
| 475 | tf_config=None, |
| 476 | server=None): |
| 477 | model.is_training = True |
| 478 | hooks = [] |
| 479 | hooks.extend(input_hooks) |
| 480 | |
| 481 | scaffold = tf.train.Scaffold( |
| 482 | local_init_op=tf.group(tf.local_variables_initializer(), data_init_op), |
| 483 | saver=tf.train.Saver(max_to_keep=args.keep_checkpoint_max, sharded=True)) |
| 484 | |
| 485 | stop_hook = tf.train.StopAtStepHook(last_step=steps) |
| 486 | log_hook = tf.train.LoggingTensorHook( |
| 487 | { |
| 488 | 'steps': model.global_step, |
| 489 | 'loss': model.loss |
| 490 | }, every_n_iter=100) |
| 491 | hooks.append(stop_hook) |
| 492 | hooks.append(log_hook) |
| 493 | if args.timeline > 0: |
| 494 | hooks.append( |
| 495 | tf.train.ProfilerHook(save_steps=args.timeline, |
| 496 | output_dir=checkpoint_dir)) |
| 497 | save_steps = args.save_steps if args.save_steps or args.no_eval else steps |
| 498 | ''' |
| 499 | Incremental_Checkpoint |
| 500 | Please add `save_incremental_checkpoint_secs` in 'tf.train.MonitoredTrainingSession' |
| 501 | it's default to None, Incremental_save checkpoint time in seconds can be set |
| 502 | to use incremental checkpoint function, like `tf.train.MonitoredTrainingSession( |
| 503 | save_incremental_checkpoint_secs=args.incremental_ckpt)` |
| 504 | ''' |
| 505 | if args.incremental_ckpt and not args.tf: |
| 506 | print("Incremental_Checkpoint is not really enabled.") |
| 507 | print("Please see the comments in the code.") |
| 508 | sys.exit() |
| 509 | |
| 510 | with tf.train.MonitoredTrainingSession( |
| 511 | master=server.target if server else '', |
| 512 | is_chief=tf_config['is_chief'] if tf_config else True, |
| 513 | hooks=hooks, |
| 514 | scaffold=scaffold, |
| 515 | checkpoint_dir=checkpoint_dir, |
| 516 | save_checkpoint_steps=save_steps, |
| 517 | summary_dir=checkpoint_dir, |
| 518 | save_summaries_steps=args.save_steps, |
| 519 | config=sess_config) as sess: |
| 520 | while not sess.should_stop(): |
| 521 | sess.run([model.loss, model.train_op]) |
| 522 | print("Training completed.") |
| 523 | |
| 524 | |
| 525 | def eval(sess_config, input_hooks, model, data_init_op, steps, checkpoint_dir): |