4

I want to plot my Keras model using the function below. Every time I try to do that, it gives me the following AssertionError. Can someone help?

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-37-4e7b8789844d> in <module>
      9 
     10 model,_ = create_model()
---> 11 plot_keras_model(model, show_shapes=True, show_layer_names=False)

<ipython-input-37-4e7b8789844d> in plot_keras_model(model, show_shapes, show_layer_names)
      6 from keras.utils.vis_utils import model_to_dot
      7 def plot_keras_model(model, show_shapes=True, show_layer_names=True):
----> 8     return SVG(model_to_dot(model, show_shapes=show_shapes, show_layer_names=show_layer_names, expand_nested=True).create(prog='dot',format='svg'))
      9 
     10 model,_ = create_model()

/opt/conda/lib/python3.6/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir, expand_nested, dpi, subgraph)
    180                             if not is_model(layer) and (
    181                                     not is_wrapped_model(layer)):
--> 182                                 assert dot.get_node(inbound_layer_id)
    183                                 assert dot.get_node(layer_id)
    184                                 dot.add_edge(pydot.Edge(inbound_layer_id,

AssertionError: 

import pydotplus as pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
def plot_keras_model(model, show_shapes=True, show_layer_names=True):
    return SVG(model_to_dot(model, show_shapes=show_shapes, show_layer_names=show_layer_names).create(prog='dot',format='svg'))

model,_ = create_model()
plot_keras_model(model, show_shapes=True, show_layer_names=False)
Dominik
  • 41
  • 3

2 Answers2

0

I just want to point out that I was having same assertion error ----> dot.get_node(inbound_layer_id) while trying keras.utils.plot_model(model, etc)

The issue got solved when I changed my imports:

from keras.models import Sequential
from keras.layers import Dense

To:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

I also had to change tensorflow.keras.utils import to_categorical because of the fully integration of Keras into Tensorflow. As seen here: Error in "from keras.utils import to_categorical"

Ivan
  • 1
0

We cannot exactly tell without knowing how you built your model. But if I had to guess then you're probably mixing tf.keras.layers with keras.layers in your Keras model. This is one of the reasons this type of error is thrown.

Ahmad Obeid
  • 91
  • 1
  • 3