我正在尝试在深度学习代码中设置备份/恢复机制。 不幸的是,我在尝试从仅使用元图初始化的保护程序恢复变量时遇到了麻烦:
import tensorflow as tf
# Create variable
v = tf.get_variable('v', shape=[3], initializer=tf.zeros_initializer)
# New node
inc_v = v.assign(v+1)
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# Create saver
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
inc_v.op.run()
# Save the variable
save_path = saver.save(sess, "./ckpt/my_model")
# Now reload everything:
tf.reset_default_graph()
# Create variable.
v = tf.get_variable("v", shape=[3])
# create the saver for restoration, this one works
#saver = tf.train.Saver()
#This one doesn't work
saver = tf.train.import_meta_graph('{}.meta'.format(save_path))
with tf.Session() as sess:
# restore
saver.restore(sess, save_path)
# Check the values of the variables
print('v : {}'.format(v.eval()))
第二个解决方案产生的错误如下:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value v
[[Node: _retval_v_0_0 = _Retval[T=DT_FLOAT, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](v)]]
虽然第一个(saver = tf.train.Saver())工作得很好。 你知道为什么我会遇到这种行为吗?我应该如何使用import_meta_graph函数?
提前感谢您的帮助