(_)
| 331 | |
| 332 | |
| 333 | def main(_): |
| 334 | tf.logging.set_verbosity(tf.logging.INFO) |
| 335 | |
| 336 | tokenization.validate_case_matches_checkpoint(FLAGS.do_lower_case, |
| 337 | FLAGS.init_checkpoint) |
| 338 | |
| 339 | if not FLAGS.do_train and not FLAGS.do_eval: |
| 340 | raise ValueError("At least one of `do_train`, `do_eval` must be True.") |
| 341 | |
| 342 | bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) |
| 343 | |
| 344 | if FLAGS.max_seq_length > bert_config.max_position_embeddings: |
| 345 | raise ValueError( |
| 346 | "Cannot use sequence length %d because the BERT model " |
| 347 | "was only trained up to sequence length %d" % |
| 348 | (FLAGS.max_seq_length, bert_config.max_position_embeddings)) |
| 349 | |
| 350 | tf.gfile.MakeDirs(FLAGS.output_dir) |
| 351 | |
| 352 | tpu_cluster_resolver = None |
| 353 | if FLAGS.use_tpu and FLAGS.tpu_name: |
| 354 | tpu_cluster_resolver = contrib_cluster_resolver.TPUClusterResolver( |
| 355 | FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project) |
| 356 | |
| 357 | is_per_host = contrib_tpu.InputPipelineConfig.PER_HOST_V2 |
| 358 | run_config = contrib_tpu.RunConfig( |
| 359 | cluster=tpu_cluster_resolver, |
| 360 | master=FLAGS.master, |
| 361 | model_dir=FLAGS.output_dir, |
| 362 | save_checkpoints_steps=FLAGS.save_checkpoints_steps, |
| 363 | tpu_config=contrib_tpu.TPUConfig( |
| 364 | iterations_per_loop=FLAGS.iterations_per_loop, |
| 365 | num_shards=FLAGS.num_tpu_cores, |
| 366 | per_host_input_for_training=is_per_host)) |
| 367 | |
| 368 | num_train_steps = None |
| 369 | num_warmup_steps = None |
| 370 | if FLAGS.do_train: |
| 371 | num_train_steps = int(FLAGS.train_data_size / FLAGS.train_batch_size) |
| 372 | num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) |
| 373 | num_loop_steps = int(num_train_steps / FLAGS.num_train_rounds) |
| 374 | |
| 375 | model_fn = model_fn_builder( |
| 376 | bert_config=bert_config, |
| 377 | init_checkpoint=FLAGS.init_checkpoint, |
| 378 | learning_rate=FLAGS.learning_rate, |
| 379 | num_train_steps=num_train_steps, |
| 380 | num_warmup_steps=num_warmup_steps, |
| 381 | use_tpu=FLAGS.use_tpu, |
| 382 | use_one_hot_embeddings=FLAGS.use_tpu) |
| 383 | |
| 384 | # If TPU is not available, this will fall back to normal Estimator on CPU |
| 385 | # or GPU. |
| 386 | estimator = contrib_tpu.TPUEstimator( |
| 387 | use_tpu=FLAGS.use_tpu, |
| 388 | model_fn=model_fn, |
| 389 | config=run_config, |
| 390 | train_batch_size=FLAGS.train_batch_size, |
nothing calls this directly
no test coverage detected
searching dependent graphs…