如何恢复tensorflow中模型使用的变量子集
我有2个文件,learn.py保存模型,learn_2.py恢复模型(这里是tf.variable a)并初始化新的tf.variable b,但出现了一些问题,这里是错误的:如何恢复tensorflow中模型使用的变量子集,tensorflow,restore,Tensorflow,Restore,我有2个文件,learn.py保存模型,learn_2.py恢复模型(这里是tf.variable a)并初始化新的tf.variable b,但出现了一些问题,这里是错误的: tensorflow.python.framework.errors_impl.NotFoundError: Key scope/bb not found in checkpoint 学习.py import tensorflow as tf with tf.variable_scope("scope"):
tensorflow.python.framework.errors_impl.NotFoundError: Key scope/bb not found in checkpoint
学习.py
import tensorflow as tf
with tf.variable_scope("scope"):
a = tf.get_variable("aa", shape=[2,4])
sess = tf.Session()
#sess.run(tf.global_variables_initializer())
sess.run(tf.initialize_variables([a]))
saver = tf.train.Saver()
save_path = saver.save(sess, "./tmp/model.ckpt")
print "---"
print sess.run(a)
学习2.py
import tensorflow as tf
with tf.variable_scope("scope"):
a = tf.get_variable("aa", shape=[2,4])
b = tf.get_variable("bb", shape=[2,4])
sess = tf.Session()
#sess.run(tf.global_variables_initializer())
#sess.run(tf.initialize_variables([b]))
saver = tf.train.Saver()
save_path = saver.restore(sess, "./tmp/model.ckpt")
sess.run(tf.initialize_variables([b]))
print "---"
print sess.run(a)
print sess.run(b)
在第二个脚本learn2.py中执行此操作。为我工作
import tensorflow as tf
with tf.variable_scope("scope"):
a = tf.get_variable("aa", shape=[2,4])
sess = tf.Session()
saver = tf.train.Saver()
save_path = saver.restore(sess, "/tmp/model.ckpt")
b = tf.get_variable("bb", shape=[2,4])
sess.run(tf.initialize_variables([b]))
print "---"
print sess.run(a)
print sess.run(b)
脚本1的输出(与learn.py相同)
脚本2的输出(如上所述)
说明:
您在第二个脚本中将变量“a”和“b”添加到图形中。当您尝试恢复时,它将搜索当前图形中的所有变量(“a”和“b”)。我的解决办法是
我希望这有帮助 哪一行给出了这个错误?如果在创建和初始化变量b之前加载图形,是否有效?如果是这样的话,你按照这个顺序做事会有问题吗?@gdelab在第10行(save_path=saver.restore(sess,“./tmp/model.ckpt”)),无论是先创建图形还是先恢复旧模型,都会有问题看看这个例子(显示保存+恢复变量):如果你还有任何疑问,请告诉我。@hars是的,你说得对,我知道如何保存和恢复变量,这是很有效的。但是,当我编写file1来保存模型,编写file2来加载模型时,它不起作用,就像我上面写的那样。也许你可以复制上面的代码并尝试一下,谢谢。是的,现在可以了!非常感谢你的解释。@color_tree,很高兴听到这个消息。请接受/投票回答,这样问题就结束了。
---
[[ 0.21811056 0.75089216 0.43180299 -0.36542225]
[-0.11786985 -0.26542974 0.68785524 -0.57991886]]
---
[[ 0.21811056 0.75089216 0.43180299 -0.36542225]
[-0.11786985 -0.26542974 0.68785524 -0.57991886]]
[[-0.62411451 -0.32599163 0.72495079 -0.09547448]
[-0.59518242 0.51209545 -0.68833208 -0.03813028]]