0

I am trying to plot my model with the tf.keras.utils.model_to_dot() function but keep getting the following error:

TypeError: object of type 'Cluster' has no len()

Here is the code I use:

import tensorflow
import pydot

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
  tf.keras.layers.Dense(128,activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

model_graph = tf.keras.utils.model_to_dot(model, expand_nested=True, subgraph=True)
graph = pydot.graph_from_dot_data(model_graph)
graph.write_png('model.png')

What do I do wrong here?

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83

1 Answers1

0

Following the documentation, here https://pydotplus.readthedocs.io/reference.html .

pydotplus.graphviz.graph_from_dot_data(data)[source]
Load graph as defined by data in DOT format.

The data is assumed to be in DOT format. It will be parsed and a Dot class will be returned, representing the graph.

You're code should be something like this,

import pydot

(graph,) = pydot.graph_from_dot_file('somefile.dot')
graph.write_png('somefile.png')

Thanks to @Judge Maygarden, Converting dot to png in python .

Basically, it's expecting a .dot file, not the model itself.

Maybe you're looking for tf.keras.utils.plot_model? For example, from https://www.tensorflow.org/api_docs/python/tf/keras/utils/plot_model,

tf.keras.utils.plot_model(
    model, to_file='model.png', show_shapes=False, show_layer_names=True,
    rankdir='TB', expand_nested=False, dpi=96
)
Saif Ul Islam
  • 345
  • 3
  • 14
  • I don't want to use `tf.keras.utils.plot_model()`. Can you provide a minimal working example that works with a Keras model? I tried to use `pydot` without success. – Gilfoyle Aug 26 '20 at 19:49