Python 如何使用tensorflow为BERT SQuAD2.0建立输入预测保存模型

Python 如何使用tensorflow为BERT SQuAD2.0建立输入预测保存模型,python,tensorflow,deep-learning,nlp,question-answering,Python,Tensorflow,Deep Learning,Nlp,Question Answering,考虑到我已经获得了输入的所有元素,我正试图从BERT团队中为保存的模型构建输入 我通过在中运行run_stand.py对一个问答模型进行了微调,然后使用export_saved_model导出了该模型。现在,当我有一个新的上下文和问题时,我无法构建能够从模型中获得返回输出的正确输入 导出模型的代码: #导出模型 def服务_输入_接收器_fn(): 特征规格={ “唯一标识”:tf.FixedLenFeature([],tf.int64), “输入ID”:tf.FixedLenFeature([

考虑到我已经获得了输入的所有元素,我正试图从BERT团队中为保存的模型构建输入

我通过在中运行run_stand.py对一个问答模型进行了微调,然后使用export_saved_model导出了该模型。现在,当我有一个新的上下文和问题时,我无法构建能够从模型中获得返回输出的正确输入

导出模型的代码:

#导出模型
def服务_输入_接收器_fn():
特征规格={
“唯一标识”:tf.FixedLenFeature([],tf.int64),
“输入ID”:tf.FixedLenFeature([FLAGS.max\u seq\u length],tf.int64),
“输入掩码”:tf.FixedLenFeature([FLAGS.max\u seq\u length],tf.int64),
“段ID”:tf.FixedLenFeature([FLAGS.max\u seq\u length],tf.int64),
}
序列化的\u tf\u示例=tf.placeholder(dtype=tf.string,
形状=标志。预测批次大小,
name='input\u示例\u tensor')
接收器\u张量={'examples':序列化的\u tf\u example}
features=tf.parse_示例(序列化的_-tf_示例,feature_-spec)
返回tf.estimator.export.ServingInputReceiver(特征、接收器张量)
估计器=tf.contrib.tpu.tpu估计器(
使用\u tpu=标志。使用\u tpu,
型号\ fn=型号\ fn,
config=run\u config,
列车批量大小=标志。列车批量大小,
预测批次大小=标志。预测批次大小)
估算器。_导出到_tpu=假35;##!!重要的是要补充这一点
estimator.export\u保存的\u模型(
export_dir_base=“C:/Users/ZitongZhou/Desktop/qa/bert_-team/servemodel”,
服务\输入\接收器\ fn=服务\输入\接收器\ fn)
我加载模型的方式:

export\u dir='servemodel'
subdirs=[x代表路径中的x(export_dir).iterdir()
如果x.为_dir()且“temp”不在str(x)中]
latest=str(已排序(细分)[-1])
predict\u fn=预测器。来自保存的\u模型(最新)
我从run_squad.py获得了评估功能。 我尝试构建输入的方式:

feature\u spec={
“唯一标识”:np.asarray(eval\u features[0]。唯一标识)。tolist(),
“输入标识”:np.asarray(eval\u features[0]。输入标识)。tolist(),
“输入掩码”:np.asarray(评估特性[0]。输入掩码)。tolist(),
“段ID”:np.asarray(eval\u features[0]。段ID.tolist()
}
序列化的\u tf\u示例=tf.placeholder(dtype=tf.string,
形状=[1],
name='input\u示例\u tensor')
接收器\u张量={'examples':序列化的\u tf\u example}
features=tf.parse_示例(序列化的_-tf_示例,feature_-spec)
out=predict_fn({'examples':[str(特征规格)])
我希望得到一个预测,这样我就可以从中提取问题的答案

我得到的回溯:

回溯(最近一次呼叫最后一次):
文件“”,第11行,在
features=tf.parse_示例(序列化的_-tf_示例,feature_-spec)
文件“C:\Users\ZitongZhou\Anaconda3\envs\nlp\lib\site packages\tensorflow
\python\ops\parsing_ops.py”,第580行,在parse_示例中
返回parse_example_v2(序列化、功能、示例名称、名称)
文件“C:\Users\ZitongZhou\Anaconda3\envs\nlp\lib\site packages\tensorflow
\python\ops\parsing_ops.py”,第803行,在parse_示例_v2中
[VarLenFeature、SparseFeature、FixedLenFeature、FixedLenSequenceFeature])
文件“C:\Users\ZitongZhou\Anaconda3\envs\nlp\lib\site packages\tensorflow
\python\ops\parsing_ops.py”,第299行,在_features_到_raw_参数中
raise VALUERROR(“无效的功能%s:%s.%(键,功能))
ValueError:无效的功能输入\u ID:[101100521292214200319523,
6562, 1005, 102, 1005, 19523, 11233, 2003, 2274, 2086, 2214, 1005, 102,
0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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tf.train.Int64List(value=list(value)))
返回f
输入=集合。OrderedDict()
输入[“输入标识”]=创建输入标识要素(要素[0]。输入标识)
输入[“输入掩码”]=创建输入掩码功能(功能[0]。输入掩码)
输入[“段ID”]=创建段内要素(要素[0]。段ID)
输入[“唯一的\u id”]=创建\u int\u功能([features[0]。唯一的\u id])
tf_示例=tf.train.example(特征=tf.train.features(特征=输入))
out=predict_fn({'examples':[tf_example.SerializeToString()]})