parser_trainer.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Copyright 2016 Google Inc. 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. """A program to train a tensorflow neural net parser from a conll file."""
  16. import os
  17. import os.path
  18. import random
  19. import time
  20. import tensorflow as tf
  21. from tensorflow.python.platform import gfile
  22. from tensorflow.python.platform import tf_logging as logging
  23. from google.protobuf import text_format
  24. from syntaxnet.ops import gen_parser_ops
  25. from syntaxnet import task_spec_pb2
  26. from syntaxnet import sentence_pb2
  27. from dragnn.protos import spec_pb2
  28. from dragnn.python import evaluation
  29. from dragnn.python import graph_builder
  30. from dragnn.python import lexicon
  31. from dragnn.python import sentence_io
  32. from dragnn.python import spec_builder
  33. from dragnn.python import trainer_lib
  34. import dragnn.python.load_dragnn_cc_impl
  35. import syntaxnet.load_parser_ops
  36. flags = tf.app.flags
  37. FLAGS = flags.FLAGS
  38. flags.DEFINE_string('tf_master', '',
  39. 'TensorFlow execution engine to connect to.')
  40. flags.DEFINE_string('resource_path', '', 'Path to constructed resources.')
  41. flags.DEFINE_string('tensorboard_dir', '',
  42. 'Directory for TensorBoard logs output.')
  43. flags.DEFINE_string('checkpoint_filename', '',
  44. 'Filename to save the best checkpoint to.')
  45. flags.DEFINE_string('training_corpus_path', '', 'Path to training data.')
  46. flags.DEFINE_string('dev_corpus_path', '', 'Path to development set data.')
  47. flags.DEFINE_bool('compute_lexicon', False, '')
  48. flags.DEFINE_bool('projectivize_training_set', True, '')
  49. flags.DEFINE_integer('batch_size', 4, 'Batch size.')
  50. flags.DEFINE_integer('report_every', 200,
  51. 'Report cost and training accuracy every this many steps.')
  52. def main(unused_argv):
  53. logging.set_verbosity(logging.INFO)
  54. if not gfile.IsDirectory(FLAGS.resource_path):
  55. gfile.MakeDirs(FLAGS.resource_path)
  56. # Constructs lexical resources for SyntaxNet in the given resource path, from
  57. # the training data.
  58. if FLAGS.compute_lexicon:
  59. logging.info('Computing lexicon...')
  60. lexicon.build_lexicon(FLAGS.resource_path, FLAGS.training_corpus_path)
  61. # Construct the "lookahead" ComponentSpec. This is a simple right-to-left RNN
  62. # sequence model, which encodes the context to the right of each token. It has
  63. # no loss except for the downstream components.
  64. char2word = spec_builder.ComponentSpecBuilder('char_lstm')
  65. char2word.set_network_unit(
  66. name='wrapped_units.LayerNormBasicLSTMNetwork',
  67. hidden_layer_sizes='256')
  68. char2word.set_transition_system(name='char-shift-only', left_to_right='true')
  69. char2word.add_fixed_feature(name='chars', fml='char-input.text-char',
  70. embedding_dim=16)
  71. char2word.fill_from_resources(FLAGS.resource_path, FLAGS.tf_master)
  72. lookahead = spec_builder.ComponentSpecBuilder('lookahead')
  73. lookahead.set_network_unit(
  74. name='wrapped_units.LayerNormBasicLSTMNetwork',
  75. hidden_layer_sizes='256')
  76. lookahead.set_transition_system(name='shift-only', left_to_right='false')
  77. lookahead.add_link(source=char2word, fml='input.last-char-focus',
  78. embedding_dim=32)
  79. lookahead.fill_from_resources(FLAGS.resource_path, FLAGS.tf_master)
  80. # Construct the ComponentSpec for tagging. This is a simple left-to-right RNN
  81. # sequence tagger.
  82. tagger = spec_builder.ComponentSpecBuilder('tagger')
  83. tagger.set_network_unit(
  84. name='wrapped_units.LayerNormBasicLSTMNetwork',
  85. hidden_layer_sizes='256')
  86. tagger.set_transition_system(name='tagger')
  87. tagger.add_token_link(source=lookahead, fml='input.focus', embedding_dim=32)
  88. tagger.fill_from_resources(FLAGS.resource_path, FLAGS.tf_master)
  89. # Construct the ComponentSpec for parsing.
  90. parser = spec_builder.ComponentSpecBuilder('parser')
  91. parser.set_network_unit(name='FeedForwardNetwork', hidden_layer_sizes='256',
  92. layer_norm_hidden='True')
  93. parser.set_transition_system(name='arc-standard')
  94. parser.add_token_link(source=lookahead, fml='input.focus', embedding_dim=32)
  95. parser.add_token_link(
  96. source=tagger,
  97. fml='input.focus stack.focus stack(1).focus',
  98. embedding_dim=32)
  99. # Recurrent connection for the arc-standard parser. For both tokens on the
  100. # stack, we connect to the last time step to either SHIFT or REDUCE that
  101. # token. This allows the parser to build up compositional representations of
  102. # phrases.
  103. parser.add_link(
  104. source=parser, # recurrent connection
  105. name='rnn-stack', # unique identifier
  106. fml='stack.focus stack(1).focus', # look for both stack tokens
  107. source_translator='shift-reduce-step', # maps token indices -> step
  108. embedding_dim=32) # project down to 32 dims
  109. parser.fill_from_resources(FLAGS.resource_path, FLAGS.tf_master)
  110. master_spec = spec_pb2.MasterSpec()
  111. master_spec.component.extend([char2word.spec, lookahead.spec,
  112. tagger.spec, parser.spec])
  113. logging.info('Constructed master spec: %s', str(master_spec))
  114. hyperparam_config = spec_pb2.GridPoint()
  115. hyperparam_config.decay_steps = 128000
  116. hyperparam_config.learning_rate = 0.001
  117. hyperparam_config.learning_method = 'adam'
  118. hyperparam_config.adam_beta1 = 0.9
  119. hyperparam_config.adam_beta2 = 0.9
  120. hyperparam_config.adam_eps = 0.0001
  121. hyperparam_config.gradient_clip_norm = 1
  122. hyperparam_config.self_norm_alpha = 1.0
  123. hyperparam_config.use_moving_average = True
  124. hyperparam_config.dropout_rate = 0.7
  125. hyperparam_config.seed = 1
  126. # Build the TensorFlow graph.
  127. graph = tf.Graph()
  128. with graph.as_default():
  129. builder = graph_builder.MasterBuilder(master_spec, hyperparam_config)
  130. component_targets = spec_builder.default_targets_from_spec(master_spec)
  131. trainers = [
  132. builder.add_training_from_config(target) for target in component_targets
  133. ]
  134. assert len(trainers) == 2
  135. annotator = builder.add_annotation()
  136. builder.add_saver()
  137. # Read in serialized protos from training data.
  138. training_set = sentence_io.ConllSentenceReader(
  139. FLAGS.training_corpus_path,
  140. projectivize=FLAGS.projectivize_training_set).corpus()
  141. dev_set = sentence_io.ConllSentenceReader(
  142. FLAGS.dev_corpus_path, projectivize=False).corpus()
  143. # Ready to train!
  144. logging.info('Training on %d sentences.', len(training_set))
  145. logging.info('Tuning on %d sentences.', len(dev_set))
  146. pretrain_steps = [100, 0]
  147. tagger_steps = 1000
  148. train_steps = [tagger_steps, 8 * tagger_steps]
  149. tf.logging.info('Creating TensorFlow checkpoint dir...')
  150. gfile.MakeDirs(os.path.dirname(FLAGS.checkpoint_filename))
  151. summary_writer = trainer_lib.get_summary_writer(FLAGS.tensorboard_dir)
  152. with tf.Session(FLAGS.tf_master, graph=graph) as sess:
  153. # Make sure to re-initialize all underlying state.
  154. sess.run(tf.global_variables_initializer())
  155. trainer_lib.run_training(
  156. sess, trainers, annotator, evaluation.parser_summaries, pretrain_steps,
  157. train_steps, training_set, dev_set, dev_set, FLAGS.batch_size,
  158. summary_writer, FLAGS.report_every, builder.saver,
  159. FLAGS.checkpoint_filename)
  160. if __name__ == '__main__':
  161. tf.app.run()