5

I'm trying to visualize the model in Tensorboard without training.

I checked this and that, but this still doesn't work even for the simplest model.

import tensorflow as tf
import tensorflow.keras as keras
# Both tf.__version__ tensorboard.__version__ are 2.5.0

s_model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation='softmax')
])

logdir = '.../logs'
_callbacks = keras.callbacks.TensorBoard(log_dir=logdir)
_callbacks.set_model(s_model) # This is exactly suggested in the link

When I did the above, I get the error message:

Graph visualization failed.

Error: Malformed GraphDef. This can sometimes be caused by a bad network connection or difficulty reconciling mulitple GraphDefs; for the latter case, please refer to https://github.com/tensorflow/tensorboard/issues/1929.

I don't think this is a reconciliation problem because it is not a custom function, and if I compile the model, train, then I can get the graph visualization I wanted.

s_model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])

(train_images, train_labels), _ = keras.datasets.fashion_mnist.load_data()
train_images = train_images / 255.0

logdir = '.../logs'
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

s_model.fit(
    train_images,
    train_labels, 
    batch_size=64,
    epochs=5, 
    callbacks=[tensorboard_callback])

This gives the wanted graph visualization. But is there any other way to get graph visualization in Tensorboard without training?

Of course, I'm also aware that workaround, i.e. train with the tf.random.normal() for a while, would do the trick but I'm looking for the neat way like _callbacks.set_model(s_model)...

HyeonPhil Youn
  • 428
  • 4
  • 11
  • 1
    Could you please refer this similar was addressed [here](https://stackoverflow.com/questions/48391075/is-it-possible-to-visualize-a-tensorflow-graph-without-a-training-op) and also refer the [documentation](https://www.tensorflow.org/tensorboard/graphs). Thanks! –  Dec 17 '21 at 02:08
  • @TensorflowSupport the SO question you are referring to has only an answer for TF1 and not valid for TF2. And the documentation you refer to includes graphing while trainings, which is outside the OP's scope. – Mehdi LAMRANI Oct 30 '22 at 18:28
  • Ok! You can also visualize your model using plot_model or by loading the model in [netron](https://netron.app/) . But Tensorboard is better to have a bird view on how model is peforming in real time. –  Oct 31 '22 at 05:56

0 Answers0