Performing inference with a BERT (TF 1.x) saved model

不打扰是莪最后的温柔 提交于 2020-05-30 07:58:05

问题


I'm stuck on one line of code and have been stalled on a project all weekend as a result.

I am working on a project that uses BERT for sentence classification. I have successfully trained the model, and I can test the results using the example code from run_classifier.py.

I can export the model using this example code (which has been reposted repeatedly, so I believe that it's right for this model):

def export(self):
  def serving_input_fn():
    label_ids = tf.placeholder(tf.int32, [None], name='label_ids')
    input_ids = tf.placeholder(tf.int32, [None, self.max_seq_length], name='input_ids')
    input_mask = tf.placeholder(tf.int32, [None, self.max_seq_length], name='input_mask')
    segment_ids = tf.placeholder(tf.int32, [None, self.max_seq_length], name='segment_ids')
    input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn({
        'label_ids': label_ids, 'input_ids': input_ids,
        'input_mask': input_mask, 'segment_ids': segment_ids})()
    return input_fn
  self.estimator._export_to_tpu = False
  self.estimator.export_savedmodel(self.output_dir, serving_input_fn)

I can also load the exported estimator (where the export function saves the exported model into a subdirectory labeled with a timestamp):

predict_fn = predictor.from_saved_model(self.output_dir + timestamp_number)

However, for the life of me, I cannot figure out what to provide to predict_fn as input for inference. Here is my best code at the moment:

def predict(self):
  input = 'Test input'
  guid = 'predict-0'
  text_a = tokenization.convert_to_unicode(input)
  label = self.label_list[0]
  examples = [InputExample(guid=guid, text_a=text_a, text_b=None, label=label)]
  features = convert_examples_to_features(examples, self.label_list,
    self.max_seq_length, self.tokenizer)
  predict_input_fn = input_fn_builder(features, self.max_seq_length, False)
  predict_fn = predictor.from_saved_model(self.output_dir + timestamp_number)
  result = predict_fn(predict_input_fn)       # this generates an error
  print(result)

It doesn't seem to matter what I provide to predict_fn: the examples array, the features array, the predict_input_fn function. Clearly, predict_fn wants a dictionary of some type - but every single thing that I've tried generates an exception due to a tensor mismatch or other errors that generally mean: bad input.

I presumed that the from_saved_model function wants the same sort of input as the model test function - apparently, that's not the case.

It seems that lots of people have asked this very question - "how do I use an exported BERT TensorFlow model for inference?" - and have gotten no answers:

Thread #1

Thread #2

Thread #3

Thread #4

Any help? Thanks in advance.


回答1:


Thank you for this post. Your serving_input_fn was the piece I was missing! Your predict function needs to be changed to feed the features dict directly, rather than use the predict_input_fn:

def predict(sentences):
    labels = [0, 1]
    input_examples = [
        run_classifier.InputExample(
            guid="",
            text_a = x,
            text_b = None,
            label = 0
        ) for x in sentences] # here, "" is just a dummy label
    input_features = run_classifier.convert_examples_to_features(
        input_examples, labels, MAX_SEQ_LEN, tokenizer
    )
    # this is where pred_input_fn is replaced
    all_input_ids = []
    all_input_mask = []
    all_segment_ids = []
    all_label_ids = []

    for feature in input_features:
        all_input_ids.append(feature.input_ids)
        all_input_mask.append(feature.input_mask)
        all_segment_ids.append(feature.segment_ids)
        all_label_ids.append(feature.label_id)
    pred_dict = {
        'input_ids': all_input_ids,
        'input_mask': all_input_mask,
        'segment_ids': all_segment_ids,
        'label_ids': all_label_ids
    }
    predict_fn = predictor.from_saved_model('../testing/1589418540')
    result = predict_fn(pred_dict)
    print(result)
pred_sentences = [
  "That movie was absolutely awful",
  "The acting was a bit lacking",
  "The film was creative and surprising",
  "Absolutely fantastic!",
]
predict(pred_sentences)
{'probabilities': array([[-0.3579178 , -1.2010787 ],
       [-0.36648935, -1.1814401 ],
       [-0.30407643, -1.3386648 ],
       [-0.45970002, -0.9982413 ],
       [-0.36113673, -1.1936386 ],
       [-0.36672896, -1.1808994 ]], dtype=float32), 'labels': array([0, 0, 0, 0, 0, 0])}

However, the probabilities returned for sentences in pred_sentences do not match the probabilities I get use estimator.predict(predict_input_fn) where estimator is the fine-tuned model being used within the same (python) session. For example, [-0.27276006, -1.4324446 ] using estimator vs [-0.26713806, -1.4505868 ] using predictor.



来源:https://stackoverflow.com/questions/61310331/performing-inference-with-a-bert-tf-1-x-saved-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!