Skip to content

Instantly share code, notes, and snippets.

@himaprasoonpt
Last active June 23, 2023 16:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himaprasoonpt/66e990fbceea322b1548f823263b8b3e to your computer and use it in GitHub Desktop.
Save himaprasoonpt/66e990fbceea322b1548f823263b8b3e to your computer and use it in GitHub Desktop.
Easy way to launch tensorboard without calling fit or running model with data in tensorflow 2.0 using callbacks api
"""
This example doesn't use summary writer . This is the easiest way to launch tensorboard without calling fit or running model with data
"""
from tensorflow.python import keras
from tensorflow.python.keras import layers
import tensorflow as tf
import shutil
from tensorboard import program
logdir = "/tmp/my_tensorboard/"
shutil.rmtree(logdir)
# The Functional model Part / You may use sequential or Model subclassing
inputs = keras.Input(shape=(784,), name='img')
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)
# Create model Graph
model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')
TC = tf.keras.callbacks.TensorBoard(logdir)
TC.set_model(model=model)
"""Now launch Tensorboard either through commandline using : tensorboard --logdir your_log_dir
Or Programmatically as follows
"""
def launch_tensorboard(log_dir, clear_on_exit=False):
"""
Runs tensorboard with the given log_dir and wait for user input to kill the app.
:param log_dir:
:param clear_on_exit: If True Clears the log_dir on exit and kills the tensorboard app
:return:
"""
tb = program.TensorBoard()
tb.configure(argv=[None, '--logdir', log_dir])
print("Launching Tensorboard ")
url = tb.launch()
print(url)
try:
input("Enter any key to exit")
except:
pass
finally:
if clear_on_exit:
shutil.rmtree(logdir, ignore_errors=True)
print("\nCleared Logdir")
launch_tensorboard(log_dir=logdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment