tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.49k stars 1.67k forks source link

Serving tf-hub #80

Closed bsat007 closed 6 years ago

bsat007 commented 6 years ago

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 commented 6 years ago

Hi @bsat007,

The issue is that "[tf.global_variables_initializer(), tf.tables_initializer()]" are functions that add new ops/nodes in the current graph. But the current graph was different than the graph where sess was created.

This should work:

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)
    init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])
g.finalize()
sess = tf.Session(graph=g)
sess.run(init_op)
sess.run(encoding_tensor, feed_dict={similarity_input_placeholder:["hello earth", "hello mars"]})

But I would highly recomend you use MonitoredSession:

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)
    sess = tf.train.MonitoredSession(graph=g) # This finalizes the graph and runs initializers.

sess.run(encoding_tensor, feed_dict={similarity_input_placeholder:["hello earth", "hello mars"]})