本文介绍了Tensorflow:模块必须在为其实例化的图中应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Django提供通用句子编码器.

I'm trying to serve universal sentence encoder with Django.

该代码最初是作为后台进程初始化的(通过使用诸如Supervisor之类的程序),然后它使用TCP套接字与Django通信,并最终返回编码后的句子.

The code is initialized in the beginning as a background process (by using programs such as supervisor), then it communicates with Django using TCP sockets and eventually returns encoded sentence.

import socket
from threading import Thread
import tensorflow as tf
import tensorflow_hub as hub
import atexit

# Pre-loading the variables:
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
session = tf.Session()
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
atexit.register(session.close)  # session closes if the script is halted
...
# Converts string to vector embedding:
def initiate_connection(conn):
    data = conn.recv(1024)
    conn.send(session.run(embed([data])))
    conn.close()

# Process in background, waiting for TCP message from views.py
while True:
    conn, addr = _socket.accept()
    _thread = Thread(target=initiate_connection, args=(conn,))  # new thread for each request (could be limited to n threads later)
    _thread.demon = True
    _thread.start()
    conn.close()

但是执行conn.send(session.run(embed([data])))时收到以下错误:

But I receive the following error when executing conn.send(session.run(embed([data]))):


我基本上是试图在tensorflow中预加载表(因为这会花费很多时间),但是tensorflow不允许我使用预定义的会话.


I'm basically trying to pre-load table in tensorflow (because it takes quite a lot of time), but tensorflow doesn't let me use the session that was pre-defined.

我该如何解决?有什么办法可以预加载这些变量吗?

How can I fix this? Is there any way to pre-load these variables?

P.S我相信此Github问题页面可能可以解决我的问题,但我没有确定如何实施.

P.SI believe this Github issue page might have solution for my problem, but I'm not sure how could it be implemented.

推荐答案

使用您创建的图形加载模型并在会话中使用它.

Load your model with the graph that you created and use that in your session.

graph = tf.Graph()
with tf.Session(graph = graph) as session:
     embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")

并在带有会话的initialize_connection函数中使用相同的图形对象

And use the same graph object in initiate_connection function with session

def initiate_connection(conn):
    data = conn.recv(1024)
    with tf.Session(graph = graph) as session:
        session.run([tf.global_variables_initializer(), tf.tables_initializer()])
        conn.send(session.run(embed([data])))
    conn.close()

这篇关于Tensorflow:模块必须在为其实例化的图中应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:49