【问题标题】:How to make features for serving_input_receiver_fn BERT Tensorflow如何为 serving_input_receiver_fn BERT Tensorflow 制作功能
【发布时间】:2019-11-12 01:07:27
【问题描述】:

我使用 Tensorflow BERT 语言模型创建了一个二元分类器。这是示例代码的link。我能够进行预测。现在我想导出这个模型。我不确定我是否正确定义了 feature_spec。

导出模型的代码。

feature_spec = {'x': tf.VarLenFeature(tf.string)}  

def serving_input_receiver_fn():  
  serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[1],name='input_example_tensor')
  receiver_tensors = {'examples': serialized_tf_example}
  features = tf.parse_example(serialized_tf_example, feature_spec)
  return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

# Export the estimator
export_path = f'/content/drive/My Drive/binary_class/bert/export'

estimator.export_saved_model(
    export_path,
    serving_input_receiver_fn=serving_input_receiver_fn)

错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-71-56ff3fb3e002> in <module>()
     16 estimator.export_saved_model(
     17     export_path,
---> 18     serving_input_receiver_fn=serving_input_receiver_fn)

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in export_saved_model(self, export_dir_base, serving_input_receiver_fn, assets_extra, as_text, checkpoint_path, experimental_mode)
    730         as_text=as_text,
    731         checkpoint_path=checkpoint_path,
--> 732         strip_default_attrs=True)
    733 
    734   def experimental_export_all_saved_models(

/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _export_all_saved_models(self, export_dir_base, input_receiver_fn_map, assets_extra, as_text, checkpoint_path, strip_default_attrs)
    854             builder, input_receiver_fn_map, checkpoint_path,
    855             save_variables, mode=ModeKeys.PREDICT,
--> 856             strip_default_attrs=strip_default_attrs)
    857         save_variables = False
    858 

/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _add_meta_graph_for_mode(self, builder, input_receiver_fn_map, checkpoint_path, save_variables, mode, export_tags, check_variables, strip_default_attrs)
    927           labels=getattr(input_receiver, 'labels', None),
    928           mode=mode,
--> 929           config=self.config)
    930 
    931       export_outputs = export_lib.export_outputs_for_mode(

/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
   1144 
   1145     logging.info('Calling model_fn.')
-> 1146     model_fn_results = self._model_fn(features=features, **kwargs)
   1147     logging.info('Done calling model_fn.')
   1148 

<ipython-input-17-119a3167bf33> in model_fn(features, labels, mode, params)
      5     """The `model_fn` for TPUEstimator."""
      6 
----> 7     input_ids = features["input_ids"]
      8     input_mask = features["input_mask"]
      9     segment_ids = features["segment_ids"]

KeyError: 'input_ids'

【问题讨论】:

    标签: python tensorflow nlp feature-extraction text-classification


    【解决方案1】:

    notebook 中的 create_model 函数需要一些参数。这些是将传递给模型的特征。

    通过将 serving_input_fn 函数更新为跟随,服务函数可以正常工作。

    更新代码

    def serving_input_fn():
      feature_spec = {
          "input_ids" : tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
          "input_mask" : tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
          "segment_ids" : tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
          "label_ids" :  tf.FixedLenFeature([], tf.int64)
    
      }
      serialized_tf_example = tf.placeholder(dtype=tf.string, 
                                             shape=[None],
                                             name='input_example_tensor')
      receiver_tensors = {'example': serialized_tf_example}
      features = tf.parse_example(serialized_tf_example, feature_spec)
      return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2013-02-19
      相关资源
      最近更新 更多