Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serving Hub Module with Django #54

Closed
marcoaleixo opened this issue May 20, 2018 · 4 comments
Closed

Serving Hub Module with Django #54

marcoaleixo opened this issue May 20, 2018 · 4 comments

Comments

@marcoaleixo
Copy link

marcoaleixo commented May 20, 2018

I'm trying to deploy a application using Django + Text Module.
I have a view with the logic using the module, but I need to keep the module on memory, to avoid the time the module need to load in every request.

When I try to re-use my session I receive:

File "/home/venv/local/lib/python2.7/site-packages/tensorflow_hub/module.py", line 191, in call
"Module must be applied in the graph it was instantiated for.")
RuntimeError: Module must be applied in the graph it was instantiated for.

Any way to solve this problem?

@vbardiovskyg
Copy link
Contributor

Hi Marco,

you could try something like this:

First initialize the graph.

g = tf.Graph()
with g.as_default():
  text_input = tf.placeholder(dtype=tf.string, shape=[None])
  module = hub.Module(text_input)
  # other graph building
  my_result = ...
g.finalize() # not mandatory, but a nice practice, especially in multithreaded environment

Then initialize the session.

session = tf.Session(graph=g)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())

I don't know what Django allows but these would be persisted across requests.

For inference you can just then call

my_result_out = session.run(my_result, feed_dict={text_input, ["Hello world"]})

@marcoaleixo
Copy link
Author

Worked, thank you.

@bsat007
Copy link

bsat007 commented Jun 14, 2018

I am very new to Tensorflow, I am trying to use text module.

module_path = "https://tfhub.dev/google/universal-sentence-encoder/2"
g = tf.Graph()
with g.as_default():
    similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
    embed = hub.Module(module_path, trainable=True)
    encoding_tensor = embed(similarity_input_placeholder)
g.finalize()
sess = tf.Session(graph=g)
sess.run([tf.global_variables_initializer(), tf.tables_initializer()])

Getting this error:

ValueError: Fetch argument <tf.Operation 'init_5' type=NoOp> cannot be interpreted as a Tensor. (Operation name: "init_5"
op: "NoOp"
input: "^Assign_32"
input: "^Assign"
input: "^Assign_1"
input: "^Assign_9"
input: "^Assign_10"
input: "^Assign_11"
input: "^Assign_12"
input: "^Assign_13"
input: "^Assign_14"
input: "^Assign_15"
input: "^Assign_16"
input: "^Assign_2"
input: "^Assign_3"
input: "^Assign_4"
input: "^Assign_5"
input: "^Assign_6"
input: "^Assign_7"
input: "^Assign_8"
input: "^Assign_17"
input: "^Assign_18"
input: "^Assign_19"
input: "^Assign_21"
input: "^Assign_20"
input: "^Assign_23"
input: "^Assign_22"
input: "^Assign_25"
input: "^Assign_24"
input: "^Assign_27"
input: "^Assign_26"
input: "^Assign_31"
input: "^Assign_30"
input: "^Assign_29"
input: "^Assign_28"
input: "^Assign_65"
input: "^Assign_33"
input: "^Assign_34"
input: "^Assign_42"
input: "^Assign_43"
input: "^Assign_44"
input: "^Assign_45"
input: "^Assign_46"
input: "^Assign_47"
input: "^Assign_48"
input: "^Assign_49"
input: "^Assign_35"
input: "^Assign_36"
input: "^Assign_37"
input: "^Assign_38"
input: "^Assign_39"
input: "^Assign_40"
input: "^Assign_41"
input: "^Assign_50"
input: "^Assign_51"
input: "^Assign_52"
input: "^Assign_54"
input: "^Assign_53"
input: "^Assign_56"
input: "^Assign_55"
input: "^Assign_58"
input: "^Assign_57"
input: "^Assign_60"
input: "^Assign_59"
input: "^Assign_64"
input: "^Assign_63"
input: "^Assign_62"
input: "^Assign_61"
 is not an element of this graph.)

What am I doing wrong?

@andresusanopinto
Copy link
Contributor

For the record - question from @bsat007 to be answered on #80.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants