The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

cxcycz (3) [Avatar] Offline
This code in my version will generate an error at:

save_path =, "spikes.ckpt")

To fix it, change this line

saver = tf.train.Saver()

to this

saver = tf.train.Saver({'spikes': spikes})

Edit: Looking through the v1.1 API, the original code should have worked. Don't know why it's failing for me. The error I was getting is listed below.

After doing some more investigation, I found the original code listing worked if run with python on the command line, so I'm thinking my problem might have been something to do with the jupyter notebook and the tensorflow interactive session. Maybe it's picking up variables from my other open notebooks? Don't know jupyter well enough yet.

FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-18-e4280db4e145> in <module>()
     15         print(i, raw_data[i], "Spike", spikes_val[i])
---> 17 save_path =, "spikes.ckpt")
     18 print("spikes data saved in file: %s" % save_path)

[snip]... anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.pyc in save(self, sess, save_path, global_step, latest_filename, meta_graph_suffix, write_meta_graph, write_state)
   1389       model_checkpoint_path =
   1390           self.saver_def.save_tensor_name,
-> 1391           {self.saver_def.filename_tensor_name: checkpoint_file})
   1392       model_checkpoint_path = compat.as_str(model_checkpoint_path)
   1393       if write_state:


FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable
	 [[Node: save_6/SaveV2 = SaveV2[dtypes=[DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL, DT_BOOL], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_6/Const_0, save_6/SaveV2/tensor_names, save_6/SaveV2/shape_and_slices, Variable, Variable_1, Variable_2, Variable_3, Variable_4, spikes, spikes_1, spikes_2, spikes_3, spikes_4, spikes_5, spikes_6)]]

393402 (2) [Avatar] Offline
An alternative way of fixing this error, which is still an error as of 5/20/2017 given my configuration (see bottom), is to insert a session call to tf.global_variables_initializer() between notes (4) and (5) in the code on lines 1 and 3, respectively, below.

Current (produces FailedPreconditionError error regarding tf.Variable)

saver = tf.train.Saver()

Fixed version

# Add this line   

saver = tf.train.Saver()

Credit for the fix, includes explanation:

I should also mention that I am using Python 3.5.2 on Debian and Tensorflow 1.1.
Nishant Shukla (52) [Avatar] Offline
Thank you all for pointing out the issue.

Could you please let me know if the latest code on GitHub fixes it?

gli (4) [Avatar] Offline
I'm using docker image

and the current version of `` doesn't work.

I have to apply the following diff to make it work:

diff --git ch02_basics/ ch02_basics/
index e112c97..2345da2 100644
--- ch02_basics/
+++ ch02_basics/
@@ -12,6 +12,8 @@ raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13]
spikes = tf.Variable([False] * len(raw_data), name='spikes')

# The saver op will enable saving and restoring

@@ -28,7 +30,7 @@ for i in range(1, len(raw_data)):

-save_path =, "spikes.ckpt")
+save_path =, "./spikes.ckpt")
print("spikes data saved in file: %s" % save_path)

Right now I'm running through all the examples, do you want me to prepare PR with all the fixes?