1

I try to understand how to collect summaries for tensorboard and wrote a simple code to increment x from 1 till 5.
For some unknown reason I see variable My_x as 0 in all steps.

import tensorflow as tf

tf.reset_default_graph() # To clear the defined variables/operations

# create the scalar variable
x = tf.Variable(0, name='x')

# ____step 1:____ create the scalar summary
x_summ = tf.summary.scalar(name='My_x', tensor=x)

# accumulate all summaries
merged_summary = tf.summary.merge_all()

# create the op for initializing all variables
model = tf.global_variables_initializer()

# launch the graph in a session
with tf.Session() as session:
    # ____step 2:____ creating the writer inside the session
    summary_writer = tf.summary.FileWriter('output', session.graph)

    for i in range(5):
        #initialize variables
        session.run(model)

        x = x + 1

        # ____step 3:____ evaluate the scalar summary
        merged_summary_ans, x_summ_ans, x_ans = session.run([merged_summary, x_summ, x])
        print(x_ans)
        print(x_summ_ans)
        print(merged_summary_ans)

        # ____step 4:____ add the summary to the writer (i.e. to the event file)
        summary_writer.add_summary(summary=x_summ_ans, global_step=i)

    summary_writer.flush()
    summary_writer.close()
    print('Done with writing the scalar summary')

1 Answer 1

3

There are two problems that I can see in your code:

1) The first is that in each loop you are re-initialising the global variables again. This is resetting x back to its original value (0).

2) Second of all when you are updating x you are overwriting the link to the variable with a TensorFlow addition operation. Your code to increase x replaces 'x' with a tf.add operation and then your summary value is no longer tracing a tf.Variable but an addition operation. If you add "print(x)" after you define it and have it run once in every loop, you will see that originally it starts out as <tf.Variable 'x:0' shape=() dtype=int32_ref> but then after seeing that "x = x+1" then print(x) becomes Tensor("add:0", shape=(), dtype=int32). Here you can see that tf.summary.scalar is only compatible with the original value and you can see why it can't be updated.

Here is code I altered to get it to work so you can see the linear of the value of x in Tensorboard.

import tensorflow as tf
tf.reset_default_graph()

x = tf.Variable(0, name='x')
x_summary = tf.summary.scalar('x_', x)
init = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    merged_summary_op = tf.summary.merge_all()
    summary_writer = tf.summary.FileWriter('output', session.graph)

    for i in range(5):
        print(x.eval())
        summary = session.run(merged_summary_op)
        summary_writer.add_summary(summary, i)
        session.run(tf.assign(x, x+1))

    summary_writer.flush()
    summary_writer.close()
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.