Search code examples
tensorflowtensorboardtensorflow2.0

How to write graph to tensorboard using tensorflow 2.0?


Am doing this

    # eager on
    tf.summary.trace_on(graph=True, profiler=True)
    tf.summary.trace_export('stuff', step=1, profiler_outdir='output')
    # ... call train operation
    tf.summary.trace_off()

Profile section shows up in tensorboard but no graph yet.


Solution

  • Please find the github gist here where I have created a graph using Tf2.0 and visualized it in tensorboard. Also for more information, please go through the following link.

    Code for the same is mentioned below:

    !pip install tensorflow==2.0.0-beta1
    
    import tensorflow as tf
    # The function to be traced.
    @tf.function
    def my_func(x, y):
      # A simple hand-rolled layer.
      return tf.nn.relu(tf.matmul(x, y))
    
    # Set up logging.
    logdir = './logs/func'
    writer = tf.summary.create_file_writer(logdir)
    
    # Sample data for your function.
    x = tf.random.uniform((3, 3))
    y = tf.random.uniform((3, 3))
    
    # 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 = my_func(x, y)
    with writer.as_default():
      tf.summary.trace_export(
          name="my_func_trace",
          step=0,
          profiler_outdir=logdir)
    
    %load_ext tensorboard
    %tensorboard --logdir ./logs/func
    

    If the answer was helpful, please upvote it. Thanks!