gpt4 book ai didi

tensorflow - 您必须使用 dtype 字符串和形状 [1] 为占位符张量 'input_example_tensor' 提供一个值

转载 作者:行者123 更新时间:2023-12-04 13:48:05 25 4
gpt4 key购买 nike

我正在使用 chatbot-retrieval 开发一个 tensorflow 服务客户端/服务器应用程序项目。

我的代码有两部分,即服务部分和客户端部分。

下面是服务部分的代码片段。

def get_features(context, utterance):

context_len = 50
utterance_len = 50

features = {
"context": context,
"context_len": tf.constant(context_len, shape=[1,1], dtype=tf.int64),
"utterance": utterance,
"utterance_len": tf.constant(utterance_len, shape=[1,1], dtype=tf.int64),
}

return features


def my_input_fn(estimator, input_example_tensor ):
feature_configs = {
'context':tf.FixedLenFeature(shape=[50], dtype=tf.int64),
'utterance':tf.FixedLenFeature(shape=[50], dtype=tf.int64)
}
tf_example = tf.parse_example(input_example_tensor, feature_configs)
context = tf.identity(tf_example['context'], name='context')
utterance = tf.identity(tf_example['utterance'], name='utterance')
features = get_features(context, utterance)
return features

def my_signature_fn(input_example_tensor, features, predictions):
feature_configs = {
'context':tf.FixedLenFeature(shape=[50], dtype=tf.int64),
'utterance':tf.FixedLenFeature(shape=[50], dtype=tf.int64)
}

tf_example = tf.parse_example(input_example_tensor, feature_configs)
tf_context = tf.identity(tf_example['context'], name='tf_context_utterance')
tf_utterance = tf.identity(tf_example['utterance'], name='tf_utterance')

default_graph_signature = exporter.regression_signature(
input_tensor=input_example_tensor,
output_tensor=tf.identity(predictions)
)

named_graph_signatures = {
'inputs':exporter.generic_signature(
{
'context':tf_context,
'utterance':tf_utterance
}
),
'outputs':exporter.generic_signature(
{
'scores':predictions
}
)
}

return default_graph_signature, named_graph_signatures

def main():
##preliminary codes here##

estimator.fit(input_fn=input_fn_train, steps=100, monitors=[eval_monitor])

estimator.export(
export_dir = FLAGS.export_dir,
input_fn = my_input_fn,
use_deprecated_input_fn = True,
signature_fn = my_signature_fn,
exports_to_keep = 1
)

下面是客户端部分的代码片段。
def tokenizer_fn(iterator):
return (x.split(" ") for x in iterator)

vp = tf.contrib.learn.preprocessing.VocabularyProcessor.restore(FLAGS.vocab_processor_file)

input_context = "biz banka kart farkli bir banka atmsinde para"
input_utterance = "farkli banka kart biz banka atmsinde para"

context_feature = np.array(list(vp.transform([input_context])))
utterance_feature = np.array(list(vp.transform([input_utterance])))

context_tensor = tf.contrib.util.make_tensor_proto(context_feature, shape=[1, context_feature.size])
utterance_tensor = tf.contrib.util.make_tensor_proto(context_feature, shape=[1, context_feature.size])

request.inputs['context'].CopyFrom(context_tensor)
request.inputs['utterance'].CopyFrom(utterance_tensor)

result_counter.throttle()
result_future = stub.Predict.future(request, 5.0) # 5 seconds
result_future.add_done_callback(
_create_rpc_callback(label[0], result_counter))
return result_counter.get_error_rate()

服务和客户端部分都没有错误地构建。在运行服务应用程序和客户端应用程序后,当 rpc 调用完成时,我收到以下奇怪的错误传播到客户端应用程序。

下面是我在 rpc 调用完成时得到的错误
AbortionError(code=StatusCode.INVALID_ARGUMENT, details="You must feed a value for placeholder tensor 'input_example_tensor' with dtype string and shape [1]
[[Node: input_example_tensor = Placeholder[_output_shapes=[[1]], dtype=DT_STRING, shape=[1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]")

该错误很奇怪,因为似乎无法从客户端应用程序提供占位符。

如果我通过 tensorflow 服务访问模型,如何为占位符“input_example_tensor”提供数据?

答案:
(我在这里发布了我的答案,因为由于缺乏 StackOverflow 徽章,我无法将其发布为答案。任何自愿提交它作为他/她对问题的回答的人都非常受欢迎。我会批准它作为答案.)

我可以通过在 estimator.export 函数中使用选项 use_deprecated_input_fn = False 并相应地更改输入签名来解决问题。

下面是运行没有问题的最终代码。
def get_features(input_example_tensor, context, utterance):
context_len = 50
utterance_len = 50
features = {
"my_input_example_tensor": input_example_tensor,
"context": context,
"context_len": tf.constant(context_len, shape=[1,1], dtype=tf.int64),
"utterance": utterance,
"utterance_len": tf.constant(utterance_len, shape=[1,1], dtype=tf.int64),
}

return features

def my_input_fn():
input_example_tensor = tf.placeholder(tf.string, name='tf_example_placeholder')

feature_configs = {
'context':tf.FixedLenFeature(shape=[50], dtype=tf.int64),
'utterance':tf.FixedLenFeature(shape=[50], dtype=tf.int64)
}
tf_example = tf.parse_example(input_example_tensor, feature_configs)
context = tf.identity(tf_example['context'], name='context')
utterance = tf.identity(tf_example['utterance'], name='utterance')
features = get_features(input_example_tensor, context, utterance)

return features, None

def my_signature_fn(input_example_tensor, features, predictions):
default_graph_signature = exporter.regression_signature(
input_tensor=input_example_tensor,
output_tensor=predictions
)

named_graph_signatures = {
'inputs':exporter.generic_signature(
{
'context':features['context'],
'utterance':features['utterance']
}
),
'outputs':exporter.generic_signature(
{
'scores':predictions
}
)
}

return default_graph_signature, named_graph_signatures

def main():
##preliminary codes here##

estimator.fit(input_fn=input_fn_train, steps=100, monitors=[eval_monitor])

estimator._targets_info = tf.contrib.learn.estimators.tensor_signature.TensorSignature(tf.constant(0, shape=[1,1]))

estimator.export(
export_dir = FLAGS.export_dir,
input_fn = my_input_fn,
input_feature_key ="my_input_example_tensor",
use_deprecated_input_fn = False,
signature_fn = my_signature_fn,
exports_to_keep = 1
)

最佳答案

OP自行解决但无法自行回答,所以这是他们的回答:

问题已通过使用选项 use_deprecated_input_fn = False 修复在 estimator.export函数并相应地更改输入签名:

def my_signature_fn(input_example_tensor, features, predictions):   
default_graph_signature = exporter.regression_signature(
input_tensor=input_example_tensor,
output_tensor=predictions
)

named_graph_signatures = {
'inputs':exporter.generic_signature(
{
'context':features['context'],
'utterance':features['utterance']
}
),
'outputs':exporter.generic_signature(
{
'scores':predictions
}
)
}

return default_graph_signature, named_graph_signatures

def main():
##preliminary codes here##

estimator.fit(input_fn=input_fn_train, steps=100, monitors=[eval_monitor])

estimator._targets_info = tf.contrib.learn.estimators.tensor_signature.TensorSignature(tf.constant(0, shape=[1,1]))

estimator.export(
export_dir = FLAGS.export_dir,
input_fn = my_input_fn,
input_feature_key ="my_input_example_tensor",
use_deprecated_input_fn = False,
signature_fn = my_signature_fn,
exports_to_keep = 1
)

关于tensorflow - 您必须使用 dtype 字符串和形状 [1] 为占位符张量 'input_example_tensor' 提供一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com