1

I am new with Tensorflow and I can't figure out why I am getting this error since I think I've initialized all my variables.

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_2
[[Node: Variable_2/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_2"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_2)]]

It seems to belong to the follow summary_ops:

File "/Users/ldg/PycharmProjects/TF", line 274, in train
    summary_ops = setup_summaries()
File "/Users/ldg/PycharmProjects/TF.py", line 238, in setup_summaries
    logged_epsilon = tf.Variable(0.)

I am putting the dependent code in order make it clear.

g = tf.Graph()
    session = tf.InteractiveSession(graph=g)
    with g.as_default(), session.as_default():
        K.set_session(session)
        num_actions = get_num_actions()
        graph_ops = build_graph(num_actions)
        saver = tf.train.Saver()

        session.run(tf.global_variables_initializer())
        # session.run(init_op)

        # Initialize target network weights
        session.run(graph_ops["reset_target_network_params"])

        # Set up game environments (one per thread)
         envs = [gym.make(FLAGS.game) for i in range(FLAGS.num_concurrent)]

        summary_ops = setup_summaries()
        summary_op = summary_ops[-1]

        # Initialize variables
        summary_save_path = summary_dir + "/" + experiment
        writer = tf.summary.FileWriter(summary_save_path, session.graph)
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)


        # Show the agents training and write summary statistics
        last_summary_time = 0
        while True:
             now = time.time()
             if now - last_summary_time > FLAGS.summary_interval:
             summary_str = session.run(summary_op)
             writer.add_summary(summary_str, float(T))
             last_summary_time = now

whit the encapsulated setup_summaries()

def setup_summaries():
    episode_reward = tf.Variable(0.)
    tf.summary.scalar("Episode Reward", episode_reward)
    episode_ave_max_q = tf.Variable(0.)    
    tf.summary.scalar("Max Q Value", episode_ave_max_q)
    logged_epsilon = tf.Variable(0.)
    tf.summary.scalar("Epsilon", logged_epsilon)
    logged_T = tf.Variable(0.)
    summary_vars = [episode_reward, episode_ave_max_q, logged_epsilon]
    summary_placeholders = [tf.placeholder("float") for i in range(len(summary_vars))]
    update_ops = [summary_vars[i].assign(summary_placeholders[i]) for i in range(len(summary_vars))]
    summary_op = tf.summary.merge_all()
    return summary_placeholders, update_ops, summary_op

I had a look everywhere on the similar posts on Stackoverflow but I could not figure out a solution and really can't understand where I don't initialize my var.

Thank you in advance for your help.

1 Answer 1

3

You need to put the global variable initializer after summary_setup. The problem is caused by the fact that you are declaring tf.variable after you run the initializer. The following code snippet works

import tensorflow as tf
with tf.Graph().as_default():
    sess = tf.Session()
    with sess.as_default():

        summary_ops = setup_summaries()
        summary_op = summary_ops[-1]
        sess.run(tf.global_variables_initializer())        
        sess.run(summary_op)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.