Alex Rothberg
Alex Rothberg

Reputation: 10983

Using Restored Variables Across Sessions Gives "uninitialized value" Error

I am restoring variables into a graph in one session, closing the session and then creating a new session where I attempt to evaluate the graph. When doing so I get the error FailedPreconditionError: Attempting to use uninitialized value ....

Attempting to evaluate the graph in the first session works fine.

Calling sess.run(tf.global_variables_initializer()) on the second session "fixes" the problem, however it resets the variables' values.

The initial restoration is done using:

meta_graph_def = meta_graph.read_meta_graph_file(meta_graph_filename)
saver = tf.train.import_meta_graph(meta_graph_def)
sess = tf.Session(target=target, config=config)
saver.restore(sess, save_path)

Upvotes: 3

Views: 44

Answers (2)

Alex Rothberg
Alex Rothberg

Reputation: 10983

According to https://www.tensorflow.org/programmers_guide/faq:

What is the lifetime of a variable?

A variable is created when you first run the tf.Variable.initializer operation for that variable in a session. It is destroyed when that tf.Session.close.

So it seems that the variable cannot be used across a Session.close.

Upvotes: 2

Aaron
Aaron

Reputation: 2364

You have to restore the variables in each session.

Upvotes: 0

Related Questions