gpt4 book ai didi

tensorflow - model.summary() 输出与模型定义不一致

转载 作者:行者123 更新时间:2023-12-05 02:46:41 26 4
gpt4 key购买 nike

我正在使用子类化 API 构建一个简单的转换网络,我想使用摘要方法来了解我的模型的架构。但是,当我调用 model.summary() 时,图层乱序并且输出形状也没有显示。有没有一种干净的方法来解决这个问题?还是我需要覆盖模型类中的 model.summary() 方法。

这里是有问题的图层:

class thing(keras.Model):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.conv1 = keras.layers.convolutional.Conv2D(96,
kernel_size= (11, 11),
strides= 4,
activation = "relu",
data_format="channels_last",
input_shape= (277,277, 3))

self.flatten = keras.layers.Flatten(data_format="channels_last")

self.dense = keras.layers.Dense(4096, activation= "relu")

self.pool = keras.layers.pooling.MaxPooling2D(pool_size= (3,3), strides = 2,
data_format="channels_last")
def call(self, inputs):
conv1 = self.conv1(inputs)
pool1 = self.pool(conv1)
flatten_conv = self.flatten(pool1)
ff_1 = self.dense(flatten_conv)

return ff_1


a = thing()

a.build(input_shape=(None, 277, 277, 3))

a.summary()



OUTPUT:
Model: "thing_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_9 (Conv2D) multiple 34944
_________________________________________________________________
flatten_9 (Flatten) multiple 0
_________________________________________________________________
dense_14 (Dense) multiple 415240192
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 multiple 0
=================================================================
Total params: 415,275,136
Trainable params: 415,275,136
Non-trainable params: 0
_________________________________________________________________

最佳答案

model.summary 函数使用 tensorflow.python.keras.utils.layer_utils.print_summary打印模型结构信息的函数,它循环遍历 model.layer 打印所有层信息,而 model.layer 是一个包含所有层的列表在模型中定义(即使用 self.),此列表中层的顺序由您定义层的顺序决定。

因此,您可以使用与调用层相同的顺序定义层(尽管不会为您提供有关层连接和层输出形状的信息),或者您可以通过在自定义模型中定义一个简单的汇总函数来解决这个问题类:

def summary_model(self):
inputs = keras.Input(shape=(277, 277, 3))
outputs = self.call(inputs)
keras.Model(inputs=inputs, outputs=outputs, name="thing").summary()

并使用以下方式调用它:

a.summary_model()

哪些输出:

Model: "thing"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 277, 277, 3)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 67, 67, 96) 34944
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 33, 33, 96) 0
_________________________________________________________________
flatten (Flatten) (None, 104544) 0
_________________________________________________________________
dense (Dense) (None, 4096) 428216320
=================================================================
Total params: 428,251,264
Trainable params: 428,251,264
Non-trainable params: 0
_________________________________________________________________

关于tensorflow - model.summary() 输出与模型定义不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65365745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com