I tried to run a simple program to save a Tensorflow session to the disk as "spikes.cpkt". Although in the interactive program, the system output showed that I have successfully created that file, I can not find that file in the file system.
The version of Tensorflow I used is 0.11rc using Python 2. The operating system is Ubuntu 16.04. The program was written and run in Jupiter notebook.
The following is the source code of saving session:
# 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()
The output of the system is in Figure (1):
The files created in the File System is shown in Figure (2):
There is no file named "spikes.ckpt" in the disk.
TensorFlow recently introduced a new checkpoint format (Saver V2) which saves the checkpoint as a set of files with a common prefix. To create a tf.train.Saver
that uses the old format, you can create it as follows:
saver = tf.train.Saver(write_version=tf.train.SaverDef.V1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With