Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么TensorFlow会尝试从我没有使用的检查点还原密钥';你不要求吗?_Tensorflow - Fatal编程技术网

为什么TensorFlow会尝试从我没有使用的检查点还原密钥';你不要求吗?

为什么TensorFlow会尝试从我没有使用的检查点还原密钥';你不要求吗?,tensorflow,Tensorflow,当我尝试从检查点还原变量时,TensorFlow会查找我没有指定的键并报告错误 我可以用 import tensorflow as tf sess = tf.InteractiveSession() raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13] spikes = tf.Variable([False] * len(raw_data), name='spikes') spikes.initializer.run() # After variable

当我尝试从检查点还原变量时,TensorFlow会查找我没有指定的键并报告错误

我可以用

import tensorflow as tf
sess = tf.InteractiveSession()

raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13]
spikes = tf.Variable([False] * len(raw_data), name='spikes')
spikes.initializer.run()

# After variables, listing them in a dict if not all are to be saved
saver = tf.train.Saver()

for i in range(1, len(raw_data)):
    spikes_val = spikes.eval() # Get the current values
    spikes_val[i] = True # Update new value
    updater = tf.assign(spikes, spikes_val).eval() # Assign updated values to Variable

save_path = saver.save(sess, os.path.join(os.getcwd(), '_save_eg.ckpt'))
print("spikes data saved in file: %s" % save_path)

sess.close()
并且可以证实这已经成功了

tf.contrib.framework.list_variables(save_path)

[('spikes', [8])]
正如所料

但是当我试图用

sess_in = tf.InteractiveSession()

spikes_read = tf.Variable([False] * len(raw_data), name='spikes')
tf.train.Saver().restore(sess_in, save_path)
print(spikes_read)

sess_in.close()
我得到一个键“spikes_1”的
NotFoundError
,我没有要求:

NotFoundError: Key spikes_1 not found in checkpoint [[Node: save_1/RestoreV2_1 = RestoreV2[dtypes=[DT_BOOL], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_1/Const_0_0, save_1/RestoreV2_1/tensor_names, save_1/RestoreV2_1/shape_and_slices)]]
为什么TensorFlow试图从我没有要求的检查点恢复密钥



这基本上是第44页的例子,它不能按原样工作,这本书中的许多代码也是如此。

你的阅读阶段是错误的。您之前已经声明了
spikes
变量,这意味着当前图形中存在名为
spikes
do的变量

当您尝试恢复模型时,您要执行以下操作:

spikes_read = tf.Variable([False] * len(raw_data), name='spikes')
这是一个名为
spikes
的变量的新声明:此变量已存在于当前图形中,因此Tensorflow为您添加
\u 1
后缀,以避免冲突

在下一行:

tf.train.Saver().restore(sess_in, save_path)
您要求
保存程序使用当前图形从
保存路径
还原变量。 显然,这意味着saver不仅希望找到先前声明的
spikes
变量,还希望找到新的
spikes\u 1
变量

您可以用两种不同的方式解决您的问题:

第一条路 如果查看的文档,您可以看到is构造函数接受要恢复的变量列表。 因此,您可以使用前面声明的变量
spikes
,并将其作为构造函数参数传递

因此,您的阅读阶段变成:

sess_in = tf.InteractiveSession()

# comment the `spikes_1` variable definition and just use the
# `spikes` varialble previously declared
#spikes_read = tf.Variable([False] * len(raw_data), name='spikes')
tf.train.Saver().restore(sess_in, save_path)
# or you can explicit the variable into the saver in this way, that's
# the same exact thing
# tf.train.Saver([spikes]).restore(sess_in, save_path)
print(spikes_read)

sess_in.close()
第二条路 您可以将读取阶段包装到新的空图形中。因此,您现在可以声明一个变量,该变量的名称将是
spikes
,该变量将由saver填充:

new_graph = tf.Graph()
with new_graph.as_default():
    sess_in = tf.InteractiveSession()

    spikes_read = tf.Variable([False] * len(raw_data), name='spikes')
    tf.train.Saver().restore(sess_in, save_path)
    print(spikes_read)

第一种方法与一般情况并不匹配,在代码的其他部分,我可能不知道是否有一个名为
spikes
的变量存在,我想从名为“spikes”的检查点读取变量。第二种方法产生错误(例如“RuntimeError:会话图为空。在调用run()之前向图中添加操作”)。抱歉。第二种方法必须取消对变量定义的注释,这样才能工作,我将编辑代码。第一种方案是可行的,但是如果您想获得一个已经在图中定义的变量,您必须在启用了重用的变量范围内使用
tf.get_variable('spikes
)。看看这里