Tensorflow: Unable to feed a string through to placeholder tensor
Tensorflow: Unable to feed a string through to placeholder tensor
I'm writing a function to compare the similarity of two strings, using Google's universal sentence encoder. Following the instructions in the notebook provided here I have the following method in my class that takes two sentences as input and prints the similarity between them.
def tf_sim(self, text1, text2):
# Reduce logging output.
tf.logging.set_verbosity(tf.logging.ERROR)
sim_input1 = tf.placeholder(tf.string, shape=(None), name="sim_input1")
sim_input2 = tf.placeholder(tf.string, shape=(None), name="sim_input2")
embedding1 = self.embed([sim_input1])
embedding2 = self.embed([sim_input2])
encode1 = tf.nn.l2_normalize(embedding1, axis=1)
encode2 = tf.nn.l2_normalize(embedding2, axis=1)
sim_scores = -tf.acos(tf.reduce_sum(tf.multiply(encode1, encode2), axis=1))
init_vars = tf.global_variables_initializer()
init_tables = tf.tables_initializer()
with tf.Session() as sess:
sess.run([init_vars, init_tables])
sess.run(sim_scores, feed_dict={sim_input1: text1, sim_input2: text2})
print (sim_scores.eval())
For reference the self.embed attribute looks like this:
self.embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
When I call the function:
str1 = "Is Python a better first language to learn than Java"
str2 = "Which of Python or Java is a better programming language to
start out with"
tf_sim(str1, str2)
I get the below stack trace:
Traceback (most recent call last):
File "SimilarityCalculator.py", line 143, in <module>
sc.tf_sim(str1, str2)
File "SimilarityCalculator.py", line 67, in tf_sim
print (sim_scores.eval())
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 710, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5180, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 900, in run
run_metadata_ptr)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1135, in _run
feed_dict_tensor, options, run_metadata)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1316, in _do_run
run_metadata)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1335, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'sim_input1' with dtype string
[[Node: sim_input1 = Placeholder[dtype=DT_STRING, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'sim_input1', defined at:
File "SimilarityCalculator.py", line 143, in <module>
sc.tf_sim(str1, str2)
File "SimilarityCalculator.py", line 40, in tf_sim
sim_input1 = tf.placeholder(tf.string, shape=(None), name="sim_input1")
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1808, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 4848, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1718, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'sim_input1' with dtype string
[[Node: sim_input1 = Placeholder[dtype=DT_STRING, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Basically telling me I need to feed a string to the placeholder even though it looks like I'm doing just that. Is there anything I'm missing? Any help is greatly appreciated!
2 Answers
2
sim_scores.eval()
is equivalent to doing:
sess.run(sim_scores, feed_dict={})
so tensorflow is (rightfully) complaining that you didn't feed sim_input1
(or sim_input2
)
sim_input1
sim_input2
Instead store the result of the sess.run()
call and print that, or give the feed_dict argument to the eval()
call.
sess.run()
eval()
sess.run
sim_scores.eval()
As f4
said, you already evaluated the value of sim_scores
. You just have to store and print it:
f4
sim_scores
with tf.Session() as sess:
sess.run([init_vars, init_tables])
evaluated_sim_scores = sess.run(sim_scores, feed_dict={sim_input1: text1, sim_input2: text2})
print (evaluated_sim_scores)
Note: Do not use the same name to store sim_scores
.
sim_scores
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you, for some reason I kept thinking the error was coming from one of the previous lines where I called
sess.run
, instead of fromsim_scores.eval()
. Makes sense now.– Kaushik
Jun 28 at 17:16