Keras plot_model 将模型架构中的问号替换为 None

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

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

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

Model Architecture image I am getting

转到第 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文件了。

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')