Hub: Serving Hub Module with Django

Created on 20 May 2018  路  4Comments  路  Source: tensorflow/hub

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?

Most helpful comment

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

All 4 comments

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

Worked, thank you.

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?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arnoegw picture arnoegw  路  5Comments

bzburr picture bzburr  路  4Comments

bhack picture bhack  路  3Comments

truas picture truas  路  4Comments

martiansideofthemoon picture martiansideofthemoon  路  3Comments