Tensorboard Graph:Profiler 会话已启动

Tensorboard Graph: Profiler session started

我想使用 tensorflow 2 在 tensorboard 上显示我的网络图。我遵循了 this 教程并编写了如下代码:

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)

这样做时,我多次收到消息 Profiler session started.。当然,当我打开tensorboard时,Graph说发生了错误,无法显示任何东西。

我找到了回复 here

Actually, you can enable graph export in v2. You'll need to call tf.summary.trace_on() before the code you want to trace the graph for (e.g. L224 if you just want the train step), and then call tf.summary.trace_off() after the code completes. Since you only need one trace of the graph, I would recommend wrapping these calls with if global_step_val == 0: so that you don't produce traces every step.

实际上,要创建图形,只需要进行一次跟踪,并且在每个时期都进行跟踪是没有意义的。解决方案只是在调用跟踪之前检查一次,例如:

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)

我个人更喜欢这个decorator想法:

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()