将Flatten图层添加到Sequential模型

时间:2018-06-09 12:23:06

标签: keras layer flatten sequential autoencoder

我使用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:\Users\helde\Miniconda3\lib\site-packages\keras\engine\topology.py", line 2740, in summary
        print_fn=print_fn)

      File "C:\Users\helde\Miniconda3\lib\site-packages\keras\utils\layer_utils.py", line 150, in print_summary
        print_layer_summary(layers[i])

      File "C:\Users\helde\Miniconda3\lib\site-packages\keras\utils\layer_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图层的位置。

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

在我看来,您正在混合SequentialFunctional API。那么model_seq.add(Flatten())呢?