【问题标题】:Keras plot_model replace the question mark by None in the model architectureKeras plot_model 在模型架构中将问号替换为 None
【发布时间】:2020-07-26 14:46:42
【问题描述】:

我想获得我正在研究的 lstm 模型的模型架构。我知道问号表示未指定的批量大小,因此它显示问号,但我希望将其替换为无。你能帮我解决一下如何用 None 替换问号吗,因为我大部分时间都看到 plot_model 显示为 None 的批量大小?下面是我正在使用的代码和我得到的图像。

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

Model Architecture image I am getting

【问题讨论】:

    标签: python keras deep-learning jupyter-notebook recurrent-neural-network


    【解决方案1】:

    转到您的第 3 方包所在的文件夹,转到

    tensorflow/keras/utils/vis_utils.py
    

    并更改第 208-209 行:

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

    '?' 更改为'None'

    你可以像这样找到源代码:

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

    然后你就可以找到正确的.py文件了。

    【讨论】:

    • 感谢您的回答,但因为我只使用了 'utils',因为当我包含 'utils.vis_utils' 时出现错误,所以我没有 'tensorflow/keras/utils/vis_utils.py ' 文件,只有 'tensorflow/keras/utils.py' 文件,其中没有作为 def format_shape 的功能?
    • C:\Users\user\anaconda3\envs\env\Lib\site-packages\tensorflow_core\python\keras\utils\vis_utils.py
    • 这是 tensorflow 核心...不是 tensorlfow
    【解决方案2】:

    Ubuntu anaconda3/envs/yourEnv/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/vis_utils.py

    Windows Anaconda\envs\yourEnv\Lib\site-packages\tensorflow\python\keras\utils\vis_utils.py

    改变

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

    收件人

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

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多