将展平层添加到顺序模型时出错



我使用Keras创建并训练了一个自动编码器。 训练完这个模型后,我只想得到编码器部分,所以我做了一些pop()

后来,我根据自动编码器模型的其余层创建了Sequential()模型:

model_seq = Sequential(layers=autoencoder.layers)

为了添加Flatten()层,我做了:

l_out = Flatten()(model_seq.output)
model_seq.layers.append(l_out)

在我看来,这应该足够了,所以我打电话给model_seq.summary()检查是否一切正常。 但不幸的是,我遇到了这个错误:

model_seq.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 256, 256, 1)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 256, 256, 32)      320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 128, 128, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 128, 128, 64)      18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 64, 64, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 64, 64, 128)       73856     
_________________________________________________________________
Traceback (most recent call last):
File "<ipython-input-49-cb26bbc86f4b>", line 1, in <module>
model_seq.summary()
File "C:UsersheldeMiniconda3libsite-packageskerasenginetopology.py", line 2740, in summary
print_fn=print_fn)
File "C:UsersheldeMiniconda3libsite-packageskerasutilslayer_utils.py", line 150, in print_summary
print_layer_summary(layers[i])
File "C:UsersheldeMiniconda3libsite-packageskerasutilslayer_utils.py", line 110, in print_layer_summary
fields = [name + ' (' + cls_name + ')', output_shape, layer.count_params()]
AttributeError: 'Tensor' object has no attribute 'count_params'

summary()引发错误的部分正是Flatten层应该在的位置。

我错过了什么吗?

在我看来,您正在混合SequentialFunctionalAPI。model_seq.add(Flatten())呢?

相关内容

  • 没有找到相关文章

最新更新