Python 3.x model.summary()和plot_model()显示tensorflow.keras中构建的模型中的任何内容
我正在测试一些东西,包括动态地构建一个Python 3.x model.summary()和plot_model()显示tensorflow.keras中构建的模型中的任何内容,python-3.x,tensorflow,keras,deep-learning,tensorflow2.0,Python 3.x,Tensorflow,Keras,Deep Learning,Tensorflow2.0,我正在测试一些东西,包括动态地构建一个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,activatio
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)
令我惊讶的是,它什么也没有显示。我甚至再次编译并运行了这些函数,结果什么也没有显示
model.summary
始终显示可训练和不可训练参数的数量,但不显示模型结构和层名称。为什么会这样?或者这正常吗?关于model.summary()
,不要一次将tf 2.x
和单机版混用。如果我在tf2.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')
哦!我的错!如果意外使用
keras.model
。谢谢你的帮助。很高兴帮助你