9

我正在尝试遵循 Deep Autoencoder Keras示例。我遇到了尺寸不匹配异常,但对于我的生活,我无法弄清楚为什么。它在我只使用一个编码维度时有效,但在我堆叠它们时无效。

例外:输入 0 与图层 dense_18 不兼容:
预期形状 =(无,128),找到形状 =(无,32)*

错误在线decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

from keras.layers import Dense,Input
from keras.models import Model

import numpy as np

# this is the size of the encoded representations
encoding_dim = 32

#NPUT LAYER
input_img = Input(shape=(784,))

#ENCODE LAYER
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim*4, activation='relu')(input_img)
encoded = Dense(encoding_dim*2, activation='relu')(encoded)
encoded = Dense(encoding_dim, activation='relu')(encoded)

#DECODED LAYER
# "decoded" is the lossy reconstruction of the input
decoded = Dense(encoding_dim*2, activation='relu')(encoded)
decoded = Dense(encoding_dim*4, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)

#MODEL
autoencoder = Model(input=input_img, output=decoded)


#SEPERATE ENCODER MODEL
encoder = Model(input=input_img, output=encoded)

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))

# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]

# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

#COMPILER
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
4

3 回答 3

11

感谢 Marcin 的提示。事实证明,所有解码器层都需要展开才能使其工作。

# retrieve the last layer of the autoencoder model
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]

# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))
于 2016-06-16T21:38:12.667 回答
2

问题在于:

# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]

在以前的模型中 - 最后一层是唯一的解码器层。所以它的输入也是解码器的输入。但是现在你有 3 个解码层,所以你必须回到第一个才能获得解码器的第一层。所以将此行更改为:

# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-3]

应该做的工作。

于 2016-06-11T15:48:19.090 回答
0

您需要将每个解码器层的转换应用到上一层。您可以像接受的答案一样手动展开和硬编码这些,或者下面的循环应该处理它:

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))

# retrieve the decoder layers and apply to each prev layer
num_decoder_layers = 3
decoder_layer = encoded_input
for i in range(-num_decoder_layers, 0):
    decoder_layer = autoencoder.layers[i](decoder_layer)

# create the decoder model
decoder = Model(encoded_input, decoder_layer)
于 2017-04-14T12:38:10.257 回答