gpt4 book ai didi

python - 如何使用saved_model_cli查询tensorflow 'wide and deep'模型?

转载 作者:行者123 更新时间:2023-11-30 22:30:01 45 4
gpt4 key购买 nike

遵循 https://github.com/MtDersvan/tf_playground/blob/master/wide_and_deep_tutorial/wide_and_deep_basic_serving.md 上的 SavedModel“广泛而深入”教程后,我似乎无法使用Tensorflow自带的saved_model_cli查询SavedModel。

这是我已经尝试过的示例:

saved_model_cli run --dir /tmp/serving_savemodel/1504908631 --tag_set serve --signature_def serving_default --input_exprs 'age=25;capital_gain=0;capital_loss=0;education=11th;education_num=7;gender=Male;hours_per_week=40;native_country=United-States;occupation=Machine-op-inspct;relationship=Own-child;workclass=Private'

最佳答案

MtDersvan answered他的问题Github repo ,这对我来说是成功的(避免由于格式错误而引用):

因为输入是序列化的 tf.Example,您需要将 feature_dict 转换为序列化的原始张量,如下所示:

import tensorflow as tf

def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

feature_dict = {'age': _float_feature(value=25),
'capital_gain': _float_feature(value=0),
'capital_loss': _float_feature(value=0),
'education': _bytes_feature(value='11th'.encode()),
'education_num': _float_feature(value=7),
'gender': _bytes_feature(value='Male'.encode()),
'hours_per_week': _float_feature(value=40),
'native_country': _bytes_feature(value='United-States'.encode()),
'occupation': _bytes_feature(value='Machine-op-inspct'.encode()),
'relationship': _bytes_feature(value='Own-child'.encode()),
'workclass': _bytes_feature(value='Private'.encode())}

example = tf.train.Example(features=tf.train.Features(feature=feature_dict))

serialized = example.SerializeToString()

serialized_proto = tf.contrib.util.make_tensor_proto(serialized, shape=[1])

serialized_proto_handle = serialized_proto.string_val

这个serialized_proto_handle应该看起来像这样:

[b'\n\xaa\x02\n\x1a\n\x0ehours_per_week\x12\x08\x12\x06\n\x04\x00\x00 B\n\x1d\n\x0crelationship\x12\r\n\x0b\n\tOwn-child\n\x19\n\reducation_num\x12\x08\x12\x06\n\x04\x00\x00\xe0@\n#\n\noccupation\x12\x15\n\x13\n\x11Machine-op-inspct\n\x18\n\x0ccapital_gain\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x18\n\x0ccapital_loss\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x15\n\teducation\x12\x08\n\x06\n\x0411th\n\x0f\n\x03age\x12\x08\x12\x06\n\x04\x00\x00\xc8A\n#\n\x0enative_country\x12\x11\n\x0f\n\rUnited-States\n\x18\n\tworkclass\x12\x0b\n\t\n\x07Private\n\x12\n\x06gender\x12\x08\n\x06\n\x04Male']

所以这可以单独完成,例如在 ipython 或 python 脚本中。接下来,您可以将其转储到文件中(pickle、.npy、.npz)或直接从控制台将其启动:

saved_model_cli run --dir ./wide_and_deep_tutorial/serving_savemodel/${MODEL_DIR} \
--tag_set serve --signature_def serving_default \
--input_exprs "inputs=[b'\n\xaa\x02\n\x1a\n\x0ehours_per_week\x12\x08\x12\x06\n\x04\x00\x00 B\n\x1d\n\x0crelationship\x12\r\n\x0b\n\tOwn-child\n\x19\n\reducation_num\x12\x08\x12\x06\n\x04\x00\x00\xe0@\n#\n\noccupation\x12\x15\n\x13\n\x11Machine-op-inspct\n\x18\n\x0ccapital_gain\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x18\n\x0ccapital_loss\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x15\n\teducation\x12\x08\n\x06\n\x0411th\n\x0f\n\x03age\x12\x08\x12\x06\n\x04\x00\x00\xc8A\n#\n\x0enative_country\x12\x11\n\x0f\n\rUnited-States\n\x18\n\tworkclass\x12\x0b\n\t\n\x07Private\n\x12\n\x06gender\x12\x08\n\x06\n\x04Male']"

Result for output key classes:
[[b'0' b'1']]
Result for output key scores:
[[ 0.85787761 0.14212234]]

关于python - 如何使用saved_model_cli查询tensorflow 'wide and deep'模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186746/

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