我尝试运行一个简单的程序来将Tensorflow会话保存到磁盘上,并将其命名为"spikes.cpkt“。尽管在交互式程序中,系统输出显示我已成功创建该文件,但我在文件系统中找不到该文件。
我使用的Tensorflow版本是0.11rc,使用的是Python 2。操作系统是Ubuntu 16.04。该程序是在Jupiter notebook中编写和运行的。
保存会话的源码如下:
# Import TensorFlow and enable interactive sessions
import tensorflow as tf
sess = tf.InteractiveSession()
# Let's say we have a series data like this
raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13.]
# Define a boolean vector called `spikes` to locate a sudden spike in raw data
spikes = tf.Variable([False] * len(raw_data), name='spikes')
# Don't forget to initialize the variable
spikes.initializer.run()
# The saver op will enable saving and restoring variables.
# If no dictionary is passed into the constructor, then the saver operators of all variables in the current program.
saver = tf.train.Saver()
# Loop through the data and update the spike variable when there is a significant increase
for i in range(1, len(raw_data)):
if raw_data[i] - raw_data[i-1] > 5:
spikes_val = spikes.eval()
spikes_val[i] = True
# Update the value of spikes by using the `tf.assign` function
updater = tf.assign(spikes, spikes_val)
# Don't forget to actually evaluate the updater, otherwise spikes will not be updated
updater.eval()
# Save the variable to the disk
save_path = saver.save(sess, "spikes.ckpt")
# Print out where the relative file path of the saved variables
print("spikes data saved in file: %s" % save_path)
# Remember to close the session after it will no longer be used
sess.close()系统的输出如图(1)所示:

在文件系统中创建的文件如图(2)所示:

磁盘中没有名为"spikes.ckpt“的文件。
发布于 2016-12-08 13:46:40
TensorFlow最近引入了一种新的检查点格式(Saver V2),它将检查点保存为一组具有公共前缀的文件。要创建使用旧格式的tf.train.Saver,您可以执行create it操作,如下所示:
saver = tf.train.Saver(write_version=tf.train.SaverDef.V1)发布于 2017-09-01 19:27:53
您只需要将变量的名称放在tf.trai.Saver中
saver = tf.train.Saver([spikes]) 发布于 2018-04-16 04:36:28
我也有同样的问题,我正在阅读Tensorflow的机器学习这本书,在论坛上你也可以找到解决方案,那就是使路径相对
save_path = saver.save(sess, "./spikes.ckpt")https://stackoverflow.com/questions/41032075
复制相似问题