Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TensorFlow compatibility in BERT scripts #743

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def write_instance_to_example_files(instances, tokenizer, max_seq_length,
total_written += 1

if inst_index < 20:
tf.logging.info("*** Example ***")
tf.logging.info("tokens: %s" % " ".join(
tf.compat.v1.logging.info("*** Example ***")
tf.compat.v1.logging.info("tokens: %s" % " ".join(
[tokenization.printable_text(x) for x in instance.tokens]))

for feature_name in features.keys():
Expand All @@ -139,13 +139,13 @@ def write_instance_to_example_files(instances, tokenizer, max_seq_length,
values = feature.int64_list.value
elif feature.float_list.value:
values = feature.float_list.value
tf.logging.info(
tf.compat.v1.logging.info(
"%s: %s" % (feature_name, " ".join([str(x) for x in values])))

for writer in writers:
writer.close()

tf.logging.info("Wrote %d total instances", total_written)
tf.compat.v1.logging.info("Wrote %d total instances", total_written)


def create_int_feature(values):
Expand All @@ -171,7 +171,7 @@ def create_training_instances(input_files, tokenizer, max_seq_length,
# (2) Blank lines between documents. Document boundaries are needed so
# that the "next sentence prediction" task doesn't span between documents.
for input_file in input_files:
with tf.gfile.GFile(input_file, "r") as reader:
with tf.io.gfile.GFile(input_file, "r") as reader:
while True:
line = tokenization.convert_to_unicode(reader.readline())
if not line:
Expand Down Expand Up @@ -393,18 +393,18 @@ def truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng):


def main(_):
tf.logging.set_verbosity(tf.logging.INFO)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)

tokenizer = tokenization.FullTokenizer(
vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)

input_files = []
for input_pattern in FLAGS.input_file.split(","):
input_files.extend(tf.gfile.Glob(input_pattern))
input_files.extend(tf.io.gfile.glob(input_pattern))

tf.logging.info("*** Reading from input files ***")
tf.compat.v1.logging.info("*** Reading from input files ***")
for input_file in input_files:
tf.logging.info(" %s", input_file)
tf.compat.v1.logging.info(" %s", input_file)

rng = random.Random(FLAGS.random_seed)
instances = create_training_instances(
Expand All @@ -413,9 +413,9 @@ def main(_):
rng)

output_files = FLAGS.output_file.split(",")
tf.logging.info("*** Writing to output files ***")
tf.compat.v1.logging.info("*** Writing to output files ***")
for output_file in output_files:
tf.logging.info(" %s", output_file)
tf.compat.v1.logging.info(" %s", output_file)

write_instance_to_example_files(instances, tokenizer, FLAGS.max_seq_length,
FLAGS.max_predictions_per_seq, output_files)
Expand All @@ -425,4 +425,4 @@ def main(_):
flags.mark_flag_as_required("input_file")
flags.mark_flag_as_required("output_file")
flags.mark_flag_as_required("vocab_file")
tf.app.run()
tf.compat.v1.app.run()
Loading