Search code examples
tensorflowkerasgoogle-colaboratory

how to plot input and output shapes on top of each other using polt_model in keras


I want to plot my model using Keras.utils.plot_model function. my problem is that when I plot the model, the input and output shapes do not place on top of each other and instead will be put alongside each other (like figure 1). Here is the code to plot this model:

model = tf.keras.models.Sequential()
model.add(layers.Embedding(100, 128, input_length=45,
                       input_shape=(45,), name='embed'))
model.add(layers.Conv1D(32, 7, activation='relu'))
model.add(layers.MaxPooling1D(5))
model.add(layers.Conv1D(32, 7, activation='relu'))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(1))
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=False)

figure 1

but I like to have the model plot such as figure 2 which is the typical figure we can find on internet and I created it many times before. I couldn't find any figsize or fontsize option in plot_model to try changing them. I use google Colaboratory Notebook.

Any help is very appreciated.

enter image description here


Solution

  • I also have the same issue and I finally found this github link. github

    Just because we're using tensorflow ver2.8.0, this problem seems to happen.

    As mentioned in the link, one valid solution is to change our tensorflow version such as tf-nightly.

    [tensorflow ver2.8.0]

    import tensorflow as tf
    tf.__version__
    

    2.8.0

    model = tf.keras.models.Sequential([
      tf.keras.layers.Dense(1,input_shape=[1], name="input_layer")
    ],name="model_1")
    
    model.compile(...)
    

    Awww this is the one we don't want to.

    [tensorflow nightly]

    !pip --quiet install tf-nightly #try not to use tf ver2.8
    import tensorflow as tf
    tf.__version__
    

    2.10.0-dev20220403

    #just do the same thing as above
    model = tf.keras.models.Sequential([
      tf.keras.layers.Dense(1,input_shape=[1], name="input_layer")
    ],name="model_1")
    
    model.compile(...)
    

    our expected output!

    I hope you solve this problem.