Key Conv/biases not found in checkpoint

import tensorflow as tf

#save to file
#remember to define same dtype and shape when restore
#W = tf.Variable(tf.truncated_normal([2, 3]),dtype=tf.float32,name='weight')
#b = tf.Variable(tf.truncated_normal([3]),dtype=tf.float32,name='biases')#dtype不定义会报错
#
#init = tf.global_variables_initializer()
#
#saver = tf.train.Saver()
#
#with tf.Session() as sess:
#     sess.run(init)
#     #save sess 里所有的东西,格式注意.ckpt,可以是别的格式
#     save_path = saver.save(sess,"mynet/savetest1.ckpt")
#     print("save path:" , save_path)
    
#restore variable
#redefine same dtype and shape for you variables


W = tf.Variable(tf.truncated_normal([2, 3]),dtype = tf.float32,name = 'weight')
b = tf.Variable(tf.truncated_normal([3]),dtype = tf.float32,name = 'biases')
#not need intial
saver = tf.train.Saver()## define a saver for saving and restoring


with tf.Session() as sess:
     saver.restore(sess,"mynet/savetest1.ckpt")
     print("weight: ",sess.run(W))

     print("biases: ",sess.run(b))

运行错误:

Key Conv/biases not found in checkpoint

Variable rnn/rnn/basic_lstm_cell/kernel already exists(同样的解决方法)


原因: 保存和加载 在前后进行,在前后两次定义了

W = tf.Variable(xxx,name=”weight”)

相当于 在TensorFlow 图的堆栈创建了两次 name = “weight” 的变量,第二个(第n个)的实际 name 会变成 “weight_1” (“weight_n-1”),之后我们在保存 checkpoint 中实际搜索的是 “weight_n-1” 这个变量 而不是 “weight” ,因此就会出错。

解决方案: 
(1)在加载过程中,定义 name 相同的变量前面加 
tf.reset_default_graph() 清除默认图的堆栈,并设置全局图为默认图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值