train.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Train the skip-thoughts model."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from skip_thoughts import configuration
  21. from skip_thoughts import skip_thoughts_model
  22. FLAGS = tf.flags.FLAGS
  23. tf.flags.DEFINE_string("input_file_pattern", None,
  24. "File pattern of sharded TFRecord files containing "
  25. "tf.Example protos.")
  26. tf.flags.DEFINE_string("train_dir", None,
  27. "Directory for saving and loading checkpoints.")
  28. tf.logging.set_verbosity(tf.logging.INFO)
  29. def _setup_learning_rate(config, global_step):
  30. """Sets up the learning rate with optional exponential decay.
  31. Args:
  32. config: Object containing learning rate configuration parameters.
  33. global_step: Tensor; the global step.
  34. Returns:
  35. learning_rate: Tensor; the learning rate with exponential decay.
  36. """
  37. if config.learning_rate_decay_factor > 0:
  38. learning_rate = tf.train.exponential_decay(
  39. learning_rate=float(config.learning_rate),
  40. global_step=global_step,
  41. decay_steps=config.learning_rate_decay_steps,
  42. decay_rate=config.learning_rate_decay_factor,
  43. staircase=False)
  44. else:
  45. learning_rate = tf.constant(config.learning_rate)
  46. return learning_rate
  47. def main(unused_argv):
  48. if not FLAGS.input_file_pattern:
  49. raise ValueError("--input_file_pattern is required.")
  50. if not FLAGS.train_dir:
  51. raise ValueError("--train_dir is required.")
  52. model_config = configuration.model_config(
  53. input_file_pattern=FLAGS.input_file_pattern)
  54. training_config = configuration.training_config()
  55. tf.logging.info("Building training graph.")
  56. g = tf.Graph()
  57. with g.as_default():
  58. model = skip_thoughts_model.SkipThoughtsModel(model_config, mode="train")
  59. model.build()
  60. learning_rate = _setup_learning_rate(training_config, model.global_step)
  61. optimizer = tf.train.AdamOptimizer(learning_rate)
  62. train_tensor = tf.contrib.slim.learning.create_train_op(
  63. total_loss=model.total_loss,
  64. optimizer=optimizer,
  65. global_step=model.global_step,
  66. clip_gradient_norm=training_config.clip_gradient_norm)
  67. saver = tf.train.Saver()
  68. tf.contrib.slim.learning.train(
  69. train_op=train_tensor,
  70. logdir=FLAGS.train_dir,
  71. graph=g,
  72. global_step=model.global_step,
  73. number_of_steps=training_config.number_of_steps,
  74. save_summaries_secs=training_config.save_summaries_secs,
  75. saver=saver,
  76. save_interval_secs=training_config.save_model_secs)
  77. if __name__ == "__main__":
  78. tf.app.run()