Search code examples
tensorflow2.0tensorboard

How to visualize tensorboard for tensorflow 2


I am a newbie and I have had some trouble in using tensorboard. I stared Spyder in Anaconda prompt by

conda activate D:\Software\Anaconda\envs\tf
spyder

And this is my simple code in file trial.py

import tensorflow as tf
a = 2
b = 3
x = tf.add(a, b)
writer = tf.summary.create_file_writer('./graphs')
print(writer)

Then I ran this code in spyder. I opened another Anaconda prompt and added this line

conda activate D:\Software\Anaconda\envs\tf
tensorboard --logdir=D:\Dung\Maytinh\Python\graphs --port 6006

I opened my Chrome and typed: http://localhost:6006/. The result was below: enter image description here

I have tried many times and I could not understand where the error was. Some guides in the internet belong to tensorflow 1 and can not suit to handle. Please help me!


Solution

  • This is based on the documentation

    import tensorflow as tf
    a = 2
    b = 3
    
    @tf.function
    def func(x, y):
      return tf.add(a, b)
    
    writer = tf.summary.create_file_writer('./graphs')
    
    tf.summary.trace_on(graph=True, profiler=True)
    z = func(a, b)
    with writer.as_default():
      tf.summary.trace_export(
          name="trace",
          step=0,
          profiler_outdir='./graphs')
    

    tensorboard --logdir ./graphs