Kaggle competition: TensorFlow 2.0 Question Answering
train['Census_OSBuildRevision'] = train['Census_OSBuildRevision'].astype('category')
 test['Census_OSBuildRevision'] = test['Census_OSBuildRevision'].astype('category' )<data_type_conversions>
tf.logging.set_verbosity(tf.logging.INFO)
 tf.disable_eager_execution()
def del_all_flags(FLAGS):
flags_dict = FLAGS._flags()
keys_list = [keys for keys in flags_dict]
for keys in keys_list:
FLAGS.__delattr__(keys)
 del_all_flags(tf.app.flags.FLAGS)
 flags = tf.app.flags
flags.DEFINE_string("bert_config_file", "/kaggle/input/bert-disjoint/bert_config.json", " ")
 flags.DEFINE_string("vocab_file_bert", "/kaggle/input/bert-disjoint/vocab-nq.txt", " ")
 flags.DEFINE_string("output_dir_bert", "/kaggle/input/bert-disjoint/", " ")
 flags.DEFINE_string("init_checkpoint_bert", "/kaggle/input/bert-disjoint/model.ckpt-15000", " ")
 flags.DEFINE_string("albert_config_file", "/kaggle/input/albert-yes-no/albert_config.json", " ")
 flags.DEFINE_string("vocab_file_albert", "/kaggle/input/albert-yes-no/30k-clean.vocab", " ")
 flags.DEFINE_string("spm_model_file", "/kaggle/input/albert-yes-no/30k-clean.model", " ")
 flags.DEFINE_string("output_dir_albert", "/kaggle/input/albert-yes-no/", " ")
 flags.DEFINE_string("init_checkpoint_albert", "/kaggle/input/albert-yes-no/model.ckpt-1206", " ")
 flags.DEFINE_string("eval_record_file_bert", "eval_bert.tf_record", " ")
 flags.DEFINE_integer("max_seq_length", 512, " ")
 flags.DEFINE_integer("predict_batch_size", 8, " ")
 flags.DEFINE_string(
 "predict_file", "/kaggle/input/tensorflow2-question-answering/simplified-nq-test.jsonl",
"NQ json for predictions.E.g., dev-v1.1.jsonl.gz or test-v1.1.jsonl.gz")
 flags.DEFINE_boolean("logtostderr", True, "Logs to stderr")
 flags.DEFINE_boolean("undefok", True, "it's okay to be undefined")
 flags.DEFINE_string('f', '', 'kernel')
 flags.DEFINE_string('HistoryManager.hist_file', '', 'kernel')
 FLAGS = flags.FLAGS
train['Census_FirmwareManufacturerIdentifier'] = train['Census_FirmwareManufacturerIdentifier'].astype('category')
 test['Census_FirmwareManufacturerIdentifier'] = test['Census_FirmwareManufacturerIdentifier'].astype('category' )<data_type_conversions>
bert_tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file_bert, do_lower_case=True)
 eval_examples = bert_disjoint_utils.read_nq_examples(input_file=FLAGS.predict_file)
 eval_writer = bert_disjoint_utils.FeatureWriter(filename=FLAGS.eval_record_file_bert, is_training=False)
 eval_features = []
def append_feature(feature):
eval_features.append(feature)
 eval_writer.process_feature(feature)
 num_spans_to_ids = bert_disjoint_utils.convert_examples_to_features(examples=eval_examples,
tokenizer=bert_tokenizer,
output_fn=append_feature)
 eval_writer.close()
eval_filename = eval_writer.filename
train['Census_FirmwareVersionIdentifier'] = train['Census_FirmwareVersionIdentifier'].astype('category')
 test['Census_FirmwareVersionIdentifier'] = test['Census_FirmwareVersionIdentifier'].astype('category' )<data_type_conversions>
bert_config = bert_disjoint_modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
 run_config = tf.estimator.RunConfig(model_dir=FLAGS.output_dir_bert)
 model_fn = bert_disjoint_fn_builder.model_fn_builder(bert_config=bert_config,
init_checkpoint=FLAGS.init_checkpoint_bert)
 estimator = tf.estimator.Estimator(model_fn=model_fn,
model_dir=FLAGS.output_dir_bert,
config=run_config,
params={"batch_size": FLAGS.predict_batch_size})
 predict_input_fn = bert_disjoint_fn_builder.input_fn_builder(input_file=eval_filename,
seq_length=FLAGS.max_seq_length,
drop_remainder=False)
 tf.logging.info("***** Running predictions *****")
 tf.logging.info(" Num orig examples = %d", len(eval_examples))
tf.logging.info(" Num split examples = %d", len(eval_features))
tf.logging.info(" Batch size = %d", FLAGS.predict_batch_size)
 for spans, ids in num_spans_to_ids.items() :
tf.logging.info(" Num split into %d = %d", spans, len(ids))
all_results = []
for result in estimator.predict(predict_input_fn, yield_single_examples=True):
if len(all_results)% 1000 == 0:
tf.logging.info("Processing example: %d" %(len(all_results)))
 unique_id = int(result["unique_ids"])
 start_logits = [float(x)for x in result["start_logits"].flat]
end_logits = [float(x)for x in result["end_logits"].flat]
all_results.append(bert_disjoint_utils.RawResult(unique_id=unique_id,
start_logits=start_logits,
end_logits=end_logits))
candidates_dict = bert_disjoint_utils.read_candidates(FLAGS.predict_file)
 eval_features = [tf.train.Example.FromString(r)for r in tf.python_io.tf_record_iterator(eval_filename)]
nq_pred_dict = bert_disjoint_utils.compute_pred_dict(candidates_dict, eval_features, [r._asdict() for r in all_results])
 predictions = list(nq_pred_dict.values())
 del bert_config, run_config, model_fn, estimator, predict_input_fn
del all_results, candidates_dict, eval_features, nq_pred_dict
train['OsBuild'] = train['OsBuild'].astype('category')
 test['OsBuild'] = test['OsBuild'].astype('category' )<feature_engineering>
albert_tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file_albert,
do_lower_case=True,
spm_model_file=FLAGS.spm_model_file)
 eval_examples = albert_yes_no_utils.read_nq_examples(input_file="yes_no_questions.jsonl")
 eval_writer = albert_yes_no_utils.FeatureWriter(filename="yes_no_questions.eval.tf_record",
albert_tokenizer=albert_tokenizer)
 n_features, example_to_feature_map, feature_to_example_map = \
eval_writer.file_based_convert_examples_to_features_span(eval_examples )
def group_battery(x):
x = x.lower()
if 'li' in x:
return 1
else:
return 0
train['Census_InternalBatteryType'] = train['Census_InternalBatteryType'].apply(group_battery)
 test['Census_InternalBatteryType'] = test['Census_InternalBatteryType'].apply(group_battery )<data_type_conversions>
albert_config = albert_yes_no_modeling.AlbertConfig.from_json_file(FLAGS.albert_config_file)
 run_config = tf.estimator.RunConfig(model_dir=FLAGS.output_dir_albert)
 model_fn = albert_yes_no_fn_builder.model_fn_builder(albert_config=albert_config,
init_checkpoint=FLAGS.init_checkpoint_albert)
 estimator = tf.estimator.Estimator(model_fn=model_fn,
model_dir=FLAGS.output_dir_albert,
config=run_config,
params={"batch_size": FLAGS.predict_batch_size})
 predict_input_fn = albert_yes_no_fn_builder.input_fn_builder(input_file="yes_no_questions.eval.tf_record",
seq_length=FLAGS.max_seq_length,
drop_remainder=False)
 tf.logging.info(" Num features: %d" % n_features)
 all_results = []
for result in estimator.predict(predict_input_fn, yield_single_examples=True):
if len(all_results)% 100 == 0:
tf.logging.info("Processing example: %d" %(len(all_results)))
 answer_type_logits = [float(x)for x in result["answer_type_logits"].flat]
all_results.append(answer_type_logits)
 yes_no_pred_dict = albert_yes_no_utils.compute_pred_dicts_cls("yes_no_questions.jsonl",
all_results,
example_to_feature_map,
feature_to_example_map)
 del albert_config, run_config, model_fn, estimator, predict_input_fn
del all_results
train['Census_OSEdition'] = train['Census_OSEdition'].astype(str)
 test['Census_OSEdition'] = test['Census_OSEdition'].astype(str)
 train['Census_OSEdition'] = train['Census_OSEdition'].apply(rename_edition)
 test['Census_OSEdition'] = test['Census_OSEdition'].apply(rename_edition)
 train['Census_OSEdition'] = train['Census_OSEdition'].astype('category')
 test['Census_OSEdition'] = test['Census_OSEdition'].astype('category' )<data_type_conversions>
create_submission.merge_predictions(predictions, yes_no_pred_dict)
 create_submission.create_answers(predictions, "submission.csv" )