【问题标题】:Performing inference with a BERT (TF 1.x) saved model使用 BERT (TF 1.x) 保存的模型执行推理
【发布时间】:2020-04-19 19:24:32
【问题描述】:

我被困在一行代码上,结果整个周末都在一个项目上停滞不前。

我正在做一个使用 BERT 进行句子分类的项目。我已经成功训练了模型,我可以使用 run_classifier.py 中的示例代码来测试结果。

我可以使用这个示例代码导出模型(已经多次转发,所以我相信它适合这个模型):

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)

我还可以加载导出的估计器(导出函数将导出的模型保存到标有时间戳的子目录中):

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

但是,在我的一生中,我无法弄清楚要为 predict_fn 提供什么作为推理的输入。这是我目前最好的代码:

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)

我为 predict_fn 提供什么似乎并不重要:示例数组、特征数组、predict_input_fn 函数。显然,predict_fn 需要某种类型的字典 - 但我尝试过的每一件事都会由于张量不匹配或其他通常意味着输入错误的错误而产生异常。

我假设 from_saved_model 函数需要与模型测试函数相同类型的输入 - 显然,事实并非如此。

似乎很多 人都问过这个问题 - “我如何使用导出的 BERT TensorFlow 模型进行推理?” - 并没有得到任何答案:

Thread #1

Thread #2

Thread #3

Thread #4

有什么帮助吗?提前致谢。

【问题讨论】:

    标签: tensorflow tensorflow-serving tensorflow-estimator


    【解决方案1】:

    感谢您的这篇文章。你的serving_input_fn 是我错过的部分!您的predict 函数需要更改以直接提供特征字典,而不是使用 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])}
    

    但是,pred_sentences 中的句子返回的概率与我得到的使用 estimator.predict(predict_input_fn) 的概率不匹配,其中estimator 是在同一 (python) 会话中使用的微调模型。例如,[-0.27276006, -1.4324446 ] 使用 estimator 与 [-0.26713806, -1.4505868 ] 使用 predictor

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多