Last active
June 23, 2023 16:36
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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