Tensorboard图:探查器会话已启动
我想用tensorflow 2在tensorboard上显示我的网络图。我遵循教程,编写了如下代码:Tensorboard图:探查器会话已启动,tensorboard,tensorflow2.0,Tensorboard,Tensorflow2.0,我想用tensorflow 2在tensorboard上显示我的网络图。我遵循教程,编写了如下代码: for epoch in range(epochs): # Bracket the function call with # tf.summary.trace_on() and tf.summary.trace_export(). tf.summary.trace_on(graph=True, profiler=True) # Call only one tf.f
for epoch in range(epochs):
# Bracket the function call with
# tf.summary.trace_on() and tf.summary.trace_export().
tf.summary.trace_on(graph=True, profiler=True)
# Call only one tf.function when tracing.
z = train_step(x, y)
with writer.as_default():
tf.summary.trace_export(name="train_graph", step=0, profiler_outdir=logdir)
当我这样做时,我多次收到消息探查器会话启动。
。当然,当我打开tensorboard时,图表显示发生了错误,无法显示任何内容。我找到了响应
实际上,您可以在v2中启用图形导出。你需要打电话
tf.summary.trace_on()
在要跟踪图形的代码之前
(例如L224,如果您只想乘坐火车),然后致电
tf.summary.trace_off()
代码完成后。既然你只需要
如果有一个图表,我建议使用if包装这些调用
全局_step_val==0:
,这样就不会在每个步骤都生成跟踪
实际上,要创建图形,只需进行一次跟踪,而在每个历元进行跟踪是没有意义的。解决方案是在调用跟踪之前检查一次,如下所示:
for epoch in range(epochs):
if epoch == 0:
tf.summary.trace_on(graph=True, profiler=True)
z = train_step(x, y)
if epoch == 0:
with writer.as_default():
tf.summary.trace_export(name="train_graph", step=0, profiler_outdir=logdir)
我个人更喜欢这个想法:
def run_once(f):
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
return f(*args, **kwargs)
wrapper.has_run = False
return wrapper
@run_once
def _start_graph_tensorflow(self):
tf.summary.trace_on(graph=True, profiler=True) # https://www.tensorflow.org/tensorboard/graphs
@run_once
def _end_graph_tensorflow(self):
with self.graph_writer.as_default():
tf.summary.trace_export(name="graph", step=0, profiler_outdir=self.graph_writer_logdir)
for epoch in range(epochs):
_start_graph_tensorflow()
z = train_step(x, y)
_end_graph_tensorflow()