tensorflow - TensorFlow:如何使用 TensorHub 模块导出估算器?
问题描述
我有一个使用 TensorHub text_embedding 列的估计器,如下所示:
my_dataframe = pandas.DataFrame(columns=["title"})
# populate data
labels = []
# populate labels with 0|1
embedded_text_feature_column = hub.text_embedding_column(
key="title"
,module_spec="https://tfhub.dev/google/nnlm-en-dim128-with-normalization/1")
estimator = tf.estimator.LinearClassifier(
feature_columns = [ embedded_text_feature_column ]
,optimizer=tf.train.FtrlOptimizer(
learning_rate=0.1
,l1_regularization_strength=1.0
)
,model_dir=model_dir
)
estimator.train(
input_fn=tf.estimator.inputs.pandas_input_fn(
x=my_dataframe
,y=labels
,batch_size=128
,num_epochs=None
,shuffle=True
,num_threads=5
)
,steps=5000
)
export(estimator, "/tmp/my_model")
如何导出和服务模型,以便它接受字符串作为预测的输入?我有一个serving_input_receiver_fn
如下,并尝试了很多,但我很困惑它需要看起来像什么,以便我可以提供它(例如使用saved_model_cli)并用标题字符串(或简单的JSON)调用它结构)作为输入。
def export(estimator, dir_path):
def serving_input_receiver_fn():
feature_spec = tf.feature_column.make_parse_example_spec(hub.text_embedding_column(
key="title"
,module_spec="https://tfhub.dev/google/nnlm-en-dim128-with-normalization/1"))
return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
estimator.export_savedmodel(
export_dir_base=dir_path
,serving_input_receiver_fn=serving_input_receiver_fn()
)
解决方案
如果您想提供原始字符串,您可能需要考虑使用原始输入接收器。这段代码:
feature_placeholder = {'title': tf.placeholder('string', [1], name='title_placeholder')}
serving_input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(feature_placeholder)
estimator.export_savedmodel(dir_path, serving_input_fn)
SavedModel
根据SavedModel
CLI ,将为您提供以下输入规范:
saved_model_cli show --dir ./ --tag_set serve --signature_def serving_default
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: title_placeholder_1:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 2)
name: linear/head/Tile:0
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 2)
name: linear/head/predictions/probabilities:0
您可以向 CLI 提供一个 python 表达式来为模型提供输入以验证它是否有效:
saved_model_cli run --dir ./ --tag_set serve --signature_def \
serving_default --input_exprs "inputs=['this is a test sentence']"
Result for output key classes:
[[b'0' b'1']]
Result for output key scores:
[[0.5123377 0.4876624]]
推荐阅读
- python - 在 Python 中安排一个简单的通知脚本每小时运行一次
- apache-spark - spark任务中如何查看executor的gc信息?
- flutter - 在为 ios 构建期间颤动“找不到文件”
- mysql - 需要帮助对可用的订单明细表进行分组以得出 count(order_line_item) 、 Qty 和 count(Qty) 的分布
- tensorflow - 如何关闭 tensorflow contrib 模块警告
- javascript - 开发服务器返回错误代码:500(React Native)
- php - 在 html 模板中查找 php.ini 文件
- java - Corda bootcamp-cordapp 抛出 java.security.NoSuchAlgorithmException:找不到为 KeyPairGenerator(提供者:BC)配置的类
- python - 'NoneType' 对象在 Python 中不是可迭代的错误
- chart.js - 我们可以在图表的轴上显示长数字为 1k、1M、1B 吗?