Search code examples
pythonkerasdeep-learningjupyter-notebookrecurrent-neural-network

Keras plot_model replace the question mark by None in the model architecture


I want to get model architecture of the lstm model I am working on. I know the question mark means batch sizes which are not specified so it is displaying question mark but I want it to be replaced by None. Can you please help me with how I can replace the question mark with None because I have mostly seen the batch size shown as None by plot_model? Below is the code I am using and the image I am getting.

    tf.keras.utils.plot_model(
    model,
    to_file='model.png',
    show_shapes=True,
    show_layer_names=True
    )

Model Architecture image I am getting


Solution

  • Go to your folder where 3rd party packages are, go to

    tensorflow/keras/utils/vis_utils.py
    

    And change line 208-209:

    def format_shape(shape):
        return str(shape).replace(str(None), '?')
    

    Change '?' with 'None'

    You can find the source code like this:

    import tensorflow as tf
    
    print(tf.__file__)
    
    'C:\Users\User\anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py'
    

    Then you can find the right .py file.