Tensorflow 不计算摘要
Tensorflow doesn't calculate summary
我试图了解如何为 tensorboard 收集摘要并编写了一个简单的代码来将 x 从 1 递增到 5。
由于某些未知原因,我在所有步骤中看到变量 My_x 为 0。
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) 首先是在每个循环中你都在重新初始化全局变量。这是将 x 重置回其原始值 (0)。
2) 其次,当您更新 x 时,您正在使用 TensorFlow 加法操作将 link 覆盖到变量。您增加 x 的代码将 'x' 替换为 tf.add 操作,然后您的汇总值不再跟踪 tf.Variable 而是一个加法操作。如果你在定义它之后添加 "print(x)" 并在每个循环中让它 运行 一次,你会看到最初它开始时是 <tf.Variable 'x:0' shape=() dtype=int32_ref>
但是在看到 "x = x+1" 之后print(x) 变为 Tensor("add:0", shape=(), dtype=int32)
。这里可以看到tf.summary.scalar只兼容原值,也可以看到为什么不能更新
这是我修改后的代码,以便您可以在 Tensorboard 中看到 x 值的线性。
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()
我试图了解如何为 tensorboard 收集摘要并编写了一个简单的代码来将 x 从 1 递增到 5。
由于某些未知原因,我在所有步骤中看到变量 My_x 为 0。
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) 首先是在每个循环中你都在重新初始化全局变量。这是将 x 重置回其原始值 (0)。
2) 其次,当您更新 x 时,您正在使用 TensorFlow 加法操作将 link 覆盖到变量。您增加 x 的代码将 'x' 替换为 tf.add 操作,然后您的汇总值不再跟踪 tf.Variable 而是一个加法操作。如果你在定义它之后添加 "print(x)" 并在每个循环中让它 运行 一次,你会看到最初它开始时是 <tf.Variable 'x:0' shape=() dtype=int32_ref>
但是在看到 "x = x+1" 之后print(x) 变为 Tensor("add:0", shape=(), dtype=int32)
。这里可以看到tf.summary.scalar只兼容原值,也可以看到为什么不能更新
这是我修改后的代码,以便您可以在 Tensorboard 中看到 x 值的线性。
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()