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

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


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


Tensorflow1的;2的没用过,不保证适用。


问题描述

今天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 tf

a = 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 tf

with 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去获取

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

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值