Tensorflow ValueError: No variables to save from
The tf.train.Saver
must be created after the variables that you want to restore (or save). Additionally it must be created in the same graph as those variables.
Assuming that Process.forward_propagation(…)
also creates the variables in your model, adding the saver creation after this line should work:
forward_propgation_results = Process.forward_propagation(images)
In addition, you must pass the new tf.Graph
that you created to the tf.Session
constructor so you'll need to move the creation of sess
inside that with
block as well.
The resulting function will be something like:
def evaluate():
with tf.Graph().as_default() as g:
images, labels = Process.eval_inputs(eval_data = eval_data)
forward_propgation_results = Process.forward_propagation(images)
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()
top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)
with tf.Session(graph=g) as sess:
sess.run(init_op)
saver.restore(sess, eval_dir)
print(sess.run(top_k_op))
Simply, there should be at least one tf.variable that is defined before you create your saver object.
You can get the above code running by adding the following line of code before the saver object definition.
The code that you need to add has come between the two ###.
import tensorflow as tf
import main
import Process
import Input
eval_dir = "/Users/Zanhuang/Desktop/NNP/model.ckpt-30"
checkpoint_dir = "/Users/Zanhuang/Desktop/NNP/checkpoint"
init_op = tf.initialize_all_variables()
### Here Comes the fake variable that makes defining a saver object possible.
_ = tf.Variable(initial_value='fake_variable')
###
saver = tf.train.Saver()
...
Note that since TF 0.11 — a long time ago yet after the currently accepted answer — tf.train.Saver
gained a defer_build
argument in its constructor that allows you to define variables after it has been constructed. However you now need to call its build
member function when all variables have been added, typically just before finilize
ing your graph.
saver = tf.train.Saver(defer_build=True)
# build you graph here
saver.build()
graph.finalize()
# now entering training loop