def file_based_convert_examples_to_features(examples, max_seq_length, tokenizer,
                                            output_file):
  """Convert a set of `InputExample`s to a TFRecord file."""

  def create_int_feature(values):
    f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))
    return f

  def create_string_feature(values):
    f = tf.train.Feature(bytes_list=tf.train.BytesList(value=list(values)))
    return f

  def feature_list_from_input_feature(input_feature):
    features = collections.OrderedDict()
    features["guid"] = [create_string_feature([input_feature.guid])]
    features["input_ids"] = [create_int_feature(input_feature.input_ids)]
    features["input_mask"] = [create_int_feature(input_feature.input_mask)]
    features["segment_ids"] = [create_int_feature(input_feature.segment_ids)]
    return features

  writer = tf.python_io.TFRecordWriter(output_file)

  for (ex_index, example) in enumerate(examples):
    if ex_index % 10000 == 0:
      tf.logging.info("Writing example %d of %d" % (ex_index, len(examples)))

    query_features = feature_list_from_input_feature(
        convert_single_example(ex_index, example.query, max_seq_length,
                               tokenizer))
    features_list_dict = {
        name: tf.train.FeatureList() for name in query_features.keys()
    }
    for name in query_features:
      features_list_dict[name].feature.extend(query_features[name])

    for _, test_set in six.iteritems(example.sets):
      for test_example in test_set:
        test_features = feature_list_from_input_feature(
            convert_single_example(ex_index, test_example, max_seq_length,
                                   tokenizer))
        for name in test_features:
          features_list_dict[name].feature.extend(test_features[name])

    sequence_example = tf.train.SequenceExample(
        feature_lists=tf.train.FeatureLists(feature_list=features_list_dict))
    writer.write(sequence_example.SerializeToString())
Convert a set of `InputExample`s to a TFRecord file.
language/relation_learning/models/bert_fewshot_classifier.py