KeyError:“名称'scores'指的是图中未包含的操作。”

时间:2018-06-20 02:42:48

标签: python tensorflow deep-learning

得分和预测定义如下:

with tf.name_scope("output"):
            self.scores = tf.layers.dense(self.h_drop,
                                          num_classes,
                                          name='scores',
                                          kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                          bias_initializer=tf.constant_initializer(0.1),
                                          kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=l2_reg_lambda),
                                          bias_regularizer=tf.contrib.layers.l2_regularizer(scale=l2_reg_lambda))


            self.predictions = tf.argmax(self.scores, 1, name="predictions")
            softmax_scores = tf.nn.softmax(logits=self.scores, axis=-1)
            other_class_idx = tf.cast(num_classes-1, tf.int64)
            other_class_idx = tf.tile(tf.expand_dims(other_class_idx, 0), [tf.shape(softmax_scores)[0]] )
            is_other = tf.reduce_max(tf.cast(softmax_scores > threshold, tf.int8), axis=1)
            self.predictions1 =tf.where(is_other>0,tf.argmax(softmax_scores,axis=1),other_class_idx, name="predictions1" )

当我想恢复模型并检查分数和预测时。错误:

    scores = graph.get_operation_by_name("output/scores").outputs[0]      File "E:\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3606, in get_operation_by_name
    return self.as_graph_element(name, allow_tensor=False, allow_operation=True)
  File "E:\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3478, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "E:\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3538, in _as_graph_element_locked
    "graph." % repr(name))
KeyError: "The name 'scores' refers to an Operation not in the graph."

而且真正令人生畏的是,错误只发生在 scores = graph.get_operation_by_name(“ scores”)。outputs [0] 上。预测可以正常打印。

    # Tensors we want to evaluate
    scores = graph.get_operation_by_name("output/scores").outputs[0]
    predictions1 = graph.get_operation_by_name("output/predictions1").outputs[0]
    predictions = graph.get_operation_by_name("output/predictions").outputs[0]

0 个答案:

没有答案