Search code examples
pythonmachine-learningdata-visualizationtensorboard

Plot custom data with Tensorboard


I have a personal implementation of a RL algorithm that generates performance metrics every x time steps.

That metric is simply a scalar, so I have an array of scalars that I want to display as a simple graph such as:

enter image description here

I want to display it in real time in tensorboard like my above example.

Thanks in advance


Solution

  • If you really want to use tensorboard you can start looking at tensorflow site and this datacamp tutorial on tensorboard.

    With tensorflow you can use summary.scalar to plot your custom data (as the example), no need for particular format, as the summary is taking care of that, the only condition is that data has to be a real numeric scalar value, convertible to a float32 Tensor.

    import tensorflow as tf
    
    import numpy as np
    
    import os
    import time
    
    now = time.localtime()
    subdir = time.strftime("%d-%b-%Y_%H.%M.%S", now)
    
    summary_dir1 = os.path.join("stackoverflow", subdir, "t1")
    summary_writer1 = tf.summary.create_file_writer(summary_dir1)
    
    for cont in range(200):
        with summary_writer1.as_default():
            tf.summary.scalar(name="unify/sin_x", data=np.math.sin(cont) ,step=cont)
            tf.summary.scalar(name="unify/sin_x_2", data=np.math.sin(cont/2), step=cont)
        summary_writer1.flush()
    

    tensorboard array of scalars

    That said, if you are not planning on using tensorflow with your implementation, I would suggest you just use matplotlib as this library also enables you to plot data in real time https://youtu.be/Ercd-Ip5PfQ?t=444.