0
$\begingroup$

I'm new to Machine Learning and I have just gone through a tutorial explaining how to create a neural network in TensorFlow. I was wondering if it is possible to visualize the neural network I created. The output should be a picture like this

Thanks.

MRE:

import tensorflow as tf

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=6, activation='relu'))
ann.add(tf.keras.layers.Dense(units=6, activation='relu'))
ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
tf.keras.utils.plot_model(ann, to_file='model.png', show_shapes=True, 
                          show_layer_names=True, rankdir='TB',
                          expand_nested=False, dpi=96
                          )
$\endgroup$

1 Answer 1

1
$\begingroup$

This should do the trick:

tf.keras.utils.plot_model(
        model, to_file='model.png', show_shapes=False, show_layer_names=True,
        rankdir='TB', expand_nested=False, dpi=96
    )

This will generate an image like:

enter image description here

Example: https://www.tensorflow.org/guide/keras/functional

$\endgroup$
4
  • $\begingroup$ That doesn't seem to work perfectly. I'm getting a png file with a single box saying 'sequential_1' I'm using tf 2.0 and I have a network with four layers (including input / output) $\endgroup$
    – NNN
    Commented Dec 1, 2020 at 5:51
  • $\begingroup$ If you update the question with some code, I can have a look. Include the part where you define the model etc. $\endgroup$
    – Burger
    Commented Dec 1, 2020 at 7:24
  • $\begingroup$ added MRE code to the post $\endgroup$
    – NNN
    Commented Dec 1, 2020 at 9:00
  • 1
    $\begingroup$ Was able to get this to work following the instructions in stackoverflow.com/questions/61227174/… I needed to specify the shape of the input layer using ann.add(tf.keras.Input(shape=(12,))) $\endgroup$
    – NNN
    Commented Dec 1, 2020 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.