Python 打印特定层的保存权重[Tensorflow]

Python 打印特定层的保存权重[Tensorflow],python,tensorflow,neural-network,deep-learning,conv-neural-network,Python,Tensorflow,Neural Network,Deep Learning,Conv Neural Network,我在PyCharm中将Tensorflow 1.0与python 3.5结合使用 在执行代码之后,我得到了每500次迭代保存的模型(索引、元数据和ckpt文件)。现在要加载模型,我们需要指向哪个文件? 我编写了下面的代码来加载ckpt(weights)文件(没有对上面的github代码进行任何更改) 出现以下错误 W c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework

我在PyCharm中将Tensorflow 1.0与python 3.5结合使用
在执行代码之后,我得到了每500次迭代保存的模型(索引、元数据和ckpt文件)。现在要加载模型,我们需要指向哪个文件?
我编写了下面的代码来加载ckpt(weights)文件(没有对上面的github代码进行任何更改)

出现以下错误

W c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\u kernel.cc:975]未找到:未成功的TensorSliceReader构造函数:找不到任何与D匹配的文件:/tmp/mnist\u tutorial/model.ckpt
回溯(最近一次呼叫最后一次):
文件“C:\Users\Admin\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\client\session.py”,第1021行,在\u do\u call中 返回fn(*args)
文件“C:\Users\Admin\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\client\session.py”,第1003行,在\u run\u fn中 状态,运行(元数据)
文件“C:\Users\Admin\AppData\Local\Programs\Python\35\lib\contextlib.py”,第66行,在退出 下一个(self.gen)
文件“C:\Users\Admin\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\framework\errors\u impl.py”,第469行,处于raise\u exception\u not\u ok\u状态

pywrap_tensorflow.TF_GetCode(状态)) tensorflow.python.framework.errors\u impl.NotFoundError:不成功的TensorSliceReader构造函数:找不到任何与D匹配的文件:/tmp/mnist\u tutorial/model.ckpt
[[Node:save/RestoreV2_1=RestoreV2[dtypes=[DT_FLOAT],[u device=“/job:localhost/replica:0/task:0/cpu:0”]([u recv_save/Const_0,save/RestoreV2_1/tensor_name,save/RestoreV2_1/shape_和_切片)]]

在开始训练之前(在卷积层的函数定义中),我们可以通过以下方式打印各个层的权重:

 w = tf.Variable(tf.truncated_normal([5, 5, 1, 64], stddev=0.1), name="W")
 b = tf.Variable(tf.constant(0.1, shape=[64]), name="B")           
 init = tf.global_variables_initializer()
 with tf.Session()as sess:
      sess.run(init)
      print("weight type is ", w)
      print('bias type is', b)
      print("random generated weights are: ")
      x = tf.Print('conv/W:0', [w],summarize=1600)
      sess.run(x)    
      print("Generated Biases are: ")
      y = tf.Print(b, [b],summarize=64)
      sess.run(y)
如果有许多卷积和完全连接的层,如何从
*.ckpt
文件加载和打印任何特定层的权重和偏差,因为上述方法不起作用



更新:对代码进行了更改并更新了错误消息

查看tensorflow存储库中的以读取检查点。

我遇到的问题与您的问题几乎相同,但我只是通过将模型及其索引文件添加到根目录(与运行**.py的目录相同)来解决。
然后您将遇到一些问题,如“UnicodeDecodeError:'ascii'编解码器无法解码位置37:序号不在范围(128)”中的字节0xe3,这将很容易更正。

不要重命名ckpt文件。假设文件是'model.ckpt-2000.index'和'model.cpkt-2000.data-00000-of-00001'。您应该使用saver还原的路径是“model.ckpt-2000”,尽管实际上没有这样的文件。对于读取变量,NewCheckpointReader是一个不错的选择。您可以检查代码以了解详细信息。已将
保存的.ckpt.数据-00000-of-00001
作为参数传递到
检查点.py
,得到上述相同错误。还尝试传递
saved.ckpt.index
saved.ckpt.meta
不起作用我认为您应该传递公共路径(在您的例子中是saved.ckpt)。github的默认代码保存为model.ckpt,收到下面的消息..因此我传递了model.ckpt
tensorflow.python.framework.errors\u impl.NotFoundError:unsuccessfulltensorslicereader构造函数:找不到任何与D匹配的文件:/tmp/mnist\u tutorial/model.ckpt[[Node:save/RestoreV2=RestoreV2[dtypes=[DT\u FLOAT],\u device=“/job:localhost/replica:0/task:0/cpu:0”](_recv_save/Const_0,save/RestoreV2/tensor_names,save/RestoreV2/shape_和_slices)]
实际有哪些文件?将名称作为参数传递到它们的共同点。我应该传递
model.ckpt-2000
现在它可以工作了。而且在加载之前我还必须定义相同形状的权重和偏差。。
 w = tf.Variable(tf.truncated_normal([5, 5, 1, 64], stddev=0.1), name="W")
 b = tf.Variable(tf.constant(0.1, shape=[64]), name="B")           
 init = tf.global_variables_initializer()
 with tf.Session()as sess:
      sess.run(init)
      print("weight type is ", w)
      print('bias type is', b)
      print("random generated weights are: ")
      x = tf.Print('conv/W:0', [w],summarize=1600)
      sess.run(x)    
      print("Generated Biases are: ")
      y = tf.Print(b, [b],summarize=64)
      sess.run(y)