简体   繁体   English

KeyError:tf.Tensor&#39;Placeholder_6:0&#39;shape = <unknown> dtype =字符串

[英]KeyError: tf.Tensor 'Placeholder_6:0' shape=<unknown> dtype=string

Can you please explain the problem below? 您能解释下面的问题吗? Here is my code fragment in my python notebook: 这是我的python笔记本中的代码片段:

word2int = {}
int2word = {}

for i,word in enumerate(words):
    word2int[word] = i
    int2word[i] = word

def euclidean_dist(vec1, vec2):
    return np.sqrt(np.sum((vec1-vec2)**2))

def find_closest(word_index, vectors):
    min_dist = 10000 # to act like positive infinity
    min_index = -1
    query_vector = vectors[word_index]
    for index, vector in enumerate(vectors):
        if euclidean_dist(vector, query_vector) < min_dist and not np.array_equal(vector, query_vector):
            min_dist = euclidean_dist(vector, query_vector)
            min_index = index
    return min_index

Z = tf.placeholder(tf.string)
find_closest_word = int2word[find_closest(word2int[Z], vectors)]

# Create SignatureDef metadata for the model
classification_inputs = tf.saved_model.utils.build_tensor_info(Z)
classification_outputs_classes = tf.saved_model.utils.build_tensor_info(find_closest_word)

classification_signature = (
      tf.saved_model.signature_def_utils.build_signature_def(
          inputs={
              tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                  classification_inputs
          },
          outputs={
              tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                  classification_outputs_classes
          },
          method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME))

Here is the error message when I run the code fragment above: 这是我运行上面的代码片段时的错误消息:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-65-016dad8c7403> in <module>()
     12     return min_index
     13 Z = tf.placeholder(tf.string)
---> 14 find_closest_word = int2word[find_closest(word2int[Z], vectors)]

KeyError: <tf.Tensor 'Placeholder_7:0' shape=<unknown> dtype=string>

Updated question: 更新的问题:

How can I convert a string tensor Z to a python string so that it can be used as index in word2int ? 如何将字符串张量Z转换为python字符串,以便可以将其用作word2int索引?

From your code, I guess you think Z is the word you pass as input to the network. 从您的代码中,我想您认为Z是您作为输入传递给网络的单词。 It isn't, since you define it as Z = tf.placeholder(tf.string) . 并非如此,因为您将其定义为Z = tf.placeholder(tf.string) Z is thus a placeholder object that will eventually be filled with a string in your feed_dict when you run the graph in a tf.Session instance by calling its run() method. 因此,Z是一个占位符对象,当您通过调用tf.Session实例的run()方法在其上run()图形时, 最终将在feed_dict使用字符串填充该对象。

Since your word2int dictionary is just a string-to-index dict, you get the KeyError when you try to use the placeholder as a key. 由于word2int词典只是一个字符串到索引的字典,因此当您尝试使用占位符作为键时,会出现KeyError

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow:TypeError:预期的二进制或Unicode字符串,得到了 <tf.Tensor 'Placeholder:0' shape=<unknown> dtype = string&gt; - Tensorflow: TypeError: Expected binary or unicode string, got <tf.Tensor 'Placeholder:0' shape=<unknown> dtype=string> InvalidArgumentError:预期 'tf.Tensor(False, shape=(), dtype=bool)' 为真 - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true ('试图更新张量',<tf.tensor: shape="()," dtype="float32," numpy="3.0"> )</tf.tensor:> - ('Trying to update a Tensor ', <tf.Tensor: shape=(), dtype=float32, numpy=3.0>) JupyterNotebook InvalidArgumentError: b'No files match pattern: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true - JupyterNotebook InvalidArgumentError: b'No files matched pattern: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true 获取参数<tf.Tensor 'batch:0' shape=(128, 56, 56, 3) dtype=float32>不能解释为张量。 - Fetch argument <tf.Tensor 'batch:0' shape=(128, 56, 56, 3) dtype=float32> cannot be interpreted as a Tensor. 如何从 dtype 是字符串的 tf.tensor 中获取字符串值 - how to get string value out of tf.tensor which dtype is string 如何在TensorFlow中清除tf.Tensor的形状信息? - How do I clear the shape information of tf.Tensor in TensorFlow? IAE:预期 &#39;tf.Tensor(False, shape=(), dtype=bool)&#39; 为真。 汇总数据:b&#39;最大框坐标值大于1.100000 - IAE: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b'maximum box coordinate value is larger than 1.100000 您必须使用dtype float和shape输入占位符张量&#39;Placeholder&#39;的值 - You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape TensorFlow:如何将tf.Tensor字符串转换为python datetime数据类型? - TensorFlow: How convert tf.Tensor string to python datetime datatype?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM