我刚刚开始玩Tensorboard,并想创建一个简单的示例,其中有一个调用函数的循环。在这个函数中,我有一个张量变量,它的增量是1,然后我把它加到一个摘要中。你知道吗
我得到一个failedPrerequision错误:尝试使用非斜体值x\u标量
但我以为我是在用第10行和第14行初始化xu标量。初始化的正确方法是什么?你知道吗
import tensorflow as tf
tf.reset_default_graph() # To clear the defined variables and operations of the previous cell
# create the scalar variable
x_scalar = tf.get_variable('x_scalar', shape=[], initializer=tf.truncated_normal_initializer(mean=0, stddev=1))
# ____step 1:____ create the scalar summary
first_summary = tf.summary.scalar(name='My_first_scalar_summary', tensor=x_scalar)
step = 1
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('./graphs', sess.graph)
sess.run(x_scalar.assign(1))
print(sess.run(x_scalar))
print("---------------------------")
def main():
global init
global first_summary
global step
# launch the graph in a session
# with tf.Session() as sess:
# # ____step 2:____ creating the writer inside the session
# writer = tf.summary.FileWriter('./graphs', sess.graph)
for s in range(100):
func()
def func():
global init
global first_summary
global step
global x_scalar
with tf.Session() as sess:
# ____step 2:____ creating the writer inside the session
# loop over several initializations of the variable
sess.run(x_scalar.assign(x_scalar + 1))
# ____step 3:____ evaluate the scalar summary
summary = sess.run(first_summary)
# ____step 4:____ add the summary to the writer (i.e. to the event file)
writer.add_summary(summary, step)
step = step + 1
print('Done with writing the scalar summary')
if __name__ == '__main__':
main()
您以不同的方式初始化了变量工作组会议(). 当使用你的工作组会议()作为上下文管理器,会话在代码块完成后自动关闭。你知道吗
您可以使用检查点和元图来保存graph+权重,然后将它们加载到新创建的会话中。你知道吗
或者你可以试着传递一个会话
编辑:进行了更正
相关问题 更多 >
编程相关推荐