tensorflow 1 Session.run 花费太多时间使用通用句子编码器嵌入句子 [英] tensorflow 1 Session.run is taking too much time to embed sentence using universal sentence encoder

查看:28
本文介绍了tensorflow 1 Session.run 花费太多时间使用通用句子编码器嵌入句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 tensforflow 与 Flask REST API 结合使用

我应该如何减少 session.run

我在 REST API 中使用 tf 1/2,而不是在我的服务器上使用它.

I am using tf 1/2 in REST API, instead of serving it i am using it on my server.

我已经尝试过 tensorflow 1 和 2.

i have tried tensorflow 1 and 2.

tensorflow 1 花费了太多时间.

tensorflow 1 is taking too much time.

tensorflow 2 甚至没有返回文本的向量.

tensorflow 2 is not even returning the vectors for text.

在 tensorflow 1初始化需要 2-4 秒,session.run 需要 5-8 秒.随着我不断满足请求,时间越来越长.

in tensorflow 1 initialising is taking 2-4 seconds and session.run is taking 5-8 seconds. and time is getting increased as i keep hitting the requests.

张量流 1

import tensorflow.compat.v1 as tfo
import tensorflow_hub as hub
tfo.disable_eager_execution()

module_url = "https://tfhub.dev/google/universal-sentence-encoder-qa/3"
# Import the Universal Sentence Encoder's TF Hub module
embed = hub.Module(module_url)

def convert_text_to_vector(text):
    # Compute a representation for each message, showing various lengths supported.
    try:
        #text = "qwerty" or ["qwerty"]
        if isinstance(text, str):
            text = [text]
        with tfo.Session() as session:
            t_time = time.time()
            session.run([tfo.global_variables_initializer(), tfo.tables_initializer()])
            m_time = time.time()
            message_embeddings = session.run(embed(text))
            vector_array = message_embeddings.tolist()[0]
        return vector_array
    except Exception as err:
        raise Exception(str(err))

张量流 2

它被困在 vector_array = embedding_fn(text)

import tensorflow as tf
import tensorflow_hub as hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder-qa/3"
embedding_fn = hub.load(module_url)

@tf.function
def convert_text_to_vector(text):
    try:
        #text = ["qwerty"]
        vector_array = embedding_fn(text)
        return vector_array
    except Exception as err:
        raise Exception(str(err))

推荐答案

from flask import Flask
from flask_restplus import Api, Resource
from werkzeug.utils import cached_property

import tensorflow as tf
import tensorflow_hub as hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder-qa/3"
embedding_fn = hub.load(module_url)


app = Flask(__name__)

@app.route('/embedding', methods=['POST'])
def entry_point(args):
    if args.get("text"):
        text_term = args.get("text")
        if isinstance(text_term, str):
            text_term = [text_term]
        vectors = convert_text_to_vector(text_term)
    return vectors



@tf.function
def convert_text_to_vector(text):
    try:
        vector_array = embedding_fn.signatures['question_encoder'](tf.constant(text))
        return vector_array['outputs']
    except Exception as err:
        raise Exception(str(err))


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

"""
 ----- Requirements.txt ----
flask-restplus==0.13.0
Flask==1.1.1
Werkzeug==0.15.5
tensorboard==2.2.2
tensorboard-plugin-wit==1.6.0.post3
tensorflow==2.2.0
tensorflow-estimator==2.2.0
tensorflow-hub==0.8.0
tensorflow-text==2.2.1
"""

这篇关于tensorflow 1 Session.run 花费太多时间使用通用句子编码器嵌入句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆