【问题标题】:TF - hub universal sentence encoder module save and reload?TF - hub 通用句子编码器模块保存和重新加载?
【发布时间】:2019-07-14 01:04:10
【问题描述】:

我正在使用以下命令使用通用句子编码器预训练模型:

import tensorflow as tf
import tensorflow_hub as hub

MODEL_NAME = 'tf-sentence-encoder'
VERSION = 1
SERVE_PATH = './models/{}/{}'.format(MODEL_NAME, VERSION)

with tf.Graph().as_default():
  module = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
  text = tf.placeholder(tf.string, [None])
  embedding = module(text)

  init_op = tf.group([tf.global_variables_initializer(),
                      tf.tables_initializer()]
                     )
  with tf.Session() as session:
      session.run(init_op)
      tf.saved_model.simple_save(session, SERVE_PATH,
                                 inputs = {"text": text}, outputs = {"embedding": embedding},
                                 legacy_init_op = tf.tables_initializer()
                                 )

如何为 RESTFUL API 重新加载保存的模型?

【问题讨论】:

  • 您已经使用 tensorflow-hub 来构建模型,但我认为您的问题通常是关于 tensorflow 的。 (哪些 RESTFUL API?可能是 tensorflow-serving?)

标签: python-3.x tensorflow tensorflow-serving tensorflow-hub


【解决方案1】:

正如 Arno 在评论中提到的,这个问题与 Tensorflow Serving 有关。

使用 tf.saved_model.simple_save 保存模型后,模型将以 .pb 格式保存。

通过使用 REST API 重新加载保存的模型,我了解使用 REST API 执行推理。解释如下:

假设模型名称为 tf_sentence_encoder 您可以使用以下命令查看模型的 SignatureDef:

!saved_model_cli show --dir tf_sentence_encoder --all

我们应该使用如下所示的 Docker Pull 命令安装 TensorFlow Serving:

sudo docker pull tensorflow/serving

然后我们必须调用 Tensorflow 模型服务器:

sudo docker run -p 8501:8501 --mount type=bind,source=/Path_Of_The_Model/tf_sentence_encoder,target=/models/tf_sentence_encoder -e MODEL_NAME=tf_sentence_encoder -t tensorflow/serving &

然后,您可以使用 REST API 执行预测,如下所示:

curl -d '{"inputs": [5.1,3.3,1.7,0.5]}' \ -X POST http://localhost:8501/v1/models/tf_sentence_encoder:predict

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多