Tensorflow:无法将字符串提供给占位符张量

Tensorflow: Unable to feed a string through to placeholder tensor

我正在编写一个函数来比较两个字符串的相似性,使用 Google 的 universal sentence encoder. Following the instructions in the notebook provided here 我的 class 中有以下方法,它将两个句子作为输入并打印它们之间的相似度。

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

作为参考,self.embed 属性如下所示:

self.embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")

当我调用函数时:

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)

我得到以下堆栈跟踪:

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

基本上是在告诉我,我需要向占位符提供一个字符串,即使看起来我正在这样做。有什么我想念的吗?非常感谢任何帮助!

sim_scores.eval()

相当于做:

sess.run(sim_scores, feed_dict={})

所以 tensorflow(理所当然地)抱怨你没有喂 sim_input1(或 sim_input2

而是存储 sess.run() 调用的结果并打印它,或者将 feed_dict 参数提供给 eval() 调用。

正如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)

注意:不要使用相同的名称存储sim_scores