Python Tensorflow:无法将字符串传递到占位符tensor

Python Tensorflow:无法将字符串传递到占位符tensor,python,tensorflow,cosine-similarity,Python,Tensorflow,Cosine Similarity,我正在编写一个函数来比较两个字符串的相似性,使用Google的。按照笔记本上的说明,我在课堂上使用了以下方法,将两个句子作为输入,并打印它们之间的相似性 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="s

我正在编写一个函数来比较两个字符串的相似性,使用Google的。按照笔记本上的说明,我在课堂上使用了以下方法,将两个句子作为输入,并打印它们之间的相似性

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"]()]]
回溯(最近一次呼叫最后一次):
文件“SimilarityCalculator.py”,第143行,在
sc.tf_sim卡(str1、str2)
tf_sim中第67行的文件“SimilarityCalculator.py”
打印(sim_scores.eval())
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/framework/ops.py”,第710行,评估
使用默认会话返回评估会话(self、feed、dict、self.graph、session)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/framework/ops.py”,第5180行,使用默认会话进行评估
返回会话。运行(张量、提要)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/client/session.py”,第900行,正在运行
运行_元数据_ptr)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/client/session.py”,第1135行,正在运行
feed_dict_tensor、options、run_元数据)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/client/session.py”,第1316行,运行
运行(元数据)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/client/session.py”,第1335行,在
提升类型(e)(节点定义、操作、消息)
tensorflow.python.framework.errors\u impl.InvalidArgumentError:必须为带有数据类型字符串的占位符tensor“sim\u input1”提供一个值
[[Node:sim_input1=Placeholder[dtype=DT_STRING,shape=,_device=“/job:localhost/replica:0/task:0/device:CPU:0”]()]
由op“sim_input1”引起,定义为:
文件“SimilarityCalculator.py”,第143行,在
sc.tf_sim卡(str1、str2)
文件“SimilarityCalculator.py”,第40行,在tf_sim中
sim\u input1=tf.placeholder(tf.string,shape=(None),name=“sim\u input1”)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/ops/array_ops.py”,第1808行,占位符
返回gen_数组操作占位符(dtype=dtype,shape=shape,name=name)
占位符中的文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/ops/gen_array_ops.py”,第4848行
“占位符”,dtype=dtype,shape=shape,name=name)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/framework/op_def_library.py”,第787行,在“应用”和“操作”助手中
op_def=op_def)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/framework/ops.py”,第3392行,在create_op中
op_def=op_def)
文件“/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site packages/tensorflow/python/framework/ops.py”,第1718行,在__
self._traceback=self._graph._extract_stack()35; pylint:disable=protected access
InvalidArgumentError(回溯请参见上文):必须使用数据类型字符串为占位符张量'sim_input1'提供一个值
[[Node:sim_input1=Placeholder[dtype=DT_STRING,shape=,_device=“/job:localhost/replica:0/task:0/device:CPU:0”]()]
基本上是告诉我需要向占位符输入一个字符串,尽管看起来我正在这样做。我有什么遗漏吗?非常感谢您的帮助

sim_scores.eval()
相当于做:

sess.run(sim_scores, feed_dict={})
因此,tensorflow(正确地)抱怨您没有提供
sim\u input1
(或
sim\u input2


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

正如
f4
所说,您已经评估了
模拟分数的值。您只需存储并打印它:

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_分数

谢谢,出于某种原因,我一直认为错误来自我调用的
sess.run
,而不是
sim_分数.eval()
。现在有道理了。