Tensorflow: Module must be applied in the graph it was instantiated for

会有一股神秘感。 提交于 2019-12-11 04:14:52

问题


I'm trying to serve universal sentence encoder with 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()

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

RuntimeError: Module must be applied in the graph it was instantiated for.


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 I believe this Github issue page might have solution for my problem, but I'm not sure how could it be implemented.


回答1:


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")

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()


来源:https://stackoverflow.com/questions/52392407/tensorflow-module-must-be-applied-in-the-graph-it-was-instantiated-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!