Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 303871250
  • Loading branch information
tensorflower-gardener committed Mar 31, 2020
1 parent 91717b1 commit ad34b62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
36 changes: 33 additions & 3 deletions official/nlp/albert/run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
from __future__ import print_function

import json
import os
import time

from absl import app
from absl import flags
from absl import logging
import tensorflow as tf

from official.nlp.albert import configs as albert_configs
Expand Down Expand Up @@ -53,7 +56,7 @@ def train_squad(strategy,


def predict_squad(strategy, input_meta_data):
"""Makes predictions for a squad dataset."""
"""Makes predictions for the squad dataset."""
bert_config = albert_configs.AlbertConfig.from_json_file(
FLAGS.bert_config_file)
tokenizer = tokenization.FullSentencePieceTokenizer(
Expand All @@ -63,6 +66,18 @@ def predict_squad(strategy, input_meta_data):
bert_config, squad_lib_sp)


def eval_squad(strategy, input_meta_data):
"""Evaluate on the squad dataset."""
bert_config = albert_configs.AlbertConfig.from_json_file(
FLAGS.bert_config_file)
tokenizer = tokenization.FullSentencePieceTokenizer(
sp_model_file=FLAGS.sp_model_file)

eval_metrics = run_squad_helper.eval_squad(
strategy, input_meta_data, tokenizer, bert_config, squad_lib_sp)
return eval_metrics


def export_squad(model_export_path, input_meta_data):
"""Exports a trained model as a `SavedModel` for inference.
Expand Down Expand Up @@ -97,10 +112,25 @@ def main(_):
num_gpus=FLAGS.num_gpus,
all_reduce_alg=FLAGS.all_reduce_alg,
tpu_address=FLAGS.tpu)
if FLAGS.mode in ('train', 'train_and_predict'):

if 'train' in FLAGS.mode:
train_squad(strategy, input_meta_data, run_eagerly=FLAGS.run_eagerly)
if FLAGS.mode in ('predict', 'train_and_predict'):
if 'predict' in FLAGS.mode:
predict_squad(strategy, input_meta_data)
if 'eval' in FLAGS.mode:
eval_metrics = eval_squad(strategy, input_meta_data)
f1_score = eval_metrics['final_f1']
logging.info('SQuAD eval F1-score: %f', f1_score)
summary_dir = os.path.join(FLAGS.model_dir, 'summaries', 'eval')
summary_writer = tf.summary.create_file_writer(summary_dir)
with summary_writer.as_default():
# TODO(lehou): write to the correct step number.
tf.summary.scalar('F1-score', f1_score, step=0)
summary_writer.flush()
# Also write eval_metrics to json file.
squad_lib_sp.write_to_json_files(
eval_metrics, os.path.join(summary_dir, 'eval_metrics.json'))
time.sleep(60)


if __name__ == '__main__':
Expand Down
14 changes: 5 additions & 9 deletions official/nlp/bert/run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import json
import os
import tempfile
import time

from absl import app
Expand Down Expand Up @@ -130,18 +129,15 @@ def main(_):
eval_metrics = eval_squad(strategy, input_meta_data)
f1_score = eval_metrics['final_f1']
logging.info('SQuAD eval F1-score: %f', f1_score)
if (not strategy) or strategy.extended.should_save_summary:
summary_dir = os.path.join(FLAGS.model_dir, 'summaries')
else:
summary_dir = tempfile.mkdtemp()
summary_writer = tf.summary.create_file_writer(
os.path.join(summary_dir, 'eval'))
summary_dir = os.path.join(FLAGS.model_dir, 'summaries', 'eval')
summary_writer = tf.summary.create_file_writer(summary_dir)
with summary_writer.as_default():
# TODO(lehou): write to the correct step number.
tf.summary.scalar('F1-score', f1_score, step=0)
summary_writer.flush()
# Wait for some time, for the depending mldash/tensorboard jobs to finish
# exporting the final F1-score.
# Also write eval_metrics to json file.
squad_lib_wp.write_to_json_files(
eval_metrics, os.path.join(summary_dir, 'eval_metrics.json'))
time.sleep(60)


Expand Down

0 comments on commit ad34b62

Please sign in to comment.