我有下面的代码正在工作(没有错误)。我的问题是,我是否恢复模型正确?特别是我看不到语句print(v_)的任何输出。
所以,我想知道我做的是否正确:
编辑1
恢复这种方式有效吗?
saver = tf.train.Saver()
saver = tf.train.import_meta_graph('C:\\Users\\Abder-Rahman\\Desktop\\\Testing\\mymodel.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
print('model restored'编辑2
这就是我如何保存我的模型的:
#Save model
saver = tf.train.Saver()
saved_path = saver.save(sess, 'C:\\Users\\abc\\Desktop\\\Testing\\mymodel')
print("The model is in this file: ", saved_path)谢谢。
发布于 2017-03-28 02:45:41
你的保护程序代码是正确的。而变量必须在检索集合之前添加到集合中。tf.add_to_collection("vars", w1) tf.add_to_collection("vars", b1) ...然后all_vars = tf.get_collection('vars')
发布于 2017-03-28 02:10:25
通常,我会像这样还原一个TensorFlow模型:
with tf.Session(graph=graph) as session:
if os.path.exists(save_path):
# Restore variables from disk.
saver.restore(session, save_path)
else:
tf.initialize_all_variables().run()
print('Initialized')
# do the work
# ...
saver.save(session, save_path) # save the model示例代码可以获取这里。
我需要了解更多关于如何保存模型的信息,您的模型似乎是在保存之前恢复的,并且您的模型没有转向tf.graph并与会话连接。
发布于 2017-03-28 05:45:53
我假设您已经阅读了我的博客这里,模型保存机制非常简单,当您加载一个模型时,参数值和关系(这可能是您所关心的)由变量名称匹配。
例如
#simplesave.py
import tensorflow as tf
with tf.Graph().as_default() as g:#yes you have to have a graph first
with tf.Session() as sess:
b = tf.Variable(1.0, name="bias")
saver = tf.train.Saver()
saver.save(sess,'model') #b should be saved in the model file
#simpleload.py
import tensorflow as tf
with tf.Graph().as_default() as g:
with tf.Session() as sess:
#still need the definition, again
b = tf.Variable(0.0, name="bias")
saver = tf.train.Saver() #now it is satisfied...
saver.restore(sess,model)让我感到困惑的是,您使用了一个函数all_vars = tf.get_collection('vars'),但是您从未定义过一个名为"vars“的范围。您可能应该首先使用tf.all_variables()进行测试。
https://stackoverflow.com/questions/43057816
复制相似问题