【发布时间】:2018-06-09 12:23:06
【问题描述】:
我已经使用 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 层应该在的位置。
我错过了什么吗?
【问题讨论】:
-
您没有通过混合功能和顺序 API 来使用正确的方法。您只需要使用函数式 API 并通过使用编码器层制作模型来获取编码器。
标签: keras layer flatten sequential autoencoder