model.summary() 和 plot_model() 未显示 tensorflow.keras 中的构建模型

model.summary() and plot_model() showing nothing from the built model in tensorflow.keras

我正在测试一些东西,其中包括构建一个 FCNN 网络 动态地 。想法是构建层数及其基于给定列表的神经元,虚拟代码是:

neurons = [10,20,30] # First Dense has 10 neuron, 2nd has 20 and third has 30

inputs = keras.Input(shape=(1024,))
x = Dense(10,activation='relu')(inputs)

for n in neurons:
  x = Dense(n,activation='relu')(x)

out = Dense(1,activation='sigmoid')(x)
model = Model(inputs,out)
model.summary()
keras.utils.plot_model(model,'model.png')
for layer in model.layers:
  print(layer.name)

令我惊讶的是,它显示 nothing.I 甚至编译并再次 运行 函数,但没有任何结果。

model.summary 始终显示可训练和不可训练参数的数量,但不显示模型结构和层名称。为什么会这样?或者这是正常的?

关于 model.summary(),不要同时混合使用 tf 2.x 和独立 。如果我 运行 你在 tf 2.x 中建模,我会得到预期的结果。

from tensorflow.keras.layers import *
from tensorflow.keras import Model 
from tensorflow import keras 

# your code ...
model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 1024)]            0         
_________________________________________________________________
dense (Dense)                (None, 10)                10250     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                110       
_________________________________________________________________
dense_2 (Dense)              (None, 20)                220       
_________________________________________________________________
dense_3 (Dense)              (None, 30)                630       
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 31        
=================================================================
Total params: 11,241
Trainable params: 11,241
Non-trainable params: 0
_________________________________

关于绘制模型,在绘制 模型时可以使用几个选项。这是一个例子:

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