Ch 02: Concept 06

Saving variables

Create an interactive session and initialize a variable:


In [1]:
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()

The saver op will enable saving and restoring:


In [2]:
saver = tf.train.Saver()

Loop through the data and update the spike variable when there is a significant increase:


In [3]:
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
        updater = tf.assign(spikes, spikes_val)
        updater.eval()

Now, save your variable to disk!


In [4]:
save_path = saver.save(sess, "./spikes.ckpt")
print("spikes data saved in file: %s" % save_path)


spikes data saved in file: spikes.ckpt

Adieu:


In [5]:
sess.close()