当前位置: 代码迷 >> 综合 >> 解决“The name ‘x:0‘ refers to a Tensor which does not exist. The operation, ‘x‘, does not exist in the
  详细解决方案

解决“The name ‘x:0‘ refers to a Tensor which does not exist. The operation, ‘x‘, does not exist in the

热度:77   发布时间:2024-02-22 03:01:52.0

解决 Tensorflow 重载meta图时"The name 'x:0' refers to a Tensor which does not exist. The operation, 'y', does not exist in the graph."

  • 问题描述
  • 解决过程
  • 总结

(嫌啰嗦请直接看总结部分)

问题描述

今天tensorflow重载网络

with tf.Session() as sess:sess.run(tf.global_variables_initializer())saver = tf.train.import_meta_graph('/home/test01/csw/resnet34/ckpt3_20200617/raw_from_pythorch_0.65872/resnet34-Pytorch.meta')graph = tf.get_default_graph()x = graph.get_tensor_by_name("x:0")is_training = graph.get_tensor_by_name('is_training:0')accuracy = graph.get_tensor_by_name("accuracy:0")

报错

"The name 'x:0' refers to a Tensor which does not exist. The operation, 'y', does not exist in the graph.")

解决过程

一开始怀疑是训练时Saver在save的时候只save了trainable_variable,导致没有保存placeholder节点。
但是在本机和win Server做了下实验,发现只保存trainable_variable, meta里也是有placeholder节点的。
于是怀疑是Linux上的tf版本问题,在心中骂娘。但本着严谨的科学精神,在linux上也做了验证,发现meta里也是有placeholder节点的。

于是回忆了一下,几个月前构建resnet34忘了给x\y等placeholder,训练完模型后才在net.py文件上加上命名的,导致我重载的时候误认为有命名。

那么解决方法就是,用tf对placeholder的默认命名来找到这些节点啦,那么默认命名是啥呢?探索下

首先,构建一个纯placeholder图,并保存

import  tensorflow as tfa = tf.placeholder(tf.float32,shape=[1])
b = tf.placeholder(tf.float32,shape=[199,99])
c  = tf.placeholder(tf.float32,shape=[199,199,99])
d = tf.get_variable(shape=[1], name='b', dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01))with tf.Session() as sess:sess.run(tf.global_variables_initializer())var_list = tf.trainable_variables()saver = tf.train.Saver(var_list=var_list)saver.save(sess,'./test')

然后打印所有节点graph_op = graph.get_operations(),看下名字

import  tensorflow as tfwith tf.Session() as sess:sess.run(tf.global_variables_initializer())saver = tf.train.import_meta_graph('./test.meta')graph = tf.get_default_graph()graph_op = graph.get_operations()for i in graph_op:print(i)

部分结果截图:在这里插入图片描述

问题解决: placeholer默认命名是 Placeholer、Placeholer_1、Placeholer_2 …

验证下(注意 ‘:0’):

    b = graph.get_tensor_by_name('Placeholder:0')print(b)b = graph.get_tensor_by_name('Placeholder_1:0')print(b)b = graph.get_tensor_by_name('Placeholder_2:0')print(b)

结果:

Tensor("Placeholder:0", shape=(1,), dtype=float32)
Tensor("Placeholder_1:0", shape=(199, 99), dtype=float32)
Tensor("Placeholder_2:0", shape=(199, 199, 99), dtype=float32)

匹配上了,问题解决

总结

总结起来就是,忘了给placeholder命名,需要用默认命名Placeholer:0、Placeholer_1:0、Placeholer_2:0去获取

有点感悟,就是出了问题,首先怀疑自己出错误,不是现在犯了错,就是以前犯了错,工具错误的概率比自己小得多。
其次,写代码要规范呀,最好做好编辑、修改记录。

  相关解决方案